package com.ethan.security;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.security.InvalidKeyException;
import java.security.Key;
import java.security.NoSuchAlgorithmException;
import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.KeyGenerator;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.SecretKey;
public class SecretKeyTest {
/**
* 对称加密---------------》密钥对密钥
* key secretKey
* 非对称加密----------------->公钥 私钥
* publicKey/PrivateKey
* @param args
* @throws ClassNotFoundException
* @throws IOException
* @throws BadPaddingException
* @throws IllegalBlockSizeException
* @throws NoSuchPaddingException
* @throws NoSuchAlgorithmException
* @throws InvalidKeyException
*/
public static void main(String[] args) throws InvalidKeyException, NoSuchAlgorithmException, NoSuchPaddingException, IllegalBlockSizeException, BadPaddingException, IOException, ClassNotFoundException {
// TODO Auto-generated method stub
//secretEncrypt();
secretDecrypt();
}
public static void secretEncrypt() {
//加密类
try {
Cipher cipher = Cipher.getInstance("AES");
//密钥
SecretKey key = KeyGenerator.getInstance("AES").generateKey();//得到密钥
//1->加密 2->解密 用常量 英文单词表示数字
cipher.init(Cipher.ENCRYPT_MODE,key);
//把密钥 key写入文件
FileOutputStream fos = new FileOutputStream("ethan_secret.key");
ObjectOutputStream oosSecretKey = new ObjectOutputStream(fos);
oosSecretKey.writeObject(key);
fos.close();
//把加密结果也写进文件
//填充数据
cipher.update("aaa".getBytes());
byte[] results = cipher.doFinal();
System.out.println(new String(results));
FileOutputStream fosData = new FileOutputStream("ethan_data.data");
fosData.write(results);
fosData.close();
/*
* 直接加密 一句话
* cipher.doFinal("aaa".getBytes());
*/
/*//解密
cipher.init(Cipher.DECRYPT_MODE, key);
System.out.println(new String(cipher.doFinal(results)));*/
} catch(Exception e) {
e.printStackTrace();
}
}
private static void secretDecrypt() throws NoSuchAlgorithmException, NoSuchPaddingException, IOException, ClassNotFoundException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException {
//AES 加密的一种算法
Cipher cipher = Cipher.getInstance("AES");
FileInputStream fisKey = new FileInputStream("ethan_secret.key");
ObjectInputStream oisKey = new ObjectInputStream(fisKey);
//读取到 Key 对象 Key-----------is interface
Key key = (Key) oisKey.readObject();
oisKey.close();
fisKey.close();
cipher.init(Cipher.DECRYPT_MODE, key);
//拿到 要解密的内容
FileInputStream fisDat = new FileInputStream("ethan_data.data");
// ByteArrayInputStream bais = new ByteArrayInputStream(arg0)
ByteArrayOutputStream baos = new ByteArrayOutputStream();
copyStream(fisDat,baos);
/*
* 第二种 read 数据的方法
* byte[] src = new byte[1024];
*int len = fisDat.read(src);
* int total = 0;
* 下边不能是 while(len!=-1)因为如果read(byte[],offset,len) len=0了,则return 0;
* 不等于-1,则进入死循环
* while(total<src.length) {
* total += len;
* 从total处 开始接着读 到 src.length-total
* len = fisDat.read(src,total,src.length-total);
*
* }
*
* byte[] result = cipher.doFinal(src);
* fisDat.close();
*/
byte[] result = cipher.doFinal(baos.toByteArray());
//记得关闭流
fisDat.close();
baos.close();
System.out.println(new String(result));
}
private static void copyStream(InputStream ips,OutputStream ops) {
byte[] buffer = new byte[1024];
int len = 0;
try {
while((len=ips.read(buffer))!=-1) {
ops.write(buffer,0,len);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
永远缅怀 张孝祥老师!
java 之 加密解密学习示例
最新推荐文章于 2024-04-05 09:47:29 发布