locked
Problem decrypting string. Encrypted by UWP app, decrypted by WinForms application RRS feed

  • Question

  • 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?


    • Edited by FacilisDK Monday, April 8, 2019 9:55 PM Purtifying text.
    Monday, April 8, 2019 9:54 PM

Answers

  • The Initialisation Vector used to encrypt does not match the Initialisation Vector used to decrypt. That results in the corruption of the first 16 bytes of the decrypted data, and so the first 8 UTF-16 characters produced will be wrong.

    In the UWP app, the IV is formed from the first 16 bytes that are returned by the key derivation algorithm (and match the first 16 bytes used for the encryption key, which probably isn't a good idea).

    In the Desktop app, the IV is formed from bytes at offsets 32 through 47 as returned by the key derivation algorithm.

    • Marked as answer by FacilisDK Wednesday, April 10, 2019 9:10 AM
    Wednesday, April 10, 2019 8:42 AM

All replies

  • Hello,

    Is there any chance you can encrypt with a Windows form app and then decrypt in a Windows Form app and see if you get the same bad results?

    Also, are both operations performed on one machine?


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Monday, April 8, 2019 11:46 PM
  • Not possible to encrypt with a Windows form app, since the method that originally encrypted the string only runs on the UWP framework, not Windows Forms.

    I tried running both operations on the same machine and the outcome are the same.
    The first 8 characters always returns: ´꓌嘗丩ቸ轍쯶

    Tuesday, April 9, 2019 6:28 AM
  • Hi,

    try to fix the code:

     Return Text.Encoding.Unicode.GetString(decryptedData)

    to

     Return Text.Encoding.GetEncoding ("gb2312").GetString(decryptedData)

    Best Regards,

    Alex


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, April 10, 2019 8:40 AM
  • The Initialisation Vector used to encrypt does not match the Initialisation Vector used to decrypt. That results in the corruption of the first 16 bytes of the decrypted data, and so the first 8 UTF-16 characters produced will be wrong.

    In the UWP app, the IV is formed from the first 16 bytes that are returned by the key derivation algorithm (and match the first 16 bytes used for the encryption key, which probably isn't a good idea).

    In the Desktop app, the IV is formed from bytes at offsets 32 through 47 as returned by the key derivation algorithm.

    • Marked as answer by FacilisDK Wednesday, April 10, 2019 9:10 AM
    Wednesday, April 10, 2019 8:42 AM
  • Staring at the same code for too long must have strained my eyes, hehe.
    Thank you for your reply, everything Works perfectly now.
    Wednesday, April 10, 2019 1:26 PM
  • The Initialisation Vector used to encrypt does not match the Initialisation Vector used to decrypt. That results in the corruption of the first 16 bytes of the decrypted data, and so the first 8 UTF-16 characters produced will be wrong.

    In the UWP app, the IV is formed from the first 16 bytes that are returned by the key derivation algorithm (and match the first 16 bytes used for the encryption key, which probably isn't a good idea).

    In the Desktop app, the IV is formed from bytes at offsets 32 through 47 as returned by the key derivation algorithm.

    Just wanted to thank you for providing a quality response :-)

    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Wednesday, April 10, 2019 1:32 PM
  • Thank you for your reply, everything Works perfectly now.

    @FacilisDK: You are welcome. Glad it's working.

    Just wanted to thank you for providing a quality response :-)

    @Karen: And I thank you for your kind words, and taking the time to post them :-)

    Thursday, April 11, 2019 4:25 PM