1

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

Adrian
  • 57
  • 6
  • 3
    Never use AES to encrypt password. You should use one way cryptographic hashing function or password based key derivation function. – Justin Lessard Apr 15 '19 at 17:40
  • Why should I not use it? @JustinLessard – Adrian Apr 15 '19 at 17:43
  • See these question for more details [Is AES encrypting a password with itself more secure than SHA1?](https://security.stackexchange.com/questions/10476/is-aes-encrypting-a-password-with-itself-more-secure-than-sha1) [Why not use AES for password encryption in PHP?](https://stackoverflow.com/questions/3144283/why-not-use-aes-for-password-encryption-in-php) – Justin Lessard Apr 15 '19 at 17:44
  • What would you advice me to use then? @JustinLessard – Adrian Apr 15 '19 at 17:53
  • 1
    [PBKDF2 is included in the .net framework](https://learn.microsoft.com/en-us/aspnet/core/security/data-protection/consumer-apis/password-hashing) and is secure enough. If you're looking for something harder to brute-force, [argon2 is recommended since 2015](https://password-hashing.net/). Bcrypt and scrypt are also widely used in the community. – Justin Lessard Apr 15 '19 at 18:03
  • It's *literally telling you the problem in the error message*: the method accepts a `byte` array, you are passing a `string` to it. – Ian Kemp Apr 16 '19 at 12:22
  • I ended up using SHA256 which is also a hash function, thanks for the help @JustinLessard! :) – Adrian Apr 17 '19 at 11:35

1 Answers1

1

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 (e.g. 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.

Maarten Bodewes
  • 90,524
  • 13
  • 150
  • 263
  • 1
    Removed some snarky remark about the quality of Microsoft sample code. I'll generify: don't use sample code that is not well explained. – Maarten Bodewes Apr 16 '19 at 10:22