简体   繁体   中英

AES decryption in C# uniqUE

I am using the AES to encrypt and decrypt passwords on a website. Anyways; the encrypting works just fine. But I have some problems with the decrypting. On the line:

byte[] decrypted = DecryptStringFromBytes_Aes(encrypted, key, iv);

I recieve this error: Cannot implicity convert type 'string' to 'byte[]'. I have tried lots of things, but nothing seem to work.

You can see the rest of the code below.

string original = txtEncrypt.Text;

        byte[] key = new byte[] { 3,122,23,189,15,2,55,82,97,17,255,45,1,65,41,200 };

        byte[] iv = new byte[16];
        Aes myAes = Aes.Create();

        byte[] encrypted = EncryptStringToBytes_Aes(original, key, iv);
        byte[] decrypted = DecryptStringFromBytes_Aes(encrypted, key, iv);

Sincerely, Adrian

The sample code that you used returns the ciphertext as byte array. Modern ciphers, like AES in CBC mode as you're using, operate on bytes, not strings.

So if you need a string then you need to convert to a string and then back again. For this you could use an encoding such as base 64 encoding. So encode to base 64 after encryption and then decode before decryption.

If you just directly interpret the bytes as a string (eg UTF-8) then you will experience data loss as not every byte is a valid / printable UTF-8 character.

Don't forget to include all required information that needs to be shared, such as the IV. The example code conveniently forgets about that.

Note that CBC is not secure for transport mode security; only use for data at rest.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM