问题描述
我对AES加密和解密非常陌生。在我的应用程序中,我必须解密从服务器获取的数据。我收到的数据使用CryptoJS库加密。解密工作得很好。但是,在将数据发布到服务器时,我必须再次对数据进行加密并将其发送到服务器,这没有进行适当的加密。我已经按照进行了解密,效果很好。我将在下面发布我的解密代码。请帮忙。
I am very much new to AES encryption and decryption. In my app, I have to decrypt the data which I get from server. The data I recieve is encrypted using CryptoJS library. The decryption works pretty much fine. But while posting the data to server, I have to again encrypt the data and send it to server, which is not giving proper encryption. I have followed This Stack overflow answer for decryption which is working fine.I will post my decryption code below. Please help.
解密:
public static String Decrypt(String Encrpyt , String Key ) throws NoSuchPaddingException, NoSuchAlgorithmException, InvalidAlgorithmParameterException, InvalidKeyException, BadPaddingException, IllegalBlockSizeException {
byte[] cipherData = Base64.decode(Encrpyt, Base64.DEFAULT);
byte[] saltData = Arrays.copyOfRange(cipherData, 8, 16);
MessageDigest md5 = null;
try {
md5 = MessageDigest.getInstance("MD5");
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
final byte[][] keyAndIV = GenerateKeyAndIV(32, 16, 1, saltData, Key.getBytes(StandardCharsets.UTF_8), md5);
SecretKeySpec key = new SecretKeySpec(keyAndIV[0], "AES");
IvParameterSpec iv = new IvParameterSpec(keyAndIV[1]);
byte[] encrypted = Arrays.copyOfRange(cipherData, 16, cipherData.length);
Cipher aesCBC = Cipher.getInstance("AES/CBC/PKCS5Padding");
aesCBC.init(Cipher.DECRYPT_MODE, key, iv);
byte[] decryptedData = aesCBC.doFinal(encrypted);
String decryptedText = new String(decryptedData, StandardCharsets.UTF_8);
System.out.println(decryptedText);
return decryptedText;
}
public static byte[][] GenerateKeyAndIV(int keyLength, int ivLength, int iterations, byte[] salt, byte[] password, MessageDigest md) {
int digestLength = md.getDigestLength();
int requiredLength = (keyLength + ivLength + digestLength - 1) / digestLength * digestLength;
byte[] generatedData = new byte[requiredLength];
int generatedLength = 0;
try {
md.reset();
// Repeat process until sufficient data has been generated
while (generatedLength < keyLength + ivLength) {
// Digest data (last digest if available, password data, salt if available)
if (generatedLength > 0)
md.update(generatedData, generatedLength - digestLength, digestLength);
md.update(password);
if (salt != null)
md.update(salt, 0, 8);
try {
md.digest(generatedData, generatedLength, digestLength);
} catch (DigestException e) {
e.printStackTrace();
}
// additional rounds
for (int i = 1; i < iterations; i++) {
md.update(generatedData, generatedLength, digestLength);
md.digest(generatedData, generatedLength, digestLength);
}
generatedLength += digestLength;
}
// Copy key and IV into separate byte arrays
byte[][] result = new byte[2][];
result[0] = Arrays.copyOfRange(generatedData, 0, keyLength);
if (ivLength > 0)
result[1] = Arrays.copyOfRange(generatedData, keyLength, keyLength + ivLength);
return result;
} catch (DigestException e) {
throw new RuntimeException(e);
} finally {
// Clean out temporary data
Arrays.fill(generatedData, (byte)0);
}
}
推荐答案
此库可以在所有平台上正常工作。
This library working fine for all platforms.
用于Android加密
For android encryption
步骤1:使用此实用程序类->
步骤2:加密&
try {
String plainText = "this is my plain text";
String key = "simplekey";
String iv = "1234123412341234";
CryptLib cryptLib = new CryptLib();
String encryptedString = cryptLib.encryptSimple(plainText, key, iv);
System.out.println("encryptedString " + encryptedString);
String decryptedString = cryptLib.decryptSimple(encryptedString, key, iv);
System.out.println("decryptedString " + decryptedString);
} catch (Exception e) {
e.printStackTrace();
}
Java脚本代码
Java Script code
var plainText = "this is my plain text";
var key = "simplekey";
var iv = "1234123412341234";
var cryptoLib = require('cryptlib');
shaKey = cryptoLib.getHashSha256(key, 32); // This line is not needed on Android or iOS. Its already built into CryptLib.m and CryptLib.java
var encryptedString = cryptoLib.encrypt(plainText, shaKey, iv);
console.log('encryptedString %s', encryptedString);
var decryptedString = cryptoLib.decrypt(encryptedString, shaKey, iv);
console.log('decryptedString %s', decryptedString);
对于iOS用户,请使用此示例
For iOS users use this sample
所有平台都会产生相同的以下输出
All platforms produce same following output
解密的字符串: >这是我的纯文本
这篇关于从android加密并在CryptoJS中解密的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!