简体   繁体   English

使用 crypto-js 解密

[英]Decrypting with crypto-js

I receive data from a third party at an API that contains encrypted data.我从包含加密数据的 API 的第三方接收数据。 They provided me with a Passphrase do decrypt the content of the Json file, but I do not get any result;他们为我提供了一个密码来解密 Json 文件的内容,但我没有得到任何结果; so they provided me with the code they generate the encryption which is written in VB.NET:所以他们向我提供了他们生成加密的代码,该代码用 VB.NET 编写:

Public Shared Function EncryptString(ByVal Message As String, ByVal Passphrase As String) As String
        Dim Results As Byte()
        Dim UTF8 As System.Text.UTF8Encoding = New System.Text.UTF8Encoding()
        Dim HashProvider As MD5CryptoServiceProvider = New MD5CryptoServiceProvider()
        Dim TDESKey As Byte() = HashProvider.ComputeHash(UTF8.GetBytes(Passphrase))
        Dim TDESAlgorithm As TripleDESCryptoServiceProvider = New TripleDESCryptoServiceProvider()
        TDESAlgorithm.Key = TDESKey

        TDESAlgorithm.Mode = CipherMode.ECB
        TDESAlgorithm.Padding = PaddingMode.PKCS7
        Dim DataToEncrypt As Byte() = UTF8.GetBytes(Message)
        Dim Encryptor As ICryptoTransform = TDESAlgorithm.CreateEncryptor()
        Results = Encryptor.TransformFinalBlock(DataToEncrypt, 0, DataToEncrypt.Length)
        Return Convert.ToBase64String(Results)
    End Function

I tried to decrypt with:我试图解密:

let key = CryptoJS.enc.Utf8.parse(Passphrase);
let decryptedData = CryptoJS.AES.decrypt(Message, key, {
  iv: key
});
alert(decryptedData.toString( CryptoJS.enc.Utf8 ));

But I get an empty string and this console error: Error: Malformed UTF-8 data但我得到一个空字符串和这个控制台错误:错误:格式错误的 UTF-8 数据

If I do如果我做

const passworddes = CryptoJS.DES.decrypt(message, key, {
      mode: CryptoJS.mode.ECB, padding: CryptoJS.pad.Pkcs7
    }).toString();

I get an empty string only我只得到一个空字符串

Am I missing something on the decryption?我在解密时遗漏了什么吗?

Thank you all.谢谢你们。

UPDATE:更新:

The supplier provided me with the function they utilize to decrypt.供应商向我提供了他们用来解密的 function。 At the above Typescript code Im setting the mode and the padding as they don on their .NET code, but still getting nothing.在上面的 Typescript 代码中,我设置了模式和填充,就像他们在 .NET 代码上一样,但仍然一无所获。

Here is the function they utilize:这是他们使用的 function:

Public Shared Function DecryptString(ByVal Message As String, ByVal Passphrase As String) As String
        Dim Results As Byte()
        Dim UTF8 As System.Text.UTF8Encoding = New System.Text.UTF8Encoding()
        Dim HashProvider As MD5CryptoServiceProvider = New MD5CryptoServiceProvider()
        Dim TDESKey As Byte() = HashProvider.ComputeHash(UTF8.GetBytes(Passphrase))
        Dim TDESAlgorithm As TripleDESCryptoServiceProvider = New TripleDESCryptoServiceProvider()
        TDESAlgorithm.Key = TDESKey
        TDESAlgorithm.Mode = CipherMode.ECB
        TDESAlgorithm.Padding = PaddingMode.PKCS7
        Dim DataToDecrypt As Byte() = Convert.FromBase64String(Message)
        Dim Decryptor As ICryptoTransform = TDESAlgorithm.CreateDecryptor()
        Results = Decryptor.TransformFinalBlock(DataToDecrypt, 0, DataToDecrypt.Length)
        Return UTF8.GetString(Results)
    End Function

The VB code derives the key from the passhprase with MD5. VB 代码使用 MD5 从密码短语派生密钥。 TripleDES (aka 3DES) with a 16 bytes key (2TDEA) is used as the algorithm.具有 16 字节密钥 (2TDEA) 的 TripleDES(又名 3DES)用作算法。 ECB is applied as the mode.采用欧洲央行作为模式。 A possible decryption with CryptoJS is: CryptoJS 的一种可能的解密方式是:

 var key = CryptoJS.MD5('my passphrase'); var message = 'vg0m/29RO6Y9o5SATGFj4H3p612sIIk6/Ny1wtr8HLomM3gI5WYYNKy//pAjq/ZJ' const decrypted = CryptoJS.TripleDES.decrypt( message, key, { mode: CryptoJS.mode.ECB, padding: CryptoJS.pad.Pkcs7 } ).toString(CryptoJS.enc.Utf8); console.log(decrypted)
 <script src="https://cdnjs.cloudflare.com/ajax/libs/crypto-js/4.0.0/crypto-js.min.js"></script>

In the above example the ciphertext was created with the VB code.在上面的示例中,密文是使用 VB 代码创建的。

Note that the code is very insecure:请注意,代码非常不安全:

  • MD5 is broken, better: SHA256. MD5坏了,更好:SHA256。
  • The key derivation from a digest is also insecure (even if the digest is secure), better: a reliable key derivation function like Argon2 or PBKDF2.从摘要派生的密钥也是不安全的(即使摘要是安全的),更好的是:可靠的密钥派生 function 像 Argon2 或 PBKDF2。
  • ECB is insecure, better: CBC or even better: GCM.欧洲央行不安全,更好:CBC,甚至更好:GCM。
  • TripleDES is deprecated and slow, better: AES. TripleDES 已弃用且速度较慢,但更好:AES。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM