JavaではAESを使用して暗号化され、暗号化されたKeyの長さは16ビット以上
5908 ワード
https://blog.csdn.net/u010513756/article/details/79017014
コードサンプルソース、ありがとうhttp://www.cnblogs.com/jys509/p/4768120.html#autoid-0-1-0
ここで暗号化されたkeyは
コードサンプルソース、ありがとうhttp://www.cnblogs.com/jys509/p/4768120.html#autoid-0-1-0
import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;
/**
* AES , AES , Base64 ;
*/
public class AESOperator {
/*
* Key 26 AES-128-CBC ,key 16 。
*/
private String sKey = "smkldospdosldaaa";//key,
private String ivParameter = "0392039203920300";// ,
private static AESOperator instance = null;
private AESOperator() {
}
public static AESOperator getInstance() {
if (instance == null)
instance = new AESOperator();
return instance;
}
public static String Encrypt(String encData ,String secretKey,String vector) throws Exception {
if(secretKey == null) {
return null;
}
if(secretKey.length() != 16) {
return null;
}
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
byte[] raw = secretKey.getBytes();
SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
IvParameterSpec iv = new IvParameterSpec(vector.getBytes());// CBC , iv,
cipher.init(Cipher.ENCRYPT_MODE, skeySpec, iv);
byte[] encrypted = cipher.doFinal(encData.getBytes("utf-8"));
return new BASE64Encoder().encode(encrypted);// BASE64 。
}
//
public String encrypt(String sSrc) throws Exception {
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
byte[] raw = sKey.getBytes();
SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
IvParameterSpec iv = new IvParameterSpec(ivParameter.getBytes());// CBC , iv,
cipher.init(Cipher.ENCRYPT_MODE, skeySpec, iv);
byte[] encrypted = cipher.doFinal(sSrc.getBytes("utf-8"));
return new BASE64Encoder().encode(encrypted);// BASE64 。
}
//
public String decrypt(String sSrc) throws Exception {
try {
byte[] raw = sKey.getBytes("ASCII");
SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
IvParameterSpec iv = new IvParameterSpec(ivParameter.getBytes());
cipher.init(Cipher.DECRYPT_MODE, skeySpec, iv);
byte[] encrypted1 = new BASE64Decoder().decodeBuffer(sSrc);// base64
byte[] original = cipher.doFinal(encrypted1);
String originalString = new String(original, "utf-8");
return originalString;
} catch (Exception ex) {
return null;
}
}
public String decrypt(String sSrc,String key,String ivs) throws Exception {
try {
byte[] raw = key.getBytes("ASCII");
SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
IvParameterSpec iv = new IvParameterSpec(ivs.getBytes());
cipher.init(Cipher.DECRYPT_MODE, skeySpec, iv);
byte[] encrypted1 = new BASE64Decoder().decodeBuffer(sSrc);// base64
byte[] original = cipher.doFinal(encrypted1);
String originalString = new String(original, "utf-8");
return originalString;
} catch (Exception ex) {
return null;
}
}
public static String encodeBytes(byte[] bytes) {
StringBuffer strBuf = new StringBuffer();
for (int i = 0; i < bytes.length; i++) {
strBuf.append((char) (((bytes[i] >> 4) & 0xF) + ((int) 'a')));
strBuf.append((char) (((bytes[i]) & 0xF) + ((int) 'a')));
}
return strBuf.toString();
}
public static void main(String[] args) throws Exception {
//
String cSrc = "[{\"request_no\":\"1001\",\"service_code\":\"FS0001\",\"contract_id\":\"100002\",\"order_id\":\"0\",\"phone_id\":\"13913996922\",\"plat_offer_id\":\"100094\",\"channel_id\":\"1\",\"activity_id\":\"100045\"}]";
//
long lStart = System.currentTimeMillis();
String enString = AESOperator.getInstance().encrypt(cSrc);
System.out.println(" :" + enString);
long lUseTime = System.currentTimeMillis() - lStart;
System.out.println(" :" + lUseTime + " ");
//
lStart = System.currentTimeMillis();
String DeString = AESOperator.getInstance().decrypt(enString);
System.out.println(" :" + DeString);
lUseTime = System.currentTimeMillis() - lStart;
System.out.println(" :" + lUseTime + " ");
}
}
ここで暗号化されたkeyは
smkldospdosldaaa
で、32ビット以上に変更するとjava.security.InvalidKeyException:Illegal key sizeと報告されます. 解決策: 対応するjarファイルをダウンロードして既存のものを置き換え、 jdk 1.8http://www.oracle.com/technetwork/java/javase/downloads/jce8-download-2133166.html. 他のバージョンと同様に、ダウンロード後に解凍し、主にlocal_に使用されます.义齿export_policy.jar、JAVAにコピーHOMEの下のjrelibsecurityに置き換えると、 私は即構人形機で使っています.彼はjsonの列をAESで暗号化する必要があります.