hi guys, i need to encrypt a plain text limited to 15 characters but the output the encryption must also be 15 characters since i am limited to database constraint.
how can i do this?

thanks

Dani AI

Generated

Brief summary tied to the thread: wants a reversible cipher whose output is exactly 15 characters to fit a fixed column, and rightly flagged the practical problems with trying to force ciphertext into a small fixed width. The secure, maintainable choices are (A) use a reversible algorithm that preserves format/length, (B) replace the column with something that can hold real ciphertext, or (C) stop using reversible encryption and use tokenization or a hashed/pseudonymized value if decryption is not required.

Format-preserving encryption (FPE) is the canonical way to get reversible ciphertext that matches an input alphabet and length. FPE modes are designed to encrypt a 15-character string into another 15-character string from a chosen alphabet. This requires a vetted FPE implementation, a strong symmetric key, and a non-secret tweak (or per-record tweak) to avoid deterministic leaks. Important caution: very short domains reduce effective security—if the plaintext alphabet and length create a small search space, an attacker can brute-force.

If changing the schema is impossible, tokenization or a secure mapping table is often the simplest, safest approach: store a 15-char token in the constrained column and keep the plaintext-to-token mapping in a protected table (accessible only to authorized services). For irreversible needs (no decryption), use a keyed MAC or HMAC and keep in mind truncation reduces collision resistance and is not reversible.

Compact pseudocode (conceptual, not library-specific):

/* FPE (reversible, preserves length/alphabet) */
var fpe = FpeEngine.Create(key, alphabet);
string ct = fpe.Encrypt(plaintext, tweak: recordId);
string pt = fpe.Decrypt(ct, tweak: recordId);

/* Tokenization (reversible via lookup) */
string token = GenerateRandomAlphaNum15();
StoreMapping(token, plaintext);   // mapping table in secure store
StoreInColumn(token);

Security checklist: use vetted libraries (no DIY FPE), protect and rotate keys (KMS/Key Vault), use per-record tweak to avoid frequency leaks, and prefer expanding the column or storing binary ciphertext when possible.

For a good block cipher the responsible thing to do is change the constraint to expect cipher text length instead of plain text length or change the limit of the plain text to be short enough to meet the current database constraint. If you use a block cipher it is easy to figure out the size of the cipher text because it will be a multiple of the block size for the algorithm.

For example, if you want to use Blowfish and the plain text limit is 15 characters, that is 30 bytes for C#'s char type and the next block multiple is 32 bytes because Blowfish uses a block size of 8 bytes. Then you need to add the overhead of a text representation like base 64. Base 64 adds 4 characters to the string for every 3 bytes and does some padding for lengths not divisible by 3. To hold the base 64 cipher text of 15 two byte characters the database constraint should grow from 15 characters to 44 characters.

Growing the database constraint is better than shrinking the application constraint because if you do the math, the plain text length would have to be very small for 15 base 64 characters.

If you really want the cipher text to have the same length as the plain text, look into stream cipher algorithms. AES can be run in a mode called CFB that turns it into a stream cipher. Or you can use any of the crappy algorithms like XOR or any variation of the Caesar ciphers. But I still think changing the database to hold an encrypted string instead of plain text is best. :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.