以文本方式查看主题

-  Foxtable(狐表)  (http://foxtable.net/bbs/index.asp)
--  专家坐堂  (http://foxtable.net/bbs/list.asp?boardid=2)
----  [求助]16进制字符串转换为字符数组用什么函数或代码处理?  (http://foxtable.net/bbs/dispbbs.asp?boardid=2&id=179953)

--  作者:sxfeiliu
--  发布时间:2022/9/20 12:58:00
--  [求助]16进制字符串转换为字符数组用什么函数或代码处理?
JS加解密函数CryptoJS.enc.Hex.parse(word)在foxtable中,用什么函数或代码替代呢?

要实现加解密功能的JS代码如下:
//aes加密
function encrypt(word) {
    var key = CryptoJS.enc.Utf8.parse("1234567890000000"); //16位
    var iv = CryptoJS.enc.Utf8.parse("1234567890000000");
    var encrypted = \'\';
    if (typeof(word) == \'string\') {
        var srcs = CryptoJS.enc.Utf8.parse(word);
        encrypted = CryptoJS.AES.encrypt(srcs, key, {
            iv: iv,
            mode: CryptoJS.mode.CBC,
            padding: CryptoJS.pad.Pkcs7
        });
    } else if (typeof(word) == \'object\') {//对象格式的转成json字符串
        data = JSON.stringify(word);
        var srcs = CryptoJS.enc.Utf8.parse(data);
        encrypted = CryptoJS.AES.encrypt(srcs, key, {
            iv: iv,
            mode: CryptoJS.mode.CBC,
            padding: CryptoJS.pad.Pkcs7
        })
    }
    return encrypted.ciphertext.toString();
}
// aes解密
function decrypt(word) {
    var key = CryptoJS.enc.Utf8.parse("1234567890000000"); 
    var iv = CryptoJS.enc.Utf8.parse("1234567890000000");
    var encryptedHexStr = CryptoJS.enc.Hex.parse(word);
    var srcs = CryptoJS.enc.Base64.stringify(encryptedHexStr);
    var decrypt = CryptoJS.AES.decrypt(srcs, key, {
        iv: iv,
        mode: CryptoJS.mode.CBC,
        padding: CryptoJS.pad.Pkcs7
    });
    var decryptedStr = decrypt.toString(CryptoJS.enc.Utf8);
    return decryptedStr.toString();
}


待完善代码:

system.security.cryptography 命名空间SSC

 全局代码:

 Const key As String ="XXXXXXXXXXXXXXXX"

Const iv As String = "XXXXXXXXXXXXXXXX"

Public Function AesEncrypt(ByVal toEncrypt As String) As String
 Dim keyArray As Byte() = UTF8Encoding.UTF8.GetBytes(key)
 Dim ivArray As Byte() = UTF8Encoding.UTF8.GetBytes(iv)
 Dim toEncryptArray As Byte() = UTF8Encoding.UTF8.GetBytes(toEncrypt)
 Dim rDel As SSC.RijndaelManaged = New SSC.RijndaelManaged()
 rDel.Key = keyArray
 rDel.IV = ivArray
 rDel.Mode = SSC.CipherMode.CBC
 rDel.Padding = SSC.PaddingMode.Zeros
 Dim cTransform As SSC.ICryptoTransform = rDel.CreateEncryptor()
 Dim resultArray As Byte() = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length)
 Return Convert.ToBase64String(resultArray, 0, resultArray.Length)
End Function

Public Function AesDecrypt(ByVal toDecrypt As String) As String
 Dim keyArray As Byte() = UTF8Encoding.UTF8.GetBytes(key)
 Dim ivArray As Byte() = UTF8Encoding.UTF8.GetBytes(iv)

 Dim toEncryptArray As Byte() = Convert.FromBase64String(toDecrypt)
 Dim rDel As SSC.RijndaelManaged = New SSC.RijndaelManaged()
 rDel.Key = keyArray
 rDel.IV = ivArray
 rDel.Mode = SSC.CipherMode.CBC
 rDel.Padding = SSC.PaddingMode.Zeros
 Dim cTransform As SSC.ICryptoTransform = rDel.CreateDecryptor()
 Dim resultArray As Byte() = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length)
 Return UTF8Encoding.UTF8.GetString(resultArray)
End Function


--  作者:有点蓝
--  发布时间:2022/9/20 13:36:00
--  
什么地方有什么问题?
--  作者:sxfeiliu
--  发布时间:2022/9/20 13:47:00
--  
JS中的解密代码:var encryptedHexStr = CryptoJS.enc.Hex.parse(word);
在foxtable中应转换为什么代码?
目前无法正确解密JS加密的数据。

--  作者:有点蓝
--  发布时间:2022/9/20 14:07:00
--  
参考:http://www.foxtable.com/bbs/dispbbs.asp?BoardID=2&ID=160819&skin=0
--  作者:sxfeiliu
--  发布时间:2022/9/20 20:29:00
--  
终于搞定了,感谢!