已知由对称密钥加密算法加密的密文和密钥,求用java写解密的代码 java 有问必答
问题描述:
密文kxFIF1Iuj95KJ15SyWIcsA==
密钥HceDKgR0qoenzsk46l3Uxg==
用AES CBC加密模式
求不用软件包com.sun.org.apache.xml.internal.security.utils.Base64 的代码
有人能帮忙吗
答
import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import Decoder.BASE64Encoder;
import Decoder.BASE64Decoder;
import com.alibaba.fastjson.JSONObject;
public class Test {
private String ALGO = "AES";
private String ALGO_MODE = "AES/CBC/NoPadding";
private String akey = "kxFIF1Iuj95KJ15SyWIcsA==";
private String aiv = "HceDKgR0qoenzsk46l3Uxg==";
public static void main(String[] args) throws Exception {
Test aes = new Test();//创建AES
JSONObject data = new JSONObject();//创建Json的加密对象
data.put("haha", "hehe");
System.out.println("原始数据:"+data.toJSONString());
String rstData = pkcs7padding(data.toJSONString());//进行PKCS7Padding填充
String passwordEnc = aes.encrypt(rstData);//进行java的AES/CBC/NoPadding加密
String passwordDec = aes.decrypt(passwordEnc);//解密
System.out.println("加密之后的字符串:"+passwordEnc);
System.out.println("解密后的数据:"+passwordDec);
}
public String encrypt(String Data) throws Exception {
try {
byte[] iv = toByteArray(aiv);//因为要求IV为16byte,而此处aiv串为32位字符串,所以将32位字符串转为16byte
Cipher cipher = Cipher.getInstance(ALGO_MODE);
int blockSize = cipher.getBlockSize();
byte[] dataBytes = Data.getBytes();
int plaintextLength = dataBytes.length;
if (plaintextLength % blockSize != 0) {
plaintextLength = plaintextLength + (blockSize - (plaintextLength % blockSize));
}
byte[] plaintext = new byte[plaintextLength];
System.arraycopy(dataBytes, 0, plaintext, 0, dataBytes.length);
SecretKeySpec keyspec = new SecretKeySpec(akey.getBytes("utf-8"), ALGO);
IvParameterSpec ivspec = new IvParameterSpec(iv);
cipher.init(Cipher.ENCRYPT_MODE, keyspec, ivspec);
byte[] encrypted = cipher.doFinal(plaintext);
String EncStr = (new BASE64Encoder()).encode(encrypted);//将cipher加密后的byte数组用base64加密生成字符串
return EncStr ;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
public String decrypt(String encryptedData) throws Exception {
try {
byte[] encrypted1 = (new BASE64Decoder()).decodeBuffer(encryptedData);
byte[] iv = toByteArray(aiv);
Cipher cipher = Cipher.getInstance(ALGO_MODE);
SecretKeySpec keyspec = new SecretKeySpec(akey.getBytes("utf-8"), ALGO);
IvParameterSpec ivspec = new IvParameterSpec(iv);
cipher.init(Cipher.DECRYPT_MODE, keyspec, ivspec);
byte[] original = cipher.doFinal(encrypted1);
String originalString = new String(original);
return originalString.trim();//此处添加trim()是为了去除多余的填充字符,就不用去填充了,具体有什么问题我还没有遇到,有强迫症的同学可以自己写一个PKCS7UnPadding函数
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
//此函数是将字符串每两个字符合并生成byte数组
public static byte[] toByteArray(String hexString) {
hexString = hexString.toLowerCase();
final byte[] byteArray = new byte[hexString.length() >> 1];
int index = 0;
for (int i = 0; i < hexString.length(); i++) {
if (index > hexString.length() - 1)
return byteArray;
byte highDit = (byte) (Character.digit(hexString.charAt(index), 16) & 0xFF);
byte lowDit = (byte) (Character.digit(hexString.charAt(index + 1), 16) & 0xFF);
byteArray[i] = (byte) (highDit << 4 | lowDit);
index += 2;
}
System.out.println(byteArray.length);
return byteArray;
}
//此函数是pkcs7padding填充函数
public static String pkcs7padding(String data) {
int bs = 16;
int padding = bs - (data.length() % bs);
String padding_text = "";
for (int i = 0; i < padding; i++) {
padding_text += (char)padding;
}
return data+padding_text;
}
}