package com.sjq.common.utils;
import java.io.UnsupportedEncodingException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.KeyGenerator;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
/**
* AES加密,解密類:用于用戶密碼加密解密
* @author lupb
*/
public class AESPassword {
public static String key="123321";
public static void main(String[] args) throws UnsupportedEncodingException {
String password = "123456";
String aesPassword = AESPassword.encrypt(password, key);
System.out.println("通過AES加密:" + aesPassword);
password = AESPassword.decrypt(aesPassword, key);
System.out.println("AES解密:" + password);
}
/**
* 加密
* @param content 需要加密的內容
* @param password 加密密碼
* @return aes加密串
*/
public static String encrypt(String content, String password) {
try {
KeyGenerator kgen = KeyGenerator.getInstance("AES");
kgen.init(128, new SecureRandom(password.getBytes()));
SecretKey secretKey = kgen.generateKey();
byte[] enCodeFormat = secretKey.getEncoded();
SecretKeySpec key = new SecretKeySpec(enCodeFormat, "AES");
Cipher cipher = Cipher.getInstance("AES");// 創建密碼器
byte[] byteContent = content.getBytes("utf-8");
cipher.init(Cipher.ENCRYPT_MODE, key);// 初始化
byte[] result = cipher.doFinal(byteContent);
return parseByte2HexStr(result);//.toString(); // 加密
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (NoSuchPaddingException e) {
e.printStackTrace();
} catch (InvalidKeyException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (IllegalBlockSizeException e) {
e.printStackTrace();
} catch (BadPaddingException e) {
e.printStackTrace();
}
return null;
}
/**
* 解密
* @param content 需要解密的內容
* @param password 解密密碼
* @return 解密后的字串
*/
public static String decrypt(String content, String password) {
try {
byte[] contents=parseHexStr2Byte(content);//.getBytes();
KeyGenerator kgen = KeyGenerator.getInstance("AES");
kgen.init(128, new SecureRandom(password.getBytes()));
SecretKey secretKey = kgen.generateKey();
byte[] enCodeFormat = secretKey.getEncoded();
SecretKeySpec key = new SecretKeySpec(enCodeFormat, "AES");
Cipher cipher = Cipher.getInstance("AES");// 創建密碼器
cipher.init(Cipher.DECRYPT_MODE, key);// 初始化
byte[] result = cipher.doFinal(contents);
// return parseByte2HexStr(result);//.toString(); // 加密
return new String(result);
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (NoSuchPaddingException e) {
e.printStackTrace();
} catch (InvalidKeyException e) {
e.printStackTrace();
} catch (IllegalBlockSizeException e) {
e.printStackTrace();
} catch (BadPaddingException e) {
e.printStackTrace();
}
return null;
}
public static String parseByte2HexStr(byte buf[]) {
StringBuffer sb = new StringBuffer();
for (int i = 0; i < buf.length; i++) {
String hex = Integer.toHexString(buf[i] & 0xFF);
if (hex.length() == 1) {
hex = '0' + hex;
}
sb.append(hex.toUpperCase());
}
return sb.toString();
}
public static byte[] parseHexStr2Byte(String hexStr) {
if (hexStr.length() < 1)
return null;
byte[] result = new byte[hexStr.length()/2];
for (int i = 0;i< hexStr.length()/2; i++) {
int high = Integer.parseInt(hexStr.substring(i*2, i*2+1), 16);
int low = Integer.parseInt(hexStr.substring(i*2+1, i*2+2), 16);
result[i] = (byte) (high * 16 + low);
}
return result;
}
}
uj5u.com熱心網友回復:
用delphi 的AES 算出的結果 跟 java算出來的不一樣uj5u.com熱心網友回復:
如果自己寫的邏輯,應該是一樣的哈.可以指定用unicode字符集.uj5u.com熱心網友回復:
parseByte2HexStr
parseHexStr2Byte 這2個 看起來就是轉16進制字符,轉byte陣列,delphi的aes庫跟這java轉出來的結果差異很大。跟網上在線aes加解密也不一樣
uj5u.com熱心網友回復:
http://www.cnblogs.com/freeliver54/archive/2011/10/08/2202136.html 媽蛋,對接的那邊的java代碼有bug。。uj5u.com熱心網友回復:
java 內容:123456 key: 123321 加密后的16進制結果是 671F543C07AF8ADEAC226745EBC40D61delphi轉的 和網上轉的就沒有一個和這對的上的
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/69256.html
標籤:語言基礎/算法/系統設計
下一篇:圖片的加密解密
