有固定的密钥key的AES加密
2009-05-12 16:14
344 查看
package test;
/*****************************************************
*
* @author wuzhenzhong
*
* @since May 12, 2009
*
*****************************************************/
import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
public class EncryptSample {
public static void main(String[] args) throws Exception {
/*
* 256 bit key
*/
String key = "aaaaaaaaaabbbbbbaaaaaaaaaabbbbbb";
/*
* source data
*/
System.out.println("source -------- fengye");
String enBytes = EncryptSample.encryptAES("fengye",key);
System.out.println("mi wen --------------- "+enBytes.toString());
String deBytes = EncryptSample.decryptAES(enBytes, key);
System.out.println("ming wen------------"+new String(deBytes));
}
/*
* 转为十六进制
*/
@SuppressWarnings("unused")
private static String asHex(byte buf[]) {
StringBuffer strbuf = new StringBuffer(buf.length * 2);
int i;
for (i = 0; i < buf.length; i++) {
if (((int) buf[i] & 0xff) < 0x10)
strbuf.append("0");
strbuf.append(Long.toString((int) buf[i] & 0xff, 16));
}
System.out.println(strbuf.toString());
return strbuf.toString();
}
/*
* 转为二进制
*/
@SuppressWarnings("unused")
private static byte[] asBin(String src) {
if (src.length() < 1)
return null;
byte[] encrypted = new byte[src.length() / 2];
for (int i = 0; i < src.length() / 2; i++) {
int high = Integer.parseInt(src.substring(i * 2, i * 2 + 1), 16);
int low = Integer.parseInt(src.substring(i * 2 + 1, i * 2 + 2), 16);
encrypted[i] = (byte) (high * 16 + low);
}
return encrypted;
}
/*
* 加密
*/
public static String encryptAES(String data, String secret_key) {
byte[] key = asBin(secret_key);
SecretKeySpec sKey = new SecretKeySpec(key, "AES");
try {
Cipher cipher = Cipher.getInstance("AES");
cipher.init(Cipher.ENCRYPT_MODE, sKey);
byte[] encrypted = cipher.doFinal(data.getBytes());
return asHex(encrypted);
} catch (Exception e) {
return null;
}
}
/*
* 解密
*/
public static String decryptAES(String encData, String secret_key) {
byte[] tmp = asBin(encData);
byte[] key = asBin(secret_key);
SecretKeySpec sKey = new SecretKeySpec(key, "AES");
try {
Cipher cipher = Cipher.getInstance("AES");
cipher.init(Cipher.DECRYPT_MODE, sKey);
byte[] decrypted = cipher.doFinal(tmp);
return new String(decrypted);
} catch (Exception e) {
return null;
}
}
}
/*****************************************************
*
* @author wuzhenzhong
*
* @since May 12, 2009
*
*****************************************************/
import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
public class EncryptSample {
public static void main(String[] args) throws Exception {
/*
* 256 bit key
*/
String key = "aaaaaaaaaabbbbbbaaaaaaaaaabbbbbb";
/*
* source data
*/
System.out.println("source -------- fengye");
String enBytes = EncryptSample.encryptAES("fengye",key);
System.out.println("mi wen --------------- "+enBytes.toString());
String deBytes = EncryptSample.decryptAES(enBytes, key);
System.out.println("ming wen------------"+new String(deBytes));
}
/*
* 转为十六进制
*/
@SuppressWarnings("unused")
private static String asHex(byte buf[]) {
StringBuffer strbuf = new StringBuffer(buf.length * 2);
int i;
for (i = 0; i < buf.length; i++) {
if (((int) buf[i] & 0xff) < 0x10)
strbuf.append("0");
strbuf.append(Long.toString((int) buf[i] & 0xff, 16));
}
System.out.println(strbuf.toString());
return strbuf.toString();
}
/*
* 转为二进制
*/
@SuppressWarnings("unused")
private static byte[] asBin(String src) {
if (src.length() < 1)
return null;
byte[] encrypted = new byte[src.length() / 2];
for (int i = 0; i < src.length() / 2; i++) {
int high = Integer.parseInt(src.substring(i * 2, i * 2 + 1), 16);
int low = Integer.parseInt(src.substring(i * 2 + 1, i * 2 + 2), 16);
encrypted[i] = (byte) (high * 16 + low);
}
return encrypted;
}
/*
* 加密
*/
public static String encryptAES(String data, String secret_key) {
byte[] key = asBin(secret_key);
SecretKeySpec sKey = new SecretKeySpec(key, "AES");
try {
Cipher cipher = Cipher.getInstance("AES");
cipher.init(Cipher.ENCRYPT_MODE, sKey);
byte[] encrypted = cipher.doFinal(data.getBytes());
return asHex(encrypted);
} catch (Exception e) {
return null;
}
}
/*
* 解密
*/
public static String decryptAES(String encData, String secret_key) {
byte[] tmp = asBin(encData);
byte[] key = asBin(secret_key);
SecretKeySpec sKey = new SecretKeySpec(key, "AES");
try {
Cipher cipher = Cipher.getInstance("AES");
cipher.init(Cipher.DECRYPT_MODE, sKey);
byte[] decrypted = cipher.doFinal(tmp);
return new String(decrypted);
} catch (Exception e) {
return null;
}
}
}
相关文章推荐
- AES加密,解决了同步问题,和随机密钥和固定密钥,多端通信加密不一致解决办法
- AES加密出现InvalidKeyException: Illegal key size解决方案
- AES加密解密代码,key是16位
- AES实用类,支持加密解密和生成密钥三个功能
- AES加密时抛出java.security.InvalidKeyException: Illegal key size or default parameters
- AppNinja 开发手记5: gdb&cycript用法 - aes加密key破解
- AES/ECB/PKCS5Padding/PKCS7Padding 128位密钥(带密钥补位功能)加密解密
- AES加密时抛出java.security.InvalidKeyException: Illegal key size or default parameters
- Java使用密钥库相关问题(AES加密)
- javaAES加密解密32位密钥
- AES加密时的 java.security.InvalidKeyException: Illegal key size 异常
- AES加密时的 java.security.InvalidKeyException: Illegal key size 异常
- Java实现AES加密,异常java.security.InvalidKeyException: Illegal key size 的解决
- AES加密java.security.InvalidKeyException: Illegal key size or default parameters
- Java实现AES加密,异常java.security.InvalidKeyException: Illegal key size 的解决
- Codeigniter 利用加密Key(密钥)的对象注入漏洞
- AES加密时抛出java.security.InvalidKeyException: Illegal key size or default parameter
- java中Keytool的使用总结 (加密 密钥(key)和证书(certificates))
- PHP用私人密钥key对内容加解密算法,并且每次加密后的值不同
- AES加密时抛出 Illegal key size or default parameters