AES 加密解密的 JAVA 实现 【二】
2016-10-13 11:04
766 查看
使用环境 JDK1.8
这种方法 密钥可以不是16位,偏移量是16位。
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.security.Security;
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.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import org.bouncycastle.jce.provider.BouncyCastleProvider;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;
/**
* <b>AES 加密/解密[一种128位密钥补位的方式]</b><br>
* 调用方法参见main方法,事先可以指定自己的 iv和key参数<br>
* 注意:这是AES 的 一种,其中公开密钥16位,密钥可以不是16位;偏移量16位,否则会报错;<br>
* 另外,本方法发的实际密钥按照128位随机填充的方式,也有其他的实现方式,会产生不同的密文,需要注意。<br>
*
*
*/
public class AES {
public static String key = "ThisIsMyTestKey12345";
public static String iv = "1234567890123456";
/**
* 偏移量获取
* @return
* @throws UnsupportedEncodingException
*/
private static IvParameterSpec getIV() throws UnsupportedEncodingException {
return new IvParameterSpec(iv.getBytes("UTF-8"));
}
/**
* 密钥生成
* @return
* @throws UnsupportedEncodingException
* @throws NoSuchAlgorithmException
*/
public static SecretKeySpec getKey() throws UnsupportedEncodingException, NoSuchAlgorithmException {
Security.addProvider(new BouncyCastleProvider());
KeyGenerator kgen = KeyGenerator.getInstance("AES");
kgen.init(128, new SecureRandom(key.getBytes()));
SecretKey keygen = kgen.generateKey();
SecretKeySpec keySpec = new SecretKeySpec(keygen.getEncoded(), "AES");
return keySpec;
}
/**
* 解密
*
* @param content
* @return
*/
public static String decrypt(String content) {
try {
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
cipher.init(Cipher.DECRYPT_MODE, getKey(), getIV());
byte[] decodedContent = new BASE64Decoder().decodeBuffer(content);
byte[] original = cipher.doFinal(decodedContent);
return new String(original);
} catch (IOException e) {
e.printStackTrace();
} catch (IllegalBlockSizeException e) {
e.printStackTrace();
} catch (BadPaddingException e) {
e.printStackTrace();
} catch (InvalidKeyException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InvalidAlgorithmParameterException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (NoSuchAlgorithmException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (NoSuchPaddingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
;
return null;
}
/**
* 加密
*
* @param content
* @return
*/
public static String encrypt(String content) {
try {
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
cipher.init(Cipher.ENCRYPT_MODE, getKey(), getIV());
byte[] contentByte = content.getBytes("UTF-8");
byte[] result = cipher.doFinal(contentByte);
return new BASE64Encoder().encode(result);
} catch (IllegalBlockSizeException e) {
e.printStackTrace();
} catch (BadPaddingException e) {
e.printStackTrace();
} catch (InvalidKeyException e) {
e.printStackTrace();
} catch (InvalidAlgorithmParameterException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (NoSuchAlgorithmException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (NoSuchPaddingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
};
return null;
}
/**
* 调用的测试主方法
* @param args
* @throws UnsupportedEncodingException
*/
public static void main(String[] args) throws UnsupportedEncodingException {
String content = "欲穷千里目,更上一层楼";
AES.key +="ThisIsMyTestKey12345ThisIsMyTestKey12345ThisIsMyTestKey12345ThisIsMyTestKey12345ThisIsMyTestKey12345ThisIsMyTestKey12345";
// 加密
System.out.println("加密前:" + content);
String resultString = AES.encrypt(content);
System.out.println("加密后:" + resultString);
// 解密
String decryptResult = AES.decrypt(resultString);
System.out.println("解密后:" + decryptResult);
}
}
这种方法 密钥可以不是16位,偏移量是16位。
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.security.Security;
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.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import org.bouncycastle.jce.provider.BouncyCastleProvider;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;
/**
* <b>AES 加密/解密[一种128位密钥补位的方式]</b><br>
* 调用方法参见main方法,事先可以指定自己的 iv和key参数<br>
* 注意:这是AES 的 一种,其中公开密钥16位,密钥可以不是16位;偏移量16位,否则会报错;<br>
* 另外,本方法发的实际密钥按照128位随机填充的方式,也有其他的实现方式,会产生不同的密文,需要注意。<br>
*
*
*/
public class AES {
public static String key = "ThisIsMyTestKey12345";
public static String iv = "1234567890123456";
/**
* 偏移量获取
* @return
* @throws UnsupportedEncodingException
*/
private static IvParameterSpec getIV() throws UnsupportedEncodingException {
return new IvParameterSpec(iv.getBytes("UTF-8"));
}
/**
* 密钥生成
* @return
* @throws UnsupportedEncodingException
* @throws NoSuchAlgorithmException
*/
public static SecretKeySpec getKey() throws UnsupportedEncodingException, NoSuchAlgorithmException {
Security.addProvider(new BouncyCastleProvider());
KeyGenerator kgen = KeyGenerator.getInstance("AES");
kgen.init(128, new SecureRandom(key.getBytes()));
SecretKey keygen = kgen.generateKey();
SecretKeySpec keySpec = new SecretKeySpec(keygen.getEncoded(), "AES");
return keySpec;
}
/**
* 解密
*
* @param content
* @return
*/
public static String decrypt(String content) {
try {
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
cipher.init(Cipher.DECRYPT_MODE, getKey(), getIV());
byte[] decodedContent = new BASE64Decoder().decodeBuffer(content);
byte[] original = cipher.doFinal(decodedContent);
return new String(original);
} catch (IOException e) {
e.printStackTrace();
} catch (IllegalBlockSizeException e) {
e.printStackTrace();
} catch (BadPaddingException e) {
e.printStackTrace();
} catch (InvalidKeyException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InvalidAlgorithmParameterException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (NoSuchAlgorithmException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (NoSuchPaddingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
;
return null;
}
/**
* 加密
*
* @param content
* @return
*/
public static String encrypt(String content) {
try {
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
cipher.init(Cipher.ENCRYPT_MODE, getKey(), getIV());
byte[] contentByte = content.getBytes("UTF-8");
byte[] result = cipher.doFinal(contentByte);
return new BASE64Encoder().encode(result);
} catch (IllegalBlockSizeException e) {
e.printStackTrace();
} catch (BadPaddingException e) {
e.printStackTrace();
} catch (InvalidKeyException e) {
e.printStackTrace();
} catch (InvalidAlgorithmParameterException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (NoSuchAlgorithmException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (NoSuchPaddingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
};
return null;
}
/**
* 调用的测试主方法
* @param args
* @throws UnsupportedEncodingException
*/
public static void main(String[] args) throws UnsupportedEncodingException {
String content = "欲穷千里目,更上一层楼";
AES.key +="ThisIsMyTestKey12345ThisIsMyTestKey12345ThisIsMyTestKey12345ThisIsMyTestKey12345ThisIsMyTestKey12345ThisIsMyTestKey12345";
// 加密
System.out.println("加密前:" + content);
String resultString = AES.encrypt(content);
System.out.println("加密后:" + resultString);
// 解密
String decryptResult = AES.decrypt(resultString);
System.out.println("解密后:" + decryptResult);
}
}
相关文章推荐
- C#, Java, PHP, Python和Javascript几种语言的AES加密解密实现
- AES加密解密->java实现
- RSA AES 前端JS与后台JAVA的加密解密的是实现
- JAVA和PYTHON同时实现AES的加密解密操作---且生成的BASE62编码一致
- java和js实现aes加密解密
- C#, Java, PHP, Python和Javascript几种语言的AES加密解密实现
- java实现AES加密解密
- C# 实现 JAVA AES加密解密[原创]
- 基于Android系统的AES加密、解密的JAVA实现
- javascript 实现 aes加密解密(可与Java互解)
- C#, Java, PHP, Python和Javascript几种语言的AES加密解密实现[转载]
- JAVA实现AES加密和解密
- AES 加密解密的JAVA实现方式【一】已调通
- Go语言解密上篇中用java aes实现的加密
- PHP、Java、.Net和Javascript的AES加密解密实现
- java和js实现aes加密解密
- java和js实现aes加密解密
- Vickate_AES加密与解密(Objective-C版),暂未实现与java的交互(求大神帮助)
- Java实现AES加密解密
- [Java 实现AES加密解密]