CryptoJS AES加密和Java AES解密 [英] CryptoJS AES encryption and Java AES decryption

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

问题描述

我只是问这个,因为我已经读了很多帖子2天现在关于crypto AES加密,只是当我想我得到它,我意识到我没有得到它。

I'm only asking this because I have read many posts for 2 days now about crypto AES encryption, and just when I thought I was getting it, I realized I wasn't getting it at all.

这个帖子是我最接近的问题,我有完全相同的问题,但它是未回答:

This post is the closest one to my issue, I have exactly the same problem but it is unanswered:

CryptoJS AES加密和JAVA AES解密值不匹配

我尝试过很多方法,但我没有弄错。

I have tried doing it in many ways but I haven't gotten it right.

首先

我得到已经加密的字符串(我只有代码看看他们是怎么做的)加密方式不是一个选项。

第二

我可以访问密钥,我可以修改它(因此,如果必要,调整长度是一个选项。)

加密是在CryptoJS上完成的,它们将加密的字符串作为GET参数发送。

The encryption is done on CryptoJS and they send the encrypted string as a GET parameter.

GetParamsForAppUrl.prototype.generateUrlParams = function() {
const self = this;
 return new Promise((resolve, reject) => {
   const currentDateInMilliseconds = new Date().getTime();
   const secret = tokenSecret.secret;
   var encrypted = CryptoJS.AES.encrypt(self.authorization, secret);
   encrypted = encrypted.toString();
   self.urlParams = {
     token: encrypted,
     time: currentDateInMilliseconds
   };
   resolve();
 });
};

我可以使用CryptoJS在javascript上轻松解密:

I can easily decrypt this on javascript using CryptoJS with:

var decrypted = CryptoJS.AES.decrypt(encrypted_string, secret);
    console.log(decrypted.toString(CryptoJS.enc.Utf8)); 

但是我不想在Javascript上这样做,出于安全原因,在Java上对此解密:

But I don't want to do this on Javascript, for security reasons, so I'm trying to decrypt this on Java:

String secret = "secret";
byte[] cipherText = encrypted_string.getBytes("UTF8");
SecretKey secKey = new SecretKeySpec(secret.getBytes(), "AES");
Cipher aesCipher = Cipher.getInstance("AES");
aesCipher.init(Cipher.DECRYPT_MODE, secKey);
byte[] bytePlainText = aesCipher.doFinal(byteCipherText);
String myDecryptedText = = new String(bytePlainText);

在我对我的工作有任何想法之前,我尝试过base64解码,添加了一些IV和很多东西我读,当然没有工作。

Before I had any idea of what I was doing, I tried base64 decoding, adding some IV and a lot of stuff I read, of course none of it worked.

但是在我开始理解之后,我还在做什么,我在上面写了那个简单的脚本,并且给了我同样的错误信息: Invalid AES密钥长度

But after I started to understand, kinda, what I was doing, I wrote that simple script above, and got me the same error on the post: Invalid AES key length

我不知道从哪里开始。在阅读了很多关于这个,解决方案似乎哈希或填充,但我没有控制加密方法,所以我不能真正哈希秘密或垫它。

I don't know where to go from here. After reading a lot about this, the solution seems to be hashing or padding, but I have no control on the encryption method, so I can't really hash the secret or pad it.

但是正如我所说,我可以改变密钥,以便它可以匹配一些特定的长度,我已经尝试改变它,但是当我在黑暗中射击,我不知道这是否解决方案。

But as I said, I can change the secret key so it can match some specific length, and I have tried changing it, but as I'm shooting in the dark here, I don't really know if this is the solution.

所以,我的问题基本上是,如果我有加密的字符串(在JavaScript中像第一个脚本)和密钥,有没有办法解密它(在Java )?

So, my question basically is, If I got the encrypted string (in javascript like the first script) and the secret key, is there a way to decrypt it (in Java)? If so, how to do it?

推荐答案

CryptoJS实现与OpenSSL相同的密钥导出函数,加密的数据。因此,处理OpenSSL编码数据的所有Java代码都适用。

CryptoJS implements the same key derivation function as OpenSSL and the same format to put the IV into the encrypted data. So all Java code that deals with OpenSSL encoded data applies.

给定以下Javascript代码:

Given the following Javascript code:

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

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