gpt4 book ai didi

java - vbscript 上的 aes/rijndael 自定义 key

转载 作者:行者123 更新时间:2023-12-01 19:26:03 25 4
gpt4 key购买 nike

我正在开发一个 JAVA 程序,它向 asp 文件发出请求,并且工作正常。

现在我想加密POST请求,但我需要在JAVA和ASP上使用相同的加密 key 才能正确解密(或者这就是我的想法)。

如何从 vbscript 上的字符串生成 key ?我需要定制 IV 吗?我不知道那是什么:(

(我从未加密过任何东西,我对此很陌生)

set obj = server.CreateObject("System.Security.Cryptography.RijndaelManaged")
set utf = CreateObject("System.Text.UTF8Encoding")
s="This is a private message"
bytes=utf.GetBytes_4(s)
obj.GenerateKey() 'need to custom this
obj.GenerateIV() 'need to custom this?
set enc=obj.CreateEncryptor()
set dec=obj.CreateDecryptor()

bytec=enc.TransformFinalBlock((bytes),0,lenb(bytes))
sc=utf.GetString((bytec))
response.write sc

byted=dec.TransformFinalBlock((bytec),0,lenb(bytec))
sd=utf.GetString((byted))
response.write sd

最佳答案

经过几天的调查,我将发布我最终所做的事情。我知道这不是一个好的做法,我很快就会改变它,但现在还可以。

ASP 代码(解密 java 请求)

response.write desencriptar(encryptedStringHere)

Function desencriptar(str)
set obj= CreateObject("System.Security.Cryptography.RijndaelManaged")
set utf8 = CreateObject("System.Text.UTF8Encoding")
ekey = "16byteskeyhere" 'you'll need to change this
eiv = "16bytesIVhere" 'you'll need to change this
obj.BlockSize = 128
obj.Key = utf8.GetBytes_4(ekey)
obj.IV = utf8.GetBytes_4(eiv)
obj.Padding = 5
set decryptor=obj.CreateDecryptor()
desencriptado = Base64ToByte(str)
byted=decryptor.TransformFinalBlock((desencriptado),0,lenb(desencriptado))
desencriptado = utf8.getString((byted))
desencriptar = desencriptado
end function

Function Base64ToByte(ByVal vCode)
Dim oXML, oNode
Set oXML = CreateObject("Msxml2.DOMDocument.3.0")
Set oNode = oXML.CreateElement("base64")
oNode.dataType = "bin.base64"
oNode.text = vCode
Base64ToByte = oNode.nodeTypedValue
Set oNode = Nothing
Set oXML = Nothing
End Function

JAVA 代码(加密函数)

public static String aesEncrypt(String value, String key, String initVector) {
try {
IvParameterSpec iv = new IvParameterSpec(initVector.getBytes("UTF-8"));
SecretKeySpec skeySpec = new SecretKeySpec(key.getBytes("UTF-8"), "Rijndael");
Cipher cipher = Cipher.getInstance("Rijndael/CBC/PKCS5PADDING");
cipher.init(Cipher.ENCRYPT_MODE, skeySpec, iv);
byte[] encrypted = cipher.doFinal(value.getBytes());
return Base64.getEncoder().encodeToString(encrypted);
} catch (Exception ex) {
ex.printStackTrace();
}
return null;
}

Docs: RijndaelManaged | PaddingMode | CipherMode

关于java - vbscript 上的 aes/rijndael 自定义 key ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45501620/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com