📄 aes.java
字号:
import java.io.*;
import java.security.*;
import javax.crypto.*;
import com.sun.crypto.provider.SunJCE;
import java.math.BigInteger;
public class AES
{
public final static String filename = "secret.key";
/*
生成密匙
*/
public static void createKey() {
try
{
KeyGenerator keygen = KeyGenerator.getInstance("AES");
SecureRandom random = new SecureRandom();
keygen.init(random);
SecretKey key = keygen.generateKey();
byte[] t = key.getEncoded();
BigInteger tem = new BigInteger(t);
System.out.println("密匙:"+tem);
writeKey(key);
}
catch (Exception e)
{
e.printStackTrace();
}
}
/*
加密
*/
public static boolean Encrypt(String mingpath, String mipath) {
try
{
FileInputStream fin = new FileInputStream(mingpath);
FileOutputStream fout = new FileOutputStream(mipath);
Cipher cipher = Cipher.getInstance("AES");
Key key = (Key)getKey();
if(key==null){
System.out.println("请先生成密匙!!");
return false;
}
cipher.init(Cipher.ENCRYPT_MODE,key);
crypt(fin,fout,cipher,true);
fin.close();
fout.close();
return true;
}
catch (Exception e)
{
// e.printStackTrace();
System.out.println("待加密的ming.txt无法找到请确认其存在!");
return false;
}
}
/*
解密
*/
public static boolean Decrypt(String mipath, String demingpath) {
try
{
FileInputStream fin = new FileInputStream(mipath);
FileOutputStream fout = new FileOutputStream(demingpath);
Cipher cipher = Cipher.getInstance("AES");
Key key = (Key)getKey();
if(key==null) {
System.out.println("请先生成密匙!!");
return false;
}
cipher.init(Cipher.DECRYPT_MODE,key);
crypt(fin,fout,cipher,false);
fin.close();
fout.close();
return true;
}
catch (Exception e)
{
// e.printStackTrace();
System.out.println("待解密的mi.txt无法找到请确认其存在!");
return false;
}
}
public static void crypt(InputStream in, OutputStream out, Cipher cipher,boolean flag) {
try
{
int blockSize = cipher.getBlockSize();
int outputSize = cipher.getOutputSize(blockSize);
byte[] inBytes = new byte[blockSize];
byte[] outBytes = new byte[outputSize];
int inLength = 0;
boolean more = true;
if(flag) System.out.print("密文的char值:");
while(more){
inLength = in.read(inBytes);
if(inLength == blockSize){
int outLength = cipher.update(inBytes, 0,blockSize,outBytes);
out.write(outBytes,0,outLength);
if(flag) {
for(int i=0;i<outLength;i++)
System.out.print(outBytes[i]);
}
}
else more = false;
}
if(inLength > 0){
outBytes = cipher.doFinal(inBytes,0,inLength);
}
else{
outBytes = cipher.doFinal();
}
if(flag){
for(int i=0;i<outBytes.length;i++)
System.out.print(outBytes[i]);
System.out.print("\n");
}
out.write(outBytes);
}
catch (Exception e)
{
e.printStackTrace();
}
}
/**
* 将密匙作为对象写入文件
* @param obj
*/
private static void writeKey(Object obj) {
File file = new File(filename);
try {
FileOutputStream out = new FileOutputStream(file);
ObjectOutputStream objout = new ObjectOutputStream(out);
objout.writeObject(obj);
out.close();
objout.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/*
从文件中取出密匙
*/
public static Object getKey() {
File file = new File(filename);
Security.addProvider(new SunJCE());
try {
FileInputStream fin = new FileInputStream(file);
ObjectInputStream oin = new ObjectInputStream(fin);
Object obj = oin.readObject();
return obj;
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
} catch (IOException e) {
// TODO Auto-generated catch block
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
}
return null;
}
/*
从文件中读取字符串
*/
public static String getString(String path) {
File file = new File(path);
StringBuffer buffer = new StringBuffer();
byte[] bytes = new byte[1024];
int i = 0;
try
{
FileInputStream fin = new FileInputStream(file);
while((i=fin.read(bytes))>0) {
buffer.append(new String(bytes,0,i));
}
fin.close();
}
catch (Exception e)
{
e.printStackTrace();
}
return buffer.toString();
}
/*
将字符串写入文件中
*/
public static void storeString(String str, String path) {
File file = new File(path);
try
{
FileOutputStream fout = new FileOutputStream(file);
fout.write(str.getBytes());
fout.close();
}
catch (Exception e)
{
e.printStackTrace();
}
}
public static void main(String[] args) {
String in = null;
BufferedReader read = new BufferedReader(new InputStreamReader(System.in));
System.out.println("可选输入:");
System.out.println("1:生成密匙。2:加密。3:解密。4:退出。");
try
{
while(true){
in = read.readLine();
if(in.equals("1")){
System.out.println("生成密匙中.....");
createKey();
System.out.println("密匙已经生成!");
}else if(in.equals("2")){
System.out.println("加密文件中.....");
boolean f = Encrypt("ming.txt","mi.txt");
if(f){
System.out.println("加密成功!");
}
} else if(in.equals("3")){
System.out.println("解密文件中......");
boolean f= Decrypt("mi.txt","deming.txt");
if(f){
System.out.println("解密成功!明文在文件deming.txt中");
}
} else if(in.equals("4")){
System.out.println("Byte Byte!");
break;
} else{
System.out.println("输入有误,请按提示键操作!");
}
}
}
catch (Exception e)
{
e.printStackTrace();
System.out.println("抱歉,发生异常了!");
return ;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -