我需要使用公钥使用PHP对消息进行编码,并使用私钥解析此消息。
这是测试代码:
var memberList = {member: [1, 2, 3, ...]}
但这不起作用。为什么呢?
答案 0 :(得分:2)
CryptoJS解密需要CipherParams
个对象代替密文,而WordArray
代替密钥。如果您将密钥作为字符串传递,那么它将假定它是一个密码并尝试从MD5和不存在的盐中获取密钥。
尝试解析所有内容:
var decrypted = CryptoJS.RC4.decrypt({
ciphertext: CryptoJS.enc.Base64.parse(encrypted)
}, CryptoJS.enc.Latin1.parse(theKey));
JSEncrypt返回"普通字符串"来自crypt.decrypt()
函数,这意味着它必须用Latin1或UTF-8编码解析。
但我无法使用JSEncrypt,所以我使用了forge代替:
var privateKey = forge.pki.privateKeyFromPem(private_key);
var theKey = privateKey.decrypt(forge.util.decode64(envelope), 'RSAES-PKCS1-V1_5');
CrypoJS' .toString()
默认返回十六进制编码版本。您可以覆盖它以获取文本数据:
console.log(decrypted.toString(CryptoJS.enc.Utf8));
var private_key = `-----BEGIN RSA PRIVATE KEY-----
MIIEpAIBAAKCAQEAx1u1OPc+KT1vmLea4ro0VtdiQa9Pj7oFMF04uIPKb5BEuN6w
rHj3dQkdoeT3vWZaR/eAOnxSIgl4PwNm9C6AeW5vHAq/4XFp4FZbtbHfqCm3h6tT
m8FxmuTZQVr+Kgrkgiob/C5+bWHhD+rfUnaW8CGnvmhHD4AtxCVimMUhYLCSklkX
YOWng9aRO5kAvDx0KXyZ60YsJJwUouPat/vBdoHljRZhzOb7KHSek4yd//0zBmBW
3y9z+KG8WrpGEoCutGE14QBGh/EghRP9QAhfGV4a9kfD77S0Spu7uzmfzBuyyit5
Dhtcact4SJuJz2617I1gujGe51j5Oyex70hoEQIDAQABAoIBACJl5IMLxiH/g73q
EEOGtXk2eLG0uhmmkzahHyRXORq+3dy4L+vSg4AiLoa8pzJ68qDwR6PPx6wvzhux
TpZbX7eUIn/IKwuPMiGPRQu4wUqWyCAUo7yASEXdrs7kEG7Yqkz7TxDYROBCvz6r
3BSyeaC9beAgxg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mrDB7qj8/Nw==
-----END RSA PRIVATE KEY-----`;
var encrypted = 'E+moZJOeVuxgsg=='; var envelope = 'LMJqdAu+FVPxVpVkLgBOrqLWkY/S/56KHETnt528k2D0lUDzl7nymsgycrto5BD4w17oH6THyC+0yg/Ws+gqpIIWimcQze1KKQy+1+uvH4gR1Olt22cgEUDid09dDqvSXi0VcYYYQXyxWzIQVd2Jun1A0h1qhWc4LinzkJSP6HuhH+D52eEcXep5H4cjjy62rgGIR5YyQO/RBXuYYBBRSgmeniFjY9FbR8AaBkhcCVYeBH28uBi0dx27hPQ0yVlVWrG3KuR2i441Ruhb4J+AOnr+K1uIMT+rnk8Th8eTTc/E1CSrZo9GASfEyYK8mJdwfq2/FJ5Qnp9uHl1aN/1RSg==';
var privateKey = forge.pki.privateKeyFromPem(private_key);
var theKey = privateKey.decrypt(forge.util.decode64(envelope), 'RSAES-PKCS1-V1_5');
var decrypted = CryptoJS.RC4.decrypt({
ciphertext: CryptoJS.enc.Base64.parse(encrypted)
}, CryptoJS.enc.Latin1.parse(theKey));
console.log(decrypted.toString(CryptoJS.enc.Utf8));

<script type="text/javascript" src="https://cdn.rawgit.com/CryptoStore/crypto-js/3.1.2/build/rollups/rc4.js"></script>
<script type="text/javascript" src="https://cdn.rawgit.com/CryptoStore/crypto-js/3.1.2/build/components/enc-base64-min.js"></script>
<script type="text/javascript" src="https://rawgit.com/artjomb/96b970358e20410fa64daa2e844aeb0f/raw/5375e7171ef297d436d65b962149dcc0e1960b2b/forge_v0.6.39.min.js"></script>
&#13;