What cipher mode/padding defaults or common uses of AES encryption would be used with .NET?

前端 未结 1 1286
终归单人心
终归单人心 2021-01-28 07:40

I\'m helping someone that needs to send an encrypted value with AES to a customer. The customer has not specified the block cipher mode, padding or how to generate the key. They

相关标签:
1条回答
  • There's no constructor for or method in any of the Aes classes in .net that takes in a string as a key. Keys are specified as an array of bytes, such that the length of the array is equal to the key size / 8.

    One would hope that they would use a common hash algorithm to generate the key from the 19 character string. Best bet would probably be a Sha256 hash of the string.

    Just to give you an idea of what that would look like in .net ...

    public void InitializeCrypto()
          {
               var utf8 = new UTF8Encoding();
               var hash = new SHA256CryptoServiceProvider();
               var aes = new AesCryptoServiceProvider();
               var iv = new byte[16];
    
               Array.Clear(iv, 0, iv.Length);
               string key = "Some19CharacterString";
               var utf8Bytes = utf8.GetBytes(key);
               aes.IV = iv;
               aes.KeySize = 256;
               aes.Key = hash.ComputeHash(utf8Bytes);
               //Do crypto work
    
          }
    
    0 讨论(0)
提交回复
热议问题