如何在Javascript或Jquery中执行此操作?
请通过两个步骤建议:
1.-字阵列到单字节数组.
2.-字节数组到字符串.
也许这可以帮助:
function hex2a(hex) {
var str = '';
for (var i = 0; i < hex.length; i += 2)
str += String.fromCharCode(parseInt(hex.substr(i, 2), 16));
return str;
}
Run Code Online (Sandbox Code Playgroud) 我们有一个用Coldfusion9编写的静默登录服务,该服务接受来自外部系统的加密字符串,然后根据约定的算法/编码设置进行解密.多年来,从运行ASP/JAVA/PHP的系统开始,这一点没有问题,但我们现在有一个客户别无选择,只能使用CryptoJS来执行加密,而对于我的生活,我无法理解为什么这不会在Coldfusion中解密.
我的加密知识并不精彩,但我注意到的是CryptoJS加密的密文,完全相同的字符串/密钥每次执行加密时都不同,而在Coldfusion/Java中,我总能期望完全相同的加密字符串.我不确定这是否与编码相关,但我从来没有遇到过这个问题,之前接受来自任何其他系统的加密字符串,所以我希望这是我在CryptoJS中加密的方式不正确.
<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>
Run Code Online (Sandbox Code Playgroud) 所以,我在ruby上有加密和解密方法,它们工作得很好.我按照这个问题的答案(如何使用CryptoJS AES解密消息.我有一个有效的Ruby示例),但它返回一个空字符串.
Ruby代码
def load_vars
@key = "2e35f242a46d67eeb74aabc37d5e5d05"
@algorithm = "aes-128-cbc"
end
def encryption(data)
begin
key = @key
aes = OpenSSL::Cipher.new(@algorithm)
aes.encrypt()
aes.key = key
iv_value = aes.random_iv
aes.iv = iv_value
crypt = aes.update(data) + aes.final()
crypt_string = (Base64.encode64(iv_value + crypt))
return crypt_string
end
end
def decryption(data)
begin
key = @key
aes = OpenSSL::Cipher.new(@algorithm)
iv_value = Base64.decode64(data)[0...16]
data_value = Base64.decode64(data)[16..-1]
aes.decrypt
aes.key = @key
aes.iv = iv_value
results = aes.update(data_value) + aes.final
return results
end
end …Run Code Online (Sandbox Code Playgroud) 我的代码可以进行调试,但是当我尝试运行时出现此错误。为什么会发生这种情况。
var CryptoJS = require("crypto-js");
require('dotenv').config();
getText = process.env.USER;
var bytes = CryptoJS.AES.decrypt(getText, 'secret key 123');
var originalText = bytes.toString(CryptoJS.enc.Utf8);
console.log(originalText);
Run Code Online (Sandbox Code Playgroud)
当我尝试从 .env 读取时出现此错误。
cryptojs ×3
javascript ×3
aes ×2
arrays ×1
coldfusion ×1
cryptography ×1
encryption ×1
jquery ×1
node.js ×1
string ×1