AES在CryptoJS中加密并在Coldfusion中解密 [英] AES Encrypt in CryptoJS and decrypt in Coldfusion

查看:341
本文介绍了AES在CryptoJS中加密并在Coldfusion中解密的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们有一个在Coldfusion9中编写的静默登录服务,它接受来自外部系统的加密字符串,然后根据协议的算法/编码设置进行解密。这已经工作没有问题多年来从运行ASP / JAVA / PHP的系统,但我们现在有一个客户端谁别无选择,只能使用CryptoJS执行加密和我的生活我无法解决为什么这不会解密在Coldfusion。



我对加密的知识不是辉煌,但我注意到的事情是CryptoJS加密的密文完全相同的字符串/密钥不同,每次我执行加密,而在Coldfusion / Java我总是可以期望完全相同的加密字符串。我不知道这是否与编码相关,但我从来没有遇到过这个问题从任何其他系统接受加密的字符串,所以我希望这是我在CryptoJS加密的方式是不正确的。

 < cfoutput> 

<!---设置字符串和键--->
< cfset theKey = toBase64(1234567812345678)>
< cfset string =max.brenner@google.com.au>

<!--- CryptoJS AES库 - >
< script src =http://crypto-js.googlecode.com/svn/tags/3.1.2/build/rollups/aes.js>< / script>
< script src =http://crypto-js.googlecode.com/svn/tags/3.1.2/build/components/enc-base64-min.js>< / script>

< script>

//使用CryptoJS AES加密字符串
var encrypted = CryptoJS.AES.encrypt(#string#,#theKey#);
console.log(encrypted.toString());

//使用CryptoJS解密字符串AES
var decryptpted = CryptoJS.AES.decrypt(encrypted,#theKey#);
console.log(decryptpted.toString(CryptoJS.enc.Utf8));

< / script>

<!--- Coldfusion Decrypt String / FAILS --->
解密:#decrypt(encryptedEmail,#theKey#,AES,BASE64)#

< / cfoutput>


解决方案

似乎存在两个问题:


  1. CryptoJS不使用您的变量作为。正如@ Miguel-F所述,当您传递字符串时,将其视为密码并用于导出实际键和IV。两者都是随机生成的,这就是为什么你的加密结果不断变化。但更重要的是,这意味着CryptoJS使用一个完全不同的比您的CF代码中的一个,这就是为什么decrypt()失败。 (至少它是原因的一部分...)


  2. 第二个问题是,除了算法AES,还有两个其他加密必须匹配的设置:模式 padding scheme 。 CryptoJS和ColdFusion使用相同的默认值填充方案,模式是不同的:




设置在两侧相同。尝试在CF中使用CBC模式,因为它比ECB更安全。


$

b $ b

 <!---这是来自CryptoJS的base64加密值 - > 
< cfset encrypted =J2f66oiDpZkFlQu26BDKL6ZwgNwN7T3ixst4JtMyNIY =>
< cfset base64Key =MTIzNDU2NzgxMjM0NTY3OA ==>
< cfset base64IV =EBESExQVFhcYGRobHB0eHw ==>

< cfset ivBytes = binaryDecode(base64IV,base64)>
< cfoutput>
#decrypt(encrypted,base64Key,AES / CBC / PKCS5Padding,base64,ivBytes)#
< / cfoutput>

CryptoJS :(调整原始示例)

 < script src =http://crypto-js.googlecode.com/svn/tags/3.1.2/build/rollups/aes.js >< / script> 
< script src =http://crypto-js.googlecode.com/svn/tags/3.1.2/build/components/enc-base64-min.js>< / script>
< script>
var text =#rawString#;
var key = CryptoJS.enc.Base64.parse(#base64Key#);
var iv = CryptoJS.enc.Base64.parse(#base64IV#);

var encrypted = CryptoJS.AES.encrypt(text,key,{iv:iv});
console.log(encrypted.toString());

var decryptpted = CryptoJS.AES.decrypt(encrypted,key,{iv:iv});
console.log(decryptpted.toString(CryptoJS.enc.Utf8));
< / script>




编辑:



这么说,客户端的意思是别无选择,只能使用CryptoJS执行加密 em>?为什么他们不能使用服务器端加密?我不是加密专家,但在javascript中进行加密,并 在客户端公开密钥 ,听起来不安全...开始...


We've got a Silent Login service written in Coldfusion9 that accepts encrypted strings from external systems and then decrypts based on an agreed Algorithm/Encoding setup. This has worked without issue for years now from systems running ASP/JAVA/PHP, but we now have a client who has no choice but to use CryptoJS to perform the encryption and for the life of me I cannot work out why this won't decrypt in Coldfusion.

My knowledge of encryption isn't brilliant but the thing I am noticing is the CryptoJS encrypted ciphertext for the exact same string/key differs every time i perform the encryption whereas in Coldfusion/Java i can always expect the exact same encrypted string. I'm not sure if this is encoding related or not but i've never run into this issue accepting encrypted strings from any other system before, so I am hoping it's the way I am encrypting in CryptoJS that is incorrect.

<cfoutput>

<!--- Set String and Key --->
<cfset theKey = toBase64("1234567812345678")>
<cfset string = "max.brenner@google.com.au">

<!--- CryptoJS AES Libraries --->
<script src="http://crypto-js.googlecode.com/svn/tags/3.1.2/build/rollups/aes.js"></script>
<script src="http://crypto-js.googlecode.com/svn/tags/3.1.2/build/components/enc-base64-min.js"></script>

<script>

// Encrypt String using CryptoJS AES
var encrypted = CryptoJS.AES.encrypt("#string#", "#theKey#");
console.log(encrypted.toString());

// Decrypt String using CryptoJS AES 
var decrypted = CryptoJS.AES.decrypt(encrypted, "#theKey#");
console.log(decrypted.toString(CryptoJS.enc.Utf8));

</script>

<!--- Coldfusion Decrypt String / FAILS --->
Decrypted: #decrypt(encryptedEmail, "#theKey#", "AES", "BASE64")#

</cfoutput>

解决方案

There seem to be two issues:

  1. CryptoJS is not using your variable as the key. As @Miguel-F mentioned, when you pass in a string, "it's treated as a passphrase and used to derive [the] actual key and IV". Both are randomly generated, which is why your encrypted result keeps changing. But more importantly, this means that CryptoJS is using a completely different key than the one in your CF code and that is why decrypt() fails. (At least it is part of the reason ...)

  2. The second problem is that in addition to the algorithm "AES", there are two other encryption settings which must match: mode and padding scheme. While CryptoJS and ColdFusion use the same defaults for padding scheme, the "modes" are different:

You need to ensure all three settings are the same on both sides. Try using CBC mode in CF, since it is more secure than ECB anyway. Note: It requires adding an IV value.

CF Code:

<!--- this is the base64 encrypted value from CryptoJS ---> 
<cfset encrypted = "J2f66oiDpZkFlQu26BDKL6ZwgNwN7T3ixst4JtMyNIY=">
<cfset rawString = "max.brenner@google.com.au">
<cfset base64Key = "MTIzNDU2NzgxMjM0NTY3OA==">
<cfset base64IV = "EBESExQVFhcYGRobHB0eHw==">

<cfset ivBytes = binaryDecode(base64IV, "base64")>
<cfoutput>
    #decrypt(encrypted, base64Key, "AES/CBC/PKCS5Padding", "base64", ivBytes)#
</cfoutput>

CryptoJS: (Adjusted Original Example)

<script src="http://crypto-js.googlecode.com/svn/tags/3.1.2/build/rollups/aes.js"></script>
<script src="http://crypto-js.googlecode.com/svn/tags/3.1.2/build/components/enc-base64-min.js"></script>
<script>
    var text = "#rawString#";
    var key = CryptoJS.enc.Base64.parse("#base64Key#");
    var iv  = CryptoJS.enc.Base64.parse("#base64IV#");

    var encrypted = CryptoJS.AES.encrypt(text, key, {iv: iv});
    console.log(encrypted.toString());

    var decrypted = CryptoJS.AES.decrypt(encrypted, key, {iv: iv});
    console.log(decrypted.toString(CryptoJS.enc.Utf8));
</script>


Edit:

All that said, what do you mean by the client "has no choice but to use CryptoJS to perform the encryption"? Why cannot they use server side encryption? I am not an encryption expert, but doing encryption in javascript, and exposing the key on the client, does not sound wildly secure to begin with ...

这篇关于AES在CryptoJS中加密并在Coldfusion中解密的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆