c# - Length of the data to decrypt is invalid. CryptoStream decrypting a string -
i have encrypt , decrypt method works on 1 site when copying on site started error: length of data decrypt invalid. have had online , on of questions find here none of them seemed have fix works me
the code encrypts without issue not decrypt correctly. error occurs on line here
int decryptedbytecount = cryptostream.read(plaintextbytes,0, plaintextbytes.length);
it string trying decrypt
here full decrypt method:
public static string decrypt(string ciphertext) { string passphrase = "@not real pass"; // can string string saltvalue = "@not real salt"; // can string string hashalgorithm = "sha1"; // can "md5" int passworditerations = 2; // can number string initvector = "@1b2c3d4e5f6g7h8"; // must 16 bytes int keysize = 256; // can 192 or 128 // convert strings defining encryption key characteristics byte // arrays. let assume strings contain ascii codes. // if strings include unicode characters, use unicode, utf7, or utf8 // encoding. byte[] initvectorbytes = encoding.ascii.getbytes(initvector); byte[] saltvaluebytes = encoding.ascii.getbytes(saltvalue); // convert our ciphertext byte array. byte[] ciphertextbytes = convert.frombase64string(ciphertext); // first, must create password, key // derived. password generated specified // passphrase , salt value. password created using // specified hash algorithm. password creation can done in // several iterations. passwordderivebytes password = new passwordderivebytes( passphrase, saltvaluebytes, hashalgorithm, passworditerations); // use password generate pseudo-random bytes encryption // key. specify size of key in bytes (instead of bits). byte[] keybytes = password.getbytes(keysize / 8); // create uninitialized rijndael encryption object. rijndaelmanaged symmetrickey = new rijndaelmanaged(); // reasonable set encryption mode cipher block chaining // (cbc). use default options other symmetric key parameters. symmetrickey.mode = ciphermode.cbc; // generate decryptor existing key bytes , initialization // vector. key size defined based on number of key // bytes. icryptotransform decryptor = symmetrickey.createdecryptor(keybytes,initvectorbytes); // define memory stream used hold encrypted data. memorystream memorystream = new memorystream(ciphertextbytes); // define cryptographic stream (always use read mode encryption). cryptostream cryptostream = new cryptostream(memorystream, decryptor, cryptostreammode.read); // since @ point don't know size of decrypted data // be, allocate buffer long enough hold ciphertext; // plaintext never longer ciphertext. byte[] plaintextbytes = new byte[ciphertextbytes.length]; // start decrypting. int decryptedbytecount = cryptostream.read(plaintextbytes, 0, plaintextbytes.length); // close both streams. memorystream.close(); cryptostream.close(); // convert decrypted data string. // let assume original plaintext string utf8-encoded. string plaintext = encoding.utf8.getstring(plaintextbytes, 0, decryptedbytecount); // return decrypted string. return plaintext; } }
let me know if encryption method needed well
edit
as requested here encryption code:
public static string encrypt(string plaintext) { string passphrase = "@not real password"; // can string string saltvalue = "@not real salt"; // can string string hashalgorithm = "sha1"; // can "md5" int passworditerations = 2; // can number string initvector = "@1b2c3d4e5f6g7h8"; // must 16 bytes int keysize = 256; // can 192 or 128 // convert strings byte arrays. // let assume strings contain ascii codes. // if strings include unicode characters, use unicode, utf7, or utf8 // encoding. byte[] initvectorbytes = encoding.ascii.getbytes(initvector); byte[] saltvaluebytes = encoding.ascii.getbytes(saltvalue); // convert our plaintext byte array. // let assume plaintext contains utf8-encoded characters. byte[] plaintextbytes = encoding.utf8.getbytes(plaintext); // first, must create password, key derived. // password generated specified passphrase , // salt value. password created using specified hash // algorithm. password creation can done in several iterations. passwordderivebytes password = new passwordderivebytes( passphrase, saltvaluebytes, hashalgorithm, passworditerations); // use password generate pseudo-random bytes encryption // key. specify size of key in bytes (instead of bits). byte[] keybytes = password.getbytes(keysize / 8); // create uninitialized rijndael encryption object. rijndaelmanaged symmetrickey = new rijndaelmanaged(); // reasonable set encryption mode cipher block chaining // (cbc). use default options other symmetric key parameters. symmetrickey.mode = ciphermode.cbc; // generate encryptor existing key bytes , initialization // vector. key size defined based on number of key // bytes. icryptotransform encryptor = symmetrickey.createencryptor( keybytes, initvectorbytes); // define memory stream used hold encrypted data. memorystream memorystream = new memorystream(); // define cryptographic stream (always use write mode encryption). cryptostream cryptostream = new cryptostream(memorystream, encryptor, cryptostreammode.write); // start encrypting. cryptostream.write(plaintextbytes, 0, plaintextbytes.length); // finish encrypting. cryptostream.flushfinalblock(); // convert our encrypted data memory stream byte array. byte[] ciphertextbytes = memorystream.toarray(); // close both streams. memorystream.close(); cryptostream.close(); // convert encrypted data base64-encoded string. string ciphertext = convert.tobase64string(ciphertextbytes).tostring(); //ciphertext = ciphertext.replace("/", ""); //ciphertext = ciphertext.replace("\\", ""); // return encrypted string. return ciphertext; }
just further bit of information, string trying decode 2 test examples 1008 , 1013, both of these failing during decryption
thanks
i not sure how you're encrypting string, can see you're using byte array of ascii values, going assume of encrypted characters being encrypted weird characters messing returned string, instance ' " ' cause string end, meaning string being returned may not of right length.
maybe show encryption method , can find out sure.
Comments
Post a Comment