doutan2111 2014-06-07 11:52
浏览 113

在javascript中解密PHP aes加密

I've been programming for years now, but never had to do anything with encryption/decryption.

So I've got the following PHP:

function base64_url_decode($input) {
 return base64_decode(strtr($input, '-_,', '+/='));
}

function AESdecrypt($phrase, $user_key){
    $key = pack('H*', $user_key);

    $ciphertext_dec = base64_url_decode($phrase);

    # may remove 00h valued characters from end of plain text
    $plaintext_dec = mcrypt_decrypt(MCRYPT_RIJNDAEL_128, $key,
                                    $ciphertext_dec, MCRYPT_MODE_ECB);

    return $plaintext_dec;
}

echo AESdecrypt(
    "-GZmiQhJYnzw0FXTR6QoLryPNlgcScbfgZnmzgA35tydozNwsKWdXvIEtqWjhJIGCYdVVKB0lBKUTx-TXxHnIQn680mZIZ8lG7HNTMgprxM,",
    "2dd9bb29d2e25c18bdc12d7b75f6f5d0ef3d99ef310a0319e2796bb30278b24c557f78b6c958faa55d70ce081f2607a0e62b9fa01e2483f9a75b032b7fd9678c"
);

The output of this is

1kYJjUajo8bIlsT5CVCSqYglD_dQX-fjuIHkEFzfJouiI7Nx29IEtZ8QwTvIH6yx5uI,

I've created a JS fiddle to simulate what I have in javascript: http://jsfiddle.net/NdT3P/3/ (please scroll down to line 545 since I've pasted in some libraries I was using).

Relevant Javascript code:

var user_key = "2dd9bb29d2e25c18bdc12d7b75f6f5d0ef3d99ef310a0319e2796bb30278b24c557f78b6c958faa55d70ce081f2607a0e62b9fa01e2483f9a75b032b7fd9678c";

user_key = pack('H*', user_key);

console.log(user_key); // same as the php functions

var decrypted_wallet = "-GZmiQhJYnzw0FXTR6QoLryPNlgcScbfgZnmzgA35tydozNwsKWdXvIEtqWjhJIGCYdVVKB0lBKUTx-TXxHnIQn680mZIZ8lG7HNTMgprxM,"

decrypted_wallet = Base64.decode(strtr(decrypted_wallet, '-_,', '+/='));
console.log(decrypted_wallet); //same as the php function

var decrypted = CryptoJS.AES.decrypt(user_key,decrypted_wallet,{ mode: CryptoJS.mode.ECB });

console.log(decrypted.toString(CryptoJS.enc.Utf8)); // empty string

Anyone could point in the right direction?

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥30 win from 窗口最大最小化,控件放大缩小,闪烁问题
    • ¥20 易康econgnition精度验证
    • ¥15 msix packaging tool打包问题
    • ¥28 微信小程序开发页面布局没问题,真机调试的时候页面布局就乱了
    • ¥15 python的qt5界面
    • ¥15 无线电能传输系统MATLAB仿真问题
    • ¥50 如何用脚本实现输入法的热键设置
    • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
    • ¥30 深度学习,前后端连接
    • ¥15 孟德尔随机化结果不一致