沒什么好說的,一切盡在代碼中。
import java.io.*;
import java.security.*;
import javax.crypto.*;
/**
* AES算法生成密鑰和對文件加解密的實現。
* @author Li Xiaofeng
* AES--DES算法的后續版本,由于DES算法可以通過窮舉法破譯,因此不推薦使用。
*/
public class AESImpl {
//產生AES密鑰
public void createKey() {
try{
KeyGenerator keygen = KeyGenerator.getInstance("AES");
SecureRandom random = new SecureRandom();
keygen.init(random);
SecretKey key = keygen.generateKey();
ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream("aesKey.txt"));
out.writeObject(key);
out.close();
}catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}catch (IOException e) {
e.printStackTrace();
}
}
public void run(String code){
int mode = Cipher.ENCRYPT_MODE;
String inputFileName = "encode.txt";//要加密的文件名
String outputFileName = "aesDecode.txt";//加密后的文件名
if("DECODE".equals(code)){
mode = Cipher.DECRYPT_MODE;
inputFileName = "aesDecode.txt";//要解密的文件名
outputFileName = "encode.txt";//解密后的文件名
}
try{
//讀入AES密鑰文件
ObjectInputStream keyin = new ObjectInputStream(new FileInputStream("aesKey.txt"));
Key key = (Key)keyin.readObject();
keyin.close();
InputStream in = new FileInputStream(inputFileName);
OutputStream out = new FileOutputStream(outputFileName);
Cipher cipher = Cipher.getInstance("AES");
cipher.init(mode, key);
//加解密
crypt(in,out,cipher);
in.close();
out.close();
}catch (Exception e) {
e.printStackTrace();
}
}
public static void crypt(InputStream in, OutputStream out, Cipher cipher) throws IOException, ShortBufferException, GeneralSecurityException {
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;
while(more){
inLength = in.read(inBytes);
if(inLength == blockSize){
int outLength = cipher.update(inBytes, 0,blockSize,outBytes);
out.write(outBytes,0,outLength);
}
else more = false;
}
if(inLength > 0){
outBytes = cipher.doFinal(inBytes,0,inLength);
}
else{
outBytes = cipher.doFinal();
}
out.write(outBytes);
}
public static void main(String[] args){
AESImpl _aes = new AESImpl();
//_aes.createKey();
_aes.run("DECODE");
}
}
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.security.Key;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.SecureRandom;
import javax.crypto.Cipher;
/**
* 1、RSA算法生成密鑰對。
* 2、用生成的公共密鑰對AES密鑰加密,和用AES密鑰加密的內容放到一個文件中。
* 3、用生成的私有密鑰對文件中的AES密鑰解密,并用解密的AES密鑰解密文件的內容。
* @author Li Xiaofeng
*
*/
public class RSAImpl {
private static final int KEYSIZE = 512;
public void createKey(){
try{
KeyPairGenerator pairgen = KeyPairGenerator.getInstance("RSA");
SecureRandom random = new SecureRandom();
pairgen.initialize(KEYSIZE,random);
KeyPair keypair = pairgen.generateKeyPair();
//生成公共密鑰文件
ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream("publicKey.txt"));
out.writeObject(keypair.getPublic());
out.close();
//生成私有密鑰文件
out = new ObjectOutputStream(new FileOutputStream("privateKey.txt"));
out.writeObject(keypair.getPrivate());
out.close();
}catch (Exception e) {
e.printStackTrace();
}
}
public void encode(){
try{
//讀進公共密鑰,用來加密AES密鑰
ObjectInputStream in = new ObjectInputStream(new FileInputStream("publicKey.txt"));
Key publicKey = (Key)in.readObject();
in.close();
//讀入AES密鑰
in = new ObjectInputStream(new FileInputStream("aesKey.txt"));
Key key = (Key)in.readObject();
in.close();
//加密AES密鑰
Cipher cipher = Cipher.getInstance("RSA");
cipher.init(Cipher.WRAP_MODE,publicKey);
byte[] wrappedKey = cipher.wrap(key);
//加密后的AES寫入文件中
DataOutputStream out = new DataOutputStream(new FileOutputStream("rasDecode.txt"));
out.writeInt(wrappedKey.length);
out.write(wrappedKey);
//讀入要加密的文件
InputStream input = new FileInputStream("encode.txt");
cipher = Cipher.getInstance("AES");
cipher.init(Cipher.ENCRYPT_MODE, key);
//用AES密鑰加密要加密的文件
AESImpl.crypt(input, out, cipher);
input.close();
out.close();
}catch (Exception e) {
e.printStackTrace();
}
}
public void decode(){
try{
//讀入要解密的文件,其中包括用RAS算法加密AES的密鑰內容
DataInputStream in = new DataInputStream(new FileInputStream("rasDecode.txt"));
int length = in.readInt();
byte[] wrappedKey = new byte[length];
in.read(wrappedKey,0,length);
//讀入RSA的私有密鑰文件
ObjectInputStream keyIn = new ObjectInputStream(new FileInputStream("privateKey.txt"));
Key privateKey = (Key)keyIn.readObject();
keyIn.close();
//用RSA私有密鑰解密加密的AES密鑰
Cipher cipher = Cipher.getInstance("RSA");
cipher.init(Cipher.UNWRAP_MODE, privateKey);
Key key = cipher.unwrap(wrappedKey, "AES", Cipher.SECRET_KEY);
OutputStream out = new FileOutputStream("encode.txt");
cipher = Cipher.getInstance("AES");
cipher.init(Cipher.DECRYPT_MODE, key);
//用解密的AES密鑰解密加密的文件
AESImpl.crypt(in, out, cipher);
in.close();
out.close();
}catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args){
RSAImpl _rsa = new RSAImpl();
//_rsa.createKey();
//_rsa.encode();
_rsa.decode();
}
}