1. AES加密字符串
創(chuàng)新互聯(lián)專業(yè)為企業(yè)提供東西湖網(wǎng)站建設(shè)、東西湖做網(wǎng)站、東西湖網(wǎng)站設(shè)計、東西湖網(wǎng)站制作等企業(yè)網(wǎng)站建設(shè)、網(wǎng)頁設(shè)計與制作、東西湖企業(yè)網(wǎng)站模板建站服務(wù),十載東西湖做網(wǎng)站經(jīng)驗,不只是建網(wǎng)站,更提供有價值的思路和整體網(wǎng)絡(luò)服務(wù)。
public static byte[] encrypt(String content, String password) {
try {
KeyGenerator kgen = KeyGenerator.getInstance("AES");// 創(chuàng)建AES的Key生產(chǎn)者
kgen.init(128, new SecureRandom(password.getBytes()));// 利用用戶密碼作為隨機(jī)數(shù)初始化出
// 128位的key生產(chǎn)者
//加密沒關(guān)系,SecureRandom是生成安全隨機(jī)數(shù)序列,password.getBytes()是種子,只要種子相同,序列就一樣,所以解密只要有password就行
SecretKey secretKey = kgen.generateKey();// 根據(jù)用戶密碼,生成一個密鑰
byte[] enCodeFormat = secretKey.getEncoded();// 返回基本編碼格式的密鑰,如果此密鑰不支持編碼,則返回
// null。
SecretKeySpec key = new SecretKeySpec(enCodeFormat, "AES");// 轉(zhuǎn)換為AES專用密鑰
Cipher cipher = Cipher.getInstance("AES");// 創(chuàng)建密碼器
byte[] byteContent = content.getBytes("utf-8");
cipher.init(Cipher.ENCRYPT_MODE, key);// 初始化為加密模式的密碼器
byte[] result = cipher.doFinal(byteContent);// 加密
return result;
} catch (NoSuchPaddingException e) {
e.printStackTrace();
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (InvalidKeyException e) {
e.printStackTrace();
} catch (IllegalBlockSizeException e) {
e.printStackTrace();
} catch (BadPaddingException e) {
e.printStackTrace();
}
return null;
}
2. AES解密
public static byte[] decrypt(byte[] content, String password) {
try {
KeyGenerator kgen = KeyGenerator.getInstance("AES");// 創(chuàng)建AES的Key生產(chǎn)者
kgen.init(128, new SecureRandom(password.getBytes()));
SecretKey secretKey = kgen.generateKey();// 根據(jù)用戶密碼,生成一個密鑰
byte[] enCodeFormat = secretKey.getEncoded();// 返回基本編碼格式的密鑰
SecretKeySpec key = new SecretKeySpec(enCodeFormat, "AES");// 轉(zhuǎn)換為AES專用密鑰
Cipher cipher = Cipher.getInstance("AES");// 創(chuàng)建密碼器
cipher.init(Cipher.DECRYPT_MODE, key);// 初始化為解密模式的密碼器
byte[] result = cipher.doFinal(content);
return 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;
}
你解密的key必須是加密的key啊
你看看,你解密的時候又KeyGenerator.getInstance("AES").generateKey();這是重新搞了一個key啊,當(dāng)然解不出來了
我估計你這代碼人家原先是寫在一起的吧,加密完了再直接解密給你看,人家只generateKey一次,自然很順利,你分成了兩個例子,居然分別generateKey,自然失敗
JDK對DESede算法的支持
密鑰長度:128位
工作模式:ECB/CBC/PCBC/CTR/CTS/CFB/CFB8 to CFB128/OFB/OBF8 to OFB128
填充方式:Nopadding/PKCS5Padding/ISO10126Padding/
AES加密解密的java實現(xiàn):
package com.kongxincai.encanddec;import java.security.Key;import java.security.NoSuchAlgorithmException;import javax.crypto.Cipher;import javax.crypto.KeyGenerator;import javax.crypto.SecretKey;import javax.crypto.spec.SecretKeySpec;public class AESCoder { ? private static final String KEY_ALGORITHM = "AES"; ? private static final String DEFAULT_CIPHER_ALGORITHM = "AES/ECB/PKCS5Padding";//默認(rèn)的加密算法
public static byte[] initSecretKey() { ? ? ? //返回生成指定算法密鑰生成器的 KeyGenerator 對象
KeyGenerator kg = null; ? ? ? try {
kg = KeyGenerator.getInstance(KEY_ALGORITHM);
} catch (NoSuchAlgorithmException e) {
e.printStackTrace(); ? ? ? ? ? return new byte[0];
} ? ? ? //初始化此密鑰生成器,使其具有確定的密鑰大小 ? ? ? //AES 要求密鑰長度為 128
kg.init(128); ? ? ? //生成一個密鑰
SecretKey ?secretKey = kg.generateKey(); ? ? ? return secretKey.getEncoded();
} ? private static Key toKey(byte[] key){ ? ? ? //生成密鑰
return new SecretKeySpec(key, KEY_ALGORITHM);
} ? public static byte[] encrypt(byte[] data,Key key) throws Exception{ ? ? ? return encrypt(data, key,DEFAULT_CIPHER_ALGORITHM);
} ? public static byte[] encrypt(byte[] data,byte[] key) throws Exception{ ? ? ? return encrypt(data, key,DEFAULT_CIPHER_ALGORITHM);
} ? public static byte[] encrypt(byte[] data,byte[] key,String cipherAlgorithm) throws Exception{ ? ? ? //還原密鑰
Key k = toKey(key); ? ? ? return encrypt(data, k, cipherAlgorithm);
} ? public static byte[] encrypt(byte[] data,Key key,String cipherAlgorithm) throws Exception{ ? ? ? //實例化
Cipher cipher = Cipher.getInstance(cipherAlgorithm); ? ? ? //使用密鑰初始化,設(shè)置為加密模式 ? ? ? cipher.init(Cipher.ENCRYPT_MODE, key); ? ? ? //執(zhí)行操作
return cipher.doFinal(data);
} ? public static byte[] decrypt(byte[] data,byte[] key) throws Exception{ ? ? ? return decrypt(data, key,DEFAULT_CIPHER_ALGORITHM);
} ? public static byte[] decrypt(byte[] data,Key key) throws Exception{ ? ? ? return decrypt(data, key,DEFAULT_CIPHER_ALGORITHM);
} ? public static byte[] decrypt(byte[] data,byte[] key,String cipherAlgorithm) throws Exception{ ? ? ? //還原密鑰
Key k = toKey(key); ? ? ? return decrypt(data, k, cipherAlgorithm);
} ? public static byte[] decrypt(byte[] data,Key key,String cipherAlgorithm) throws Exception{ ? ? ? //實例化
Cipher cipher = Cipher.getInstance(cipherAlgorithm); ? ? ? //使用密鑰初始化,設(shè)置為解密模式 ? ? ? cipher.init(Cipher.DECRYPT_MODE, key); ? ? ? //執(zhí)行操作
return cipher.doFinal(data);
} ? private static String ?showByteArray(byte[] data){ ? ? ? if(null == data){ ? ? ? ? ? return null;
}
StringBuilder sb = new StringBuilder("{"); ? ? ? for(byte b:data){
sb.append(b).append(",");
}
sb.deleteCharAt(sb.length()-1);
sb.append("}"); ? ? ? return sb.toString();
} ? public static void main(String[] args) throws Exception { ? ? ? byte[] key = initSecretKey();
System.out.println("key:"+showByteArray(key));
Key k = toKey(key); //生成秘鑰
String data ="AES數(shù)據(jù)";
System.out.println("加密前數(shù)據(jù): string:"+data);
System.out.println("加密前數(shù)據(jù): byte[]:"+showByteArray(data.getBytes()));
System.out.println(); ? ? ? byte[] encryptData = encrypt(data.getBytes(), k);//數(shù)據(jù)加密
System.out.println("加密后數(shù)據(jù): byte[]:"+showByteArray(encryptData));// ? ? ? System.out.println("加密后數(shù)據(jù): hexStr:"+Hex.encodeHexStr(encryptData)); ? ? ? System.out.println(); ? ? ? byte[] decryptData = decrypt(encryptData, k);//數(shù)據(jù)解密
System.out.println("解密后數(shù)據(jù): byte[]:"+showByteArray(decryptData));
System.out.println("解密后數(shù)據(jù): string:"+new String(decryptData));
}
}
import?java.security.InvalidKeyException;
import?java.security.Key;
import?java.security.NoSuchAlgorithmException;
import?javax.crypto.*;??
import?javax.crypto.spec.*;??
/**
*?
*?@author?wchun
*?
*?AES128?算法,加密模式為ECB,填充模式為?pkcs7(實際就是pkcs5)
*?
*
*/
public?class?AES?{
static?final?String?algorithmStr="AES/ECB/PKCS5Padding";
static?private?KeyGenerator?keyGen;
static?private?Cipher?cipher;
static?boolean?isInited=false;
//初始化
static?private?void?init()
{
//初始化keyGen
try?{
keyGen=KeyGenerator.getInstance("AES");
}?catch?(NoSuchAlgorithmException?e)?{
//?TODO?Auto-generated?catch?block
e.printStackTrace();
}
keyGen.init(128);
//初始化cipher
try?{
cipher=Cipher.getInstance(algorithmStr);
}?catch?(NoSuchAlgorithmException?e)?{
//?TODO?Auto-generated?catch?block
e.printStackTrace();
}?catch?(NoSuchPaddingException?e)?{
//?TODO?Auto-generated?catch?block
e.printStackTrace();
}
isInited=true;
}
public?static?byte[]?GenKey()
{
if(!isInited)//如果沒有初始化過,則初始化
{
init();
}
return?keyGen.generateKey().getEncoded();
}
public?static?byte[]?Encrypt(byte[]?content,byte[]?keyBytes)
{
byte[]?encryptedText=null;
if(!isInited)//為初始化
{
init();
}
Key?key=new?SecretKeySpec(keyBytes,"AES");
try?{
cipher.init(Cipher.ENCRYPT_MODE,?key);
}?catch?(InvalidKeyException?e)?{
//?TODO?Auto-generated?catch?block
e.printStackTrace();
}
try?{
encryptedText=cipher.doFinal(content);
}?catch?(IllegalBlockSizeException?e)?{
//?TODO?Auto-generated?catch?block
e.printStackTrace();
}?catch?(BadPaddingException?e)?{
//?TODO?Auto-generated?catch?block
e.printStackTrace();
}
return?encryptedText;
}
//解密為byte[]
public?static?byte[]?DecryptToBytes(byte[]?content,byte[]?keyBytes)
{
byte[]?originBytes=null;
if(!isInited)
{
init();
}
Key?key=new?SecretKeySpec(keyBytes,"AES");
try?{
cipher.init(Cipher.DECRYPT_MODE,?key);
}?catch?(InvalidKeyException?e)?{
//?TODO?Auto-generated?catch?block
e.printStackTrace();
}
//解密
try?{
originBytes=cipher.doFinal(content);
}?catch?(IllegalBlockSizeException?e)?{
//?TODO?Auto-generated?catch?block
e.printStackTrace();
}?catch?(BadPaddingException?e)?{
//?TODO?Auto-generated?catch?block
e.printStackTrace();
}
return?originBytes;
}
}
先看用什么方式的加密,拿AES來說,你需要問java要到混淆值、初始化向量與AES加密的方式如AES-192-CFB.然后直接調(diào)用openssl_decrypt方法進(jìn)行解密.
openssl_decrypt('需要解密的字符串','AES-192-CFB','混淆值',0,'初始化向量'),true)
這個算法java SDK自帶的額 參考代碼如下:
/**解密
*?@param?content??待解密內(nèi)容
*?@param?password?解密密鑰
*?@return
*/
public?static?byte[]?decrypt(byte[]?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");//?創(chuàng)建密碼器
cipher.init(Cipher.DECRYPT_MODE,?key);//?初始化
byte[]?result?=?cipher.doFinal(content);
return?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;
}
/**
*?加密
*
*?@param?content?需要加密的內(nèi)容
*?@param?password??加密密碼
*?@return
*/
public?static?byte[]?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");//?創(chuàng)建密碼器
byte[]?byteContent?=?content.getBytes("utf-8");
cipher.init(Cipher.ENCRYPT_MODE,?key);//?初始化
byte[]?result?=?cipher.doFinal(byteContent);
return?result;?//?加密
}?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;
}
圖像界面的話就不說了