Cross-Platform Encryption / Decryption - Key and Initialization Vector (IV) Management

After looking at a few examples, I put together several encryption / decryption methods that use Rfc2898DeriveBytes to get the key and initialization vector. My concern is that the party receiving my encrypted content should be able to decrypt it. Since I have no control over what language they use (can be Java, PHP, C, etc.), How can I guarantee that they can get the Key vector and Initialization Vector (IV), since I use the Rfc2898DeriveBytes class in .NET? Here are the encryption and decryption methods that I use.

 Public Shared Function EncryptText(ByVal plainText As String, ByVal password As String) As String Dim aesCrypto As Rijndael = Nothing Dim plainTextBytes As Byte() plainTextBytes = Encoding.Default.GetBytes(plainText) Dim rfc2898 As Rfc2898DeriveBytes rfc2898 = New Rfc2898DeriveBytes(password, GenerateSalt(password)) aesCrypto = Rijndael.Create() aesCrypto.Padding = PaddingMode.ISO10126 Dim tx As ICryptoTransform tx = aesCrypto.CreateEncryptor(rfc2898.GetBytes(32), rfc2898.GetBytes(16)) Dim encryptedBytes As Byte() encryptedBytes = tx.TransformFinalBlock(plainTextBytes, 0, plainTextBytes.Length) Return Convert.ToBase64String(encryptedBytes) End Function Public Shared Function DecryptText(ByVal encryptedText As String, ByVal password As String) As String Dim aesCrypto As Rijndael = Nothing Dim encryptedTextBytes As Byte() encryptedTextBytes = Convert.FromBase64String(encryptedText) Dim rfc2898 As Rfc2898DeriveBytes rfc2898 = New Rfc2898DeriveBytes(password, GenerateSalt(password)) aesCrypto = Rijndael.Create() aesCrypto.Padding = PaddingMode.ISO10126 Dim tx As ICryptoTransform tx = aesCrypto.CreateEncryptor(rfc2898.GetBytes(32), rfc2898.GetBytes(16)) Dim decryptedBytes As Byte() decryptedBytes = tx.TransformFinalBlock(encryptedTextBytes, 0, encryptedTextBytes.Length) Return Encoding.Default.GetString(decryptedBytes) End Function 
+4
source share
1 answer

You would tell the recipient to implement PBKDF2, which is the standard defined in RFC2898 and PKCS # 5 . Microsoft's documentation says their function uses HMAC-SHA-1 as a pseudo-random function, and 1000 as the default number of iterations. This is the information they need.

However, you will also need to pass the salt created using GenerateSalt() on the sending side. The receiver cannot simply call GenerateSalt() its own - it must be randomly generated for each message.

+4
source

Source: https://habr.com/ru/post/1345627/


All Articles