如何解决离子v3 AES 256算法使用加密无法在Java AES / GCM / noPadding算法中解密
我们正在开发基于ionc v3的移动应用程序,我们必须在前端加密密码,并且需要在Java中间件级别解密密码。现在ionic v3我们进行了示例加密,但是加密后的数据无法在我们的Java中解密。请在下面的ionic和Java代码中提出建议,我们必须在此进行更改。
ionic v3 AES 256示例代码
import { Component } from '@angular/core';
import { NavController } from 'ionic-angular';
import { AES256 } from '@ionic-native/aes-256';
import { Platform } from 'ionic-angular/index';
import * as CryptoJS from 'crypto-js';
@Component({
selector: 'page-home',templateUrl: 'home.html'
})
export class HomePage {
text: string;
private secureKey:string ;// Any string,the length should be 32
private secureIV:string ;
private password = "test@123"
EncryptedData :string;
constructor(public navCtrl: NavController,private aes256: AES256,private platform: Platform) {
this.generateSecureKey(this.password);
this.generateSecureIV(this.password);
}
generateSecureKey(password) {
this.platform.ready().then(() => {
this.aes256.generateSecureKey(password).then((secureKey) => {
this.secureKey = secureKey;
console.log('Secure Key----',secureKey);
},(error) => {
console.log('Error----',error);
});
});
}
generateSecureIV(password) {
this.platform.ready().then(() => {
this.aes256.generateSecureIV(password).then((secureIV) => {
this.secureIV = secureIV;
console.log('Secure Key----',secureIV);
},error);
});
});
}
encrypt() {
this.platform.ready().then(() => {
this.aes256.encrypt(this.secureKey,this.secureIV,this.text)
.then(res => {alert('Encrypted Data: '+res)
console.log("Secure Key" +this.secureKey);
console.log("Secure Key" +this.secureIV);
this.EncryptedData = res;
console.log(EncryptedData);
})
.catch((error: any) => console.error(error));
});
}
decrypt() {
this.platform.ready().then(() => {
this.aes256.decrypt(this.secureKey,this.EncryptedData)
.then(res => { alert('Decrypted Data : '+res)
console.log("Secure Key" +this.secureKey);
console.log("Secure Key" +this.secureIV);
})
.catch((error: any) => console.error(error));
});
}
}
Java代码示例:
package com.test.reactapi;
import java.nio.ByteBuffer;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.security.spec.InvalidKeySpecException;
import java.security.spec.KeySpec;
import java.util.Base64;
import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.GCMParameterSpec;
import javax.crypto.spec.PBEKeySpec;
import javax.crypto.spec.SecretKeySpec;
public class TestEncry {
private static final String ENCRYPT_ALGO = "AES/GCM/nopadding";
private static final int TAG_LENGTH_BIT = 128;
private static final Charset UTF_8 = StandardCharsets.UTF_8;
public static String encrypt(byte[] pText) throws Exception {
String password = "test@123";
byte[] salt = getRandomNonce(32);
byte[] iv = getRandomNonce(12);
SecretKey aesKeyFromPassword = getAESKeyFromPassword(password.tochararray(),salt);
Cipher cipher = Cipher.getInstance(ENCRYPT_ALGO);
cipher.init(1,aesKeyFromPassword,new GCMParameterSpec(TAG_LENGTH_BIT,iv));
byte[] cipherText = cipher.doFinal(pText);
byte[] cipherTextWithIvSalt = ByteBuffer.allocate(iv.length + salt.length + cipherText.length).put(iv).put(salt)
.put(cipherText).array();
return Base64.getEncoder().encodetoString(cipherTextWithIvSalt);
}
public static SecretKey getAESKeyFromPassword(char[] password,byte[] salt)
throws NoSuchAlgorithmException,InvalidKeySpecException {
SecretKeyFactory factory = SecretKeyFactory.getInstance("SHA256");
KeySpec spec = new PBEKeySpec(password,salt,65536,256);
return new SecretKeySpec(factory.generateSecret(spec).getEncoded(),"AES");
}
public static String decrypt(String cText) throws Exception {
byte[] plainText = null;
try {
String password = "test@123";
byte[] decode = Base64.getDecoder().decode(cText.getBytes(UTF_8));
ByteBuffer bb = ByteBuffer.wrap(decode);
byte[] iv = new byte[12];
bb.get(iv);
byte[] salt = new byte[32];
bb.get(salt);
byte[] cipherText = new byte[bb.remaining()];
bb.get(cipherText);
SecretKey aesKeyFromPassword = getAESKeyFromPassword(password.tochararray(),salt);
Cipher cipher = Cipher.getInstance(ENCRYPT_ALGO);
cipher.init(2,new GCMParameterSpec(128,iv));
plainText = cipher.doFinal(cipherText);
} catch (Exception e) {
e.printstacktrace();
}
return new String(plainText,UTF_8);
}
public static byte[] getRandomNonce(int numBytes) {
byte[] nonce = new byte[numBytes];
new SecureRandom().nextBytes(nonce);
return nonce;
}
public static void main(String[] args) throws Exception {
System.out.println("Decrypted pwd:" + decrypt("kkkkkk/kllmnn=="));
}
}
请提供建议并帮助我们
错误:
java.nio.BufferUnderflowException
at java.nio.HeapByteBuffer.get(HeapByteBuffer.java:151)
at java.nio.ByteBuffer.get(ByteBuffer.java:715)
at com.test.reactapi.TestEncry.decrypt(TestEncry.java:62)
at com.test.reactapi.TestEncry.main(TestEncry.java:90)
Exception in thread "main" java.lang.NullPointerException
at java.lang.String.<init>(String.java:515)
at com.test.reactapi.TestEncry.decrypt(TestEncry.java:73)
at com.test.reactapi.TestEncry.main(TestEncry.java:90)
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。