使用Java的AES加密和使用Javascript的解密 [英] AES encryption using Java and decryption using Javascript

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

问题描述

我正在做一个需要基于Java的AES加密和基于JavaScript的解密的应用程序。
我使用以下代码作为基本形式的加密。

  public class AESencrp {

private static final String ALGO =AES;
private static final byte [] keyValue =
new byte [] {'A','b','c','d','e','f','g' b'b'h','i','j','k','l','m','n','o','p'};

public static String encrypt(String Data)throws异常{
Key key = generateKey();
密码c = Cipher.getInstance(ALGO);
c.init(Cipher.ENCRYPT_MODE,key);
byte [] encVal = c.doFinal(Data.getBytes());
String encryptedValue = new BASE64Encoder()。encode(encVal);
return encryptedValue;
}


private static Key generateKey()throws Exception {
Key key = new SecretKeySpec(keyValue,ALGO);
返回键;
}
}

我试图用来解密的JavaScript是

 < script src =http://crypto-js.googlecode.com/svn/tags/3.1.2/建立/汇总/ aes.js> < /脚本> 

var decryptpted = CryptoJS.AES.decrypt(encrypted,Abcdefghijklmnop)。toString(CryptoJS.enc.Utf8);

但JavaScript解密不起作用。我是新的,有人可以告诉我一种方法来解决,而不改变Java代码块?



我尝试Base-64解码我的文本,如下所示:

  var words = CryptoJS.enc.Base64.parse(encrKey); 
var base64 = CryptoJS.enc.Base64.stringify(words);
var decryptpted = CryptoJS.AES.decrypt(base64,Abcdefghijklmnop);
alert(dec:+解密); X-454545451-45 X- 20045 X- 20045 X- 20045 X- 20045 X- 20045 X- 20045 X- 200 X- 20045 X- 20045 X-尝试下面提出的解决方案来解决可能的填充问题,但它没有给出任何解决方案。

  var key = CryptoJS.enc.Base64。解析( QWJjZGVmZ2hpamtsbW5vcA ==); 
var decrypt = CryptoJS.AES.decrypt(encrKey,key,{mode:CryptoJS.mode.ECB,padding:CryptoJS.pad.Pkcs7});

alert(dec:+ decrypt);


解决方案


  1. Java代码使用128位AES密钥,而JavaScript代码使用256位AES密钥。


  2. 您的Java代码使用Abcdefghijklmnop.getBytes )作为实际的键值,而您的JavaScript代码使用Abcdefghijklmnop作为导出实际键的密码。


  3. Java的默认转换AES是AES / ECB / PKCS5Padding,而CryptoJS的默认转换是AES / CBC / PKCS7Padding。


修复你的例子是修复JavaScript端:

  //这是Java对应的Base64表示
// byte [] keyValue = new byte [] {'A','b','c','d','e','f','g',
//'h' ','j','k','l','m','n','o','p'};
// String keyForJS = new BASE64Encoder()。encode(keyValue);
var base64Key =QWJjZGVmZ2hpamtsbW5vcA ==;
console.log(base64Key =+ base64Key);新新新新新旗新新新新旗新新新新旗新新旗新新旗新新旗新新200新200新新新200新200新200新新新200新新旗新新200新200新新新200新新200新200新新新200新新200新200新新200新新新200新200新新200新新200新新新200新新200新新旗200新新新200新新200新新新新旗新新旗新新款200新新款旗新新款200
console.log(key =+ key);

//这是纯文本
var plaintText =Hello,World!;
console.log(plaintText =+ plaintText);

//这是Base64编码的加密数据
var encryptedData = CryptoJS.AES.encrypt(plaintText,key,{
mode:CryptoJS.mode.ECB,
padding:CryptoJS.pad.Pkcs7
});
console.log(encryptedData =+ encryptedData);

//这是解密的数据作为字节序列
var decryptptedData = CryptoJS.AES.decrypt(encryptedData,key,{
mode:CryptoJS.mode.ECB,
padding:CryptoJS.pad.Pkcs7
});
console.log(decryptptedData =+ decryptptedData);新新新新新新新旗新新新新旗新新旗新新旗新新旗新新旗新新旗新新200新新新旗新新旗新新旗新新旗新新旗新新旗新新旗新新旗新新200新新旗新新旗新新旗新新旗新新旗新新旗新新旗新新旗新新新旗新新旗新新旗新新旗新新旗新新旗新新旗新新旗新新旗新新旗新新旗新新旗新新旗新新旗新新旗新新旗新新旗新新旗新新旗新新旗新新旗新新旗新新旗新新旗新新旗新新旗新新旗新新旗新新旗新新旗新新旗新新旗新新旗新新旗新新旗新新旗新新旗新新旗新新旗新新旗
console.log(decryptptedText =+ decryptptedText);


I am making an application which needs Java based AES Encryption and JavaScript based decryption. I am using the following code for encryption as a basic form.

public class AESencrp {

  private static final String ALGO = "AES";
  private static final byte[] keyValue = 
      new byte[] { 'A', 'b', 'c', 'd', 'e', 'f', 'g',
      'h', 'i', 'j', 'k','l', 'm', 'n', 'o', 'p'};

  public static String encrypt(String Data) throws Exception {
    Key key = generateKey();
    Cipher c = Cipher.getInstance(ALGO);
    c.init(Cipher.ENCRYPT_MODE, key);
    byte[] encVal = c.doFinal(Data.getBytes());
    String encryptedValue = new BASE64Encoder().encode(encVal);
    return encryptedValue;
  }


  private static Key generateKey() throws Exception {
    Key key = new SecretKeySpec(keyValue, ALGO);
    return key;
  }
}

The JavaScript that I am trying to use to decrypt is

<script src="http://crypto-js.googlecode.com/svn/tags/3.1.2/build/rollups/aes.js">   </script>

var decrypted = CryptoJS.AES.decrypt(encrypted,"Abcdefghijklmnop").toString(CryptoJS.enc.Utf8);

But the JavaScript decryption is not working. I am new to this, could someone tell me a way to solve without changing the Java code block ?

I tried Base-64 decoding my text like this:

var words  = CryptoJS.enc.Base64.parse(encrKey);
var base64 = CryptoJS.enc.Base64.stringify(words);
var decrypted = CryptoJS.AES.decrypt(base64, "Abcdefghijklmnop");
alert("dec :" +decrypted);

but still no good.

I tried the solution suggested below to resolve possible padding issue but its not giving any solution.

var key = CryptoJS.enc.Base64.parse("QWJjZGVmZ2hpamtsbW5vcA==");
var decrypt = CryptoJS.AES.decrypt( encrKey, key, { mode: CryptoJS.mode.ECB,padding: CryptoJS.pad.Pkcs7 } );

alert("dec :" +decrypt);

解决方案

  1. Your Java code uses the 128-bit AES key while your JavaScript code uses the 256-bit AES key.

  2. Your Java code uses the "Abcdefghijklmnop".getBytes() as the actual key value, while your JavaScript code uses the "Abcdefghijklmnop" as the passphrase from which the actual key is derived.

  3. The default transformation for Java AES is AES/ECB/PKCS5Padding, while default transformation for CryptoJS is AES/CBC/PKCS7Padding.

One way to fix your example is to fix the JavaScript side:

// this is Base64 representation of the Java counterpart
// byte[] keyValue = new byte[] { 'A', 'b', 'c', 'd', 'e', 'f', 'g',
//                'h', 'i', 'j', 'k','l', 'm', 'n', 'o', 'p'};
// String keyForJS = new BASE64Encoder().encode(keyValue);
var base64Key = "QWJjZGVmZ2hpamtsbW5vcA==";
console.log( "base64Key = " + base64Key );

// this is the actual key as a sequence of bytes
var key = CryptoJS.enc.Base64.parse(base64Key);
console.log( "key = " + key );

// this is the plain text
var plaintText = "Hello, World!";
console.log( "plaintText = " + plaintText );

// this is Base64-encoded encrypted data
var encryptedData = CryptoJS.AES.encrypt(plaintText, key, {
    mode: CryptoJS.mode.ECB,
    padding: CryptoJS.pad.Pkcs7
});
console.log( "encryptedData = " + encryptedData );

// this is the decrypted data as a sequence of bytes
var decryptedData = CryptoJS.AES.decrypt( encryptedData, key, {
    mode: CryptoJS.mode.ECB,
    padding: CryptoJS.pad.Pkcs7
} );
console.log( "decryptedData = " + decryptedData );

// this is the decrypted data as a string
var decryptedText = decryptedData.toString( CryptoJS.enc.Utf8 );
console.log( "decryptedText = " + decryptedText );

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

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