java使⽤AES-128-cbc加解密
⾸先介绍相关的⼏个重要的类:
(⼀)KeyGenerator
Java提供了⼀个名称为KeyGenerator的类,该类⽤于⽣成密钥,此类的对象是可重⽤的。
要使⽤KeyGenerator类⽣成密钥,请按照以下步骤操作
第1步:创建KeyGenerator对象
KeyGenerator类提供getInstance()⽅法,该⽅法接受表⽰所需密钥⽣成算法的String变量,并返回⽣成密钥的KeyGenerator对象。
第2步:创建SecureRandom对象java.Security包的SecureRandom类提供了⼀个强⼤的随机数⽣成器,⽤于在Java中⽣成随机数
第3步:第初始化KeyGeneratorKeyGenerator类提供了⼀个名为init()的⽅法,此⽅法接受SecureRandom对象并初始化当前的KeyGenerator。
第4步:利⽤ateKey() ⽣成⼀个密钥。返回的是⼀个SecretKey ,SecretKey key = ateKey()
第5步:得到密钥的字节数组,byte[] keys = Encoded();
private static void createKey(){
try{
KeyGenerator keyGenerator = Instance("AES");
keyGenerator.init(128,new SecureRandom());
SecretKey secretKey = ateKey();
key = Encoded();
}catch(NoSuchAlgorithmException e){
e.printStackTrace();
}
}
也可以使⽤SecretKeySpec 类创造密钥:
SecretKeySpec keySpec =new Bytes(StandardCharsets.UTF_8),"AES");
(⼆)Cipher
此类为加密和解密提供密码功能。
(1)为创建 Cipher 对象,应⽤程序调⽤ Cipher 的 getInstance ⽅法并将所请求转换 的名称传递给它。
(2)初始化cipher.init(字段,密钥,new Bytes()) ) ,最后⼀个参数为初始化向量,并且string为16字节(三)CipherInputStream
读取字节流时调⽤的cipher.update()⽅法进⾏流部分加密, 当加密到最后⼀段时,会调⽤ doFinal() ⽅法。
CipherInputStream cipherInputStream =new CipherInputStream(inputFile, cipher);
加密代码:
/*
*加密
*/
private static void encodeFile(String path, String encodePath)throws NoSuchPaddingException, NoSuchAlgorithmException, InvalidAlgorithmParameter Exception, InvalidKeyException, IOException {
SecretKeySpec keySpec =new SecretKeySpec(key,"AES");
Cipher cipher = Instance("AES/CBC/PKCS5Padding");
cipher.init(Cipher.ENCRYPT_MODE,keySpec,new Bytes(StandardCharsets.UTF_8)));
FileInputStream inputStream =new FileInputStream(path);
CipherInputStream cipherInputStream =new CipherInputStream(inputStream,cipher);
FileOutputStream outputStream =new FileOutputStream(encodePath);
int length;
byte[] b =new byte[1024];
while((length = ad(b))!=-1){
outputStream.write(b);
outputStream.flush();
}
cipherInputStream.close();
outputStream.close();
}
解密:
/*
* 解密
*/
private static void decoder(String encode,String decoder)throws NoSuchPaddingException, NoSuchAlgorithmException, InvalidAlgorithmParameterExce ption, InvalidKeyException, IOException {
SecretKeySpec keySpec =new SecretKeySpec(key,"AES");
Cipher cipher = Instance("AES/CBC/PKCS5Padding");
cipher.init(Cipher.DECRYPT_MODE,keySpec,new Bytes(StandardCharsets.UTF_8)));
java生成随机数的方法FileInputStream inputStream =new FileInputStream(encode);
FileOutputStream outputStream =new FileOutputStream(decoder);
CipherOutputStream cipherOutputStream =new CipherOutputStream(outputStream,cipher);
int length;
byte[] bytes =new byte[1024];
while((length = ad(bytes))!=-1){
cipherOutputStream.write(bytes,0,length);
}
inputStream.close();
cipherOutputStream.close();
}
完整⽰例代码:(对⽂件进⾏加解密)
public class MyClass {
private static byte[] key;
private static String IV ="ikldokiujujikijo";
public static void main(String[] args){
createKey();
try{
encodeFile("C:\\Users\\zhaol\\Desktop\\SerurityUtil.java","C:\\Users\\zhaol\\Desktop\\1.java");
decoder("C:\\Users\\zhaol\\Desktop\\1.java","C:\\Users\\zhaol\\Desktop\\2.java");
}catch(NoSuchPaddingException e){
e.printStackTrace();
}catch(NoSuchAlgorithmException e){
e.printStackTrace();
}catch(InvalidAlgorithmParameterException e){
e.printStackTrace();
}catch(InvalidKeyException e){
e.printStackTrace();
}catch(IOException e){
}catch(IOException e){
e.printStackTrace();
}
}
private static void createKey(){
try{
KeyGenerator keyGenerator = Instance("AES");
keyGenerator.init(128,new SecureRandom());
SecretKey secretKey = ateKey();
key = Encoded();
}catch(NoSuchAlgorithmException e){
e.printStackTrace();
}
}
/*
*加密
*/
private static void encodeFile(String path, String encodePath)throws NoSuchPaddingException, NoSuchAlgorithmException, InvalidAlgorithmParameter Exception, InvalidKeyException, IOException {
SecretKeySpec keySpec =new SecretKeySpec(key,"AES");
Cipher cipher = Instance("AES/CBC/PKCS5Padding");
cipher.init(Cipher.ENCRYPT_MODE,keySpec,new Bytes(StandardCharsets.UTF_8)));
FileInputStream inputStream =new FileInputStream(path);
CipherInputStream cipherInputStream =new CipherInputStream(inputStream,cipher);
FileOutputStream outputStream =new FileOutputStream(encodePath);
int length;
byte[] b =new byte[1024];
while((length = ad(b))!=-1){
outputStream.write(b,0,length);
outputStream.flush();
}
cipherInputStream.close();
outputStream.close();
}
/*
* 解密
*/
private static void decoder(String encode,String decoder)throws NoSuchPaddingException, NoSuchAlgorithmException, InvalidAlgorithmParameterExce ption, InvalidKeyException, IOException {
SecretKeySpec keySpec =new SecretKeySpec(key,"AES");
Cipher cipher = Instance("AES/CBC/PKCS5Padding");
cipher.init(Cipher.DECRYPT_MODE,keySpec,new Bytes(StandardCharsets.UTF_8)));
FileInputStream inputStream =new FileInputStream(encode);
FileOutputStream outputStream =new FileOutputStream(decoder);
CipherOutputStream cipherOutputStream =new CipherOutputStream(outputStream,cipher);
int length;
byte[] bytes =new byte[1024];
while((length = ad(bytes))!=-1){
cipherOutputStream.write(bytes,0,length);
}
inputStream.close();
cipherOutputStream.close();
}
}

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。