c# - How to change padding option on an existing encrypt/decrypt code? -
i have following code, , working 2 years. have started see random issue padding. when random, mean same thing works 1 day doesn't work other day. , someday decides work randomly.
now, if add padding none mentioned in answers above, mess encrypted files. i'm thinking create different approach using goto statement in catch block in method same way did when changed encryption key. or is there better approach change padding none?
/// <summary> /// /// </summary> [serializable] public static class encryptdecrypt { private static string encryptionkey_old = "makv2spbni99212"; private static string encryptionkey = "yi9bpgg1cxr01gbwgpzrtoznojhpkgbozisbg5jl3iru48yhcfgdzu76fdpa5fuu"; /// <summary> /// /// </summary> /// <param name="cleartext"></param> /// <returns></returns> public static string encrypt(string cleartext) { byte[] whitebs = encoding.unicode.getbytes(cleartext); using (aes encryptor = aes.create()) { rfc2898derivebytes pdb = new rfc2898derivebytes(encryptionkey, new byte[] { 0x49, 0x76, 0x61, 0x6e, 0x20, 0x4d, 0x65, 0x64, 0x76, 0x65, 0x64, 0x65, 0x76 }); encryptor.key = pdb.getbytes(32); encryptor.iv = pdb.getbytes(16); encryptor.mode = ciphermode.ecb; encryptor.padding = paddingmode.pkcs7; using (memorystream ms = new memorystream()) { using (cryptostream cs = new cryptostream(ms, encryptor.createencryptor(), cryptostreammode.write)) { cs.write(whitebs, 0, whitebs.length); cs.flushfinalblock(); cs.close(); } cleartext = convert.tobase64string(ms.toarray()); } } return cleartext.endswith("==") ? cleartext.remove(cleartext.length - 2) : cleartext; } /// <summary> /// /// </summary> /// <param name="ciphertext"></param> /// <returns></returns> public static string decrypt(string ciphertext) { int attempts = 0; string exception = string.empty; starthere: ciphertext = ciphertext.replace(" ", "+"); byte[] cipherbytes; try { cipherbytes = convert.frombase64string(ciphertext); } catch { cipherbytes = convert.frombase64string(ciphertext + "=="); } using (aes encryptor = aes.create()) { rfc2898derivebytes pdb = new rfc2898derivebytes(encryptionkey, new byte[] { 0x49, 0x76, 0x61, 0x6e, 0x20, 0x4d, 0x65, 0x64, 0x76, 0x65, 0x64, 0x65, 0x76 }); encryptor.key = pdb.getbytes(32); encryptor.iv = pdb.getbytes(16); encryptor.mode = ciphermode.ecb; encryptor.padding = paddingmode.pkcs7; try { using (memorystream ms = new memorystream()) { using (cryptostream cs = new cryptostream(ms, encryptor.createdecryptor(), cryptostreammode.write)) { cs.write(cipherbytes, 0, cipherbytes.length); cs.flushfinalblock(); cs.close(); } ciphertext = encoding.unicode.getstring(ms.toarray()); } } catch { if (attempts == 2) throw; encryptionkey = encryptionkey_old; attempts++; goto starthere; } } return ciphertext; } ' changing not idea don't know how go doing because there thousands of files encrypted code.
it looks padding error being used determine decryption successful, wrong , not work! need method determine successful decryption, in case if correct key used.
see pkcs#7 padding.
if decryption wrong key result random data , there 1/256 probability last byte 0x01 correct padding , there no padding error reported. lesser extent other valid padding randomly occur.
another method necessary determine if correct decryption obtained, mac used authenticate encryption. can in data known, called crib , success rate determined uniqueness of it.
re: padding: unless length of data encrypted exact multiple of block size (16-bytes aes) padding required , pkcs#7 padding correct padding
Comments
Post a Comment