F
FacilisDK
Guest
I have a string that is encrypted by a UWP app using the following code:
Public Function Encrypt(plainText As String, pw As String) As String
Dim returnData As String = ""
Try
Dim pwBuffer As Windows.Storage.Streams.IBuffer = CryptographicBuffer.ConvertStringToBinary(pw, BinaryStringEncoding.Utf8)
Dim saltBuffer As Windows.Storage.Streams.IBuffer = CryptographicBuffer.CreateFromByteArray(New Byte() {0, 1, 2, 28, 29, 30, 3, 4, 5, 15, 32, 33, 173, 175, 164}) ' ConvertStringToBinary(salt, BinaryStringEncoding.Utf16LE)
Dim plainBuffer As Windows.Storage.Streams.IBuffer = CryptographicBuffer.ConvertStringToBinary(plainText, BinaryStringEncoding.Utf16LE)
Dim keyDerivationProvider As Core.KeyDerivationAlgorithmProvider = Windows.Security.Cryptography.Core.KeyDerivationAlgorithmProvider.OpenAlgorithm("PBKDF2_SHA1")
Dim pbkdf2Parms As Core.KeyDerivationParameters = Core.KeyDerivationParameters.BuildForPbkdf2(saltBuffer, 768)
Dim keyOriginal As Core.CryptographicKey = keyDerivationProvider.CreateKey(pwBuffer)
Dim keyMaterial As Windows.Storage.Streams.IBuffer = Core.CryptographicEngine.DeriveKeyMaterial(keyOriginal, pbkdf2Parms, 32)
Dim derivedPwKey As Core.CryptographicKey = keyDerivationProvider.CreateKey(pwBuffer)
Dim saltMaterial As Windows.Storage.Streams.IBuffer = Core.CryptographicEngine.DeriveKeyMaterial(derivedPwKey, pbkdf2Parms, 16)
Dim keyMaterialString As String = CryptographicBuffer.EncodeToBase64String(keyMaterial)
Dim saltMaterialString As String = CryptographicBuffer.EncodeToBase64String(saltMaterial)
Dim symProvider As Core.SymmetricKeyAlgorithmProvider = Core.SymmetricKeyAlgorithmProvider.OpenAlgorithm("AES_CBC_PKCS7")
Dim symmKey As Core.CryptographicKey = symProvider.CreateSymmetricKey(keyMaterial)
Dim resultBuffer As Windows.Storage.Streams.IBuffer = Core.CryptographicEngine.Encrypt(symmKey, plainBuffer, saltMaterial)
End Try
Return returnData
End Function
I then decrypt it a Windows Forms desktop application using the following code:
Public Function Decrypt(ByVal Data As String, ByVal Password As String) As String
Dim cipherBytes As Byte() = Convert.FromBase64String(Data)
Dim pdb As New Rfc2898DeriveBytes(Password, New Byte() {0, 1, 2, 28, 29, 30, 3, 4, 5, 15, 32, 33, 173, 175, 164}, 768)
Dim decryptedData As Byte() = clsDecrypt(cipherBytes, pdb.GetBytes(32), pdb.GetBytes(16))
Return Text.Encoding.Unicode.GetString(decryptedData)
End Function
Private Function clsDecrypt(ByVal cipherData As Byte(), ByVal Key As Byte(), ByVal IV As Byte()) As Byte()
Dim ms As New IO.MemoryStream()
Dim alg As Rijndael = Rijndael.Create()
alg.Key = Key
alg.IV = IV
Dim cs As New CryptoStream(ms, alg.CreateDecryptor(), CryptoStreamMode.Write)
cs.Write(cipherData, 0, cipherData.Length)
cs.Close()
Dim decryptedData As Byte() = ms.ToArray()
Return decryptedData
End Function
No errors are raised, and the string are correctly decoded except the beginning.
The first few characters in the decrypted string returns as Chinese-looking characters :/
Can anyone in here see where it goes wrong for me?
Continue reading...
Public Function Encrypt(plainText As String, pw As String) As String
Dim returnData As String = ""
Try
Dim pwBuffer As Windows.Storage.Streams.IBuffer = CryptographicBuffer.ConvertStringToBinary(pw, BinaryStringEncoding.Utf8)
Dim saltBuffer As Windows.Storage.Streams.IBuffer = CryptographicBuffer.CreateFromByteArray(New Byte() {0, 1, 2, 28, 29, 30, 3, 4, 5, 15, 32, 33, 173, 175, 164}) ' ConvertStringToBinary(salt, BinaryStringEncoding.Utf16LE)
Dim plainBuffer As Windows.Storage.Streams.IBuffer = CryptographicBuffer.ConvertStringToBinary(plainText, BinaryStringEncoding.Utf16LE)
Dim keyDerivationProvider As Core.KeyDerivationAlgorithmProvider = Windows.Security.Cryptography.Core.KeyDerivationAlgorithmProvider.OpenAlgorithm("PBKDF2_SHA1")
Dim pbkdf2Parms As Core.KeyDerivationParameters = Core.KeyDerivationParameters.BuildForPbkdf2(saltBuffer, 768)
Dim keyOriginal As Core.CryptographicKey = keyDerivationProvider.CreateKey(pwBuffer)
Dim keyMaterial As Windows.Storage.Streams.IBuffer = Core.CryptographicEngine.DeriveKeyMaterial(keyOriginal, pbkdf2Parms, 32)
Dim derivedPwKey As Core.CryptographicKey = keyDerivationProvider.CreateKey(pwBuffer)
Dim saltMaterial As Windows.Storage.Streams.IBuffer = Core.CryptographicEngine.DeriveKeyMaterial(derivedPwKey, pbkdf2Parms, 16)
Dim keyMaterialString As String = CryptographicBuffer.EncodeToBase64String(keyMaterial)
Dim saltMaterialString As String = CryptographicBuffer.EncodeToBase64String(saltMaterial)
Dim symProvider As Core.SymmetricKeyAlgorithmProvider = Core.SymmetricKeyAlgorithmProvider.OpenAlgorithm("AES_CBC_PKCS7")
Dim symmKey As Core.CryptographicKey = symProvider.CreateSymmetricKey(keyMaterial)
Dim resultBuffer As Windows.Storage.Streams.IBuffer = Core.CryptographicEngine.Encrypt(symmKey, plainBuffer, saltMaterial)
End Try
Return returnData
End Function
I then decrypt it a Windows Forms desktop application using the following code:
Public Function Decrypt(ByVal Data As String, ByVal Password As String) As String
Dim cipherBytes As Byte() = Convert.FromBase64String(Data)
Dim pdb As New Rfc2898DeriveBytes(Password, New Byte() {0, 1, 2, 28, 29, 30, 3, 4, 5, 15, 32, 33, 173, 175, 164}, 768)
Dim decryptedData As Byte() = clsDecrypt(cipherBytes, pdb.GetBytes(32), pdb.GetBytes(16))
Return Text.Encoding.Unicode.GetString(decryptedData)
End Function
Private Function clsDecrypt(ByVal cipherData As Byte(), ByVal Key As Byte(), ByVal IV As Byte()) As Byte()
Dim ms As New IO.MemoryStream()
Dim alg As Rijndael = Rijndael.Create()
alg.Key = Key
alg.IV = IV
Dim cs As New CryptoStream(ms, alg.CreateDecryptor(), CryptoStreamMode.Write)
cs.Write(cipherData, 0, cipherData.Length)
cs.Close()
Dim decryptedData As Byte() = ms.ToArray()
Return decryptedData
End Function
No errors are raised, and the string are correctly decoded except the beginning.
The first few characters in the decrypted string returns as Chinese-looking characters :/
Can anyone in here see where it goes wrong for me?
Continue reading...