i have byte[] field file contents need encrypt. nothing special or fancy, enough make sure next person gets won't able decode without effort. use encryption comes .net framework 4.0 not need make file bigger is.
i thought reversing array or adding few bytes end...?
if can avoid making array bigger great.
any suggestions?
thanks!
does addition of 1-16 bytes hurt? aes pad default using below method:
private static void encryptthendecrypt() { byte[] message; // fill bytes byte[] encmessage; // encrypted bytes byte[] decmessage; // decrypted bytes - s/b same message byte[] key; byte[] iv; using (var rijndael = new rijndaelmanaged()) { rijndael.generatekey(); rijndael.generateiv(); key = rijndael.key; iv = rijndael.iv; encmessage = encryptbytes(rijndael, message); } using (var rijndael = new rijndaelmanaged()) { rijndael.key = key; rijndael.iv = iv; decmessage = decryptbytes(rijndael, encmessage); } } private static byte[] encryptbytes( symmetricalgorithm alg, byte[] message) { if ((message == null) || (message.length == 0)) { return message; } if (alg == null) { throw new argumentnullexception("alg"); } using (var stream = new memorystream()) using (var encryptor = alg.createencryptor()) using (var encrypt = new cryptostream(stream, encryptor, cryptostreammode.write)) { encrypt.write(message, 0, message.length); encrypt.flushfinalblock(); return stream.toarray(); } } private static byte[] decryptbytes( symmetricalgorithm alg, byte[] message) { if ((message == null) || (message.length == 0)) { return message; } if (alg == null) { throw new argumentnullexception("alg"); } using (var stream = new memorystream()) using (var decryptor = alg.createdecryptor()) using (var encrypt = new cryptostream(stream, decryptor, cryptostreammode.write)) { encrypt.write(message, 0, message.length); encrypt.flushfinalblock(); return stream.toarray(); } }
Comments
Post a Comment