📄 aesandrsa.txt
字号:
//讀入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();
}
}
一般加密原理是,由用户共有的公共密钥和传输数据的用户直接有的私有密钥组成。每次加密的时候,都是由一定算法随机生成一对密钥,用密钥加密所需加密的文件,用公钥加密密钥,然后将这些一起传输给目标用户,目标用户将数据处理后,又按照此密钥加密,然后传输回去。
这样的优点别人很难破解,网络上的数据无法定位,而且产生的密钥是随机的。
接下来这个例子就是java核心技术卷2高级特性安全性一章的最后一个例子。
import java.io. * ;
import java.security. * ;
import javax.crypto. * ;
import javax.crypto.interfaces. * ;
import javax.crypto.spec. * ;
public class RSATest ... {
private static final int KEYSIZE = 512 ;
/** */ /**
* @param args
*/
public static void main(String[] args) ... {
// TODO Auto-generated method stub
try ... {
if (args[ 0 ].equals( " -genkey " )) ... {
KeyPairGenerator keyPair = KeyPairGenerator.getInstance( " RSA " );
SecureRandom random = new SecureRandom();
keyPair.initialize(KEYSIZE, random);
KeyPair keyP = keyPair.generateKeyPair();
ObjectOutputStream oos = new ObjectOutputStream( new FileOutputStream(args[ 1 ]));
oos.writeObject(keyP.getPublic());
oos.close();
oos = new ObjectOutputStream( new FileOutputStream(args[ 2 ]));
oos.writeObject(keyP.getPrivate());
oos.close();
} else if (args[ 0 ].equals( " -encrypt " )) ... {
KeyGenerator keygen = KeyGenerator.getInstance( " AES " );
SecureRandom random = new SecureRandom();
keygen.init(random);
SecretKey key = keygen.generateKey();
ObjectInputStream kin = new ObjectInputStream( new FileInputStream(args[ 3 ]));
Key publicKey = (Key)kin.readObject();
kin.close();
Cipher cipher = Cipher.getInstance( " RSA " );
System.out.println(cipher.getAlgorithm());
cipher.init(Cipher.WRAP_MODE,publicKey);
byte [] wrappedKey = cipher.wrap(key);
DataOutputStream out = new DataOutputStream( new FileOutputStream(args[ 2 ]));
out.writeInt(wrappedKey.length);
out.write(wrappedKey);
InputStream in = new FileInputStream(args[ 1 ]);
cipher = Cipher.getInstance( " AES " );
cipher.init(Cipher.ENCRYPT_MODE, key);
crypt(in,out,cipher);
in.close();
out.close();
} else ... {
DataInputStream in = new DataInputStream( new FileInputStream(args[ 1 ]));
int length = in.readInt();
byte []wrappedKey = new byte [length];
in.read(wrappedKey, 0 ,length);
ObjectInputStream keyIn = new ObjectInputStream( new FileInputStream(args[ 3 ]));
Key privateKey = (Key) keyIn.readObject();
keyIn.close();
Cipher cipher = Cipher.getInstance( " RSA " );
cipher.init(Cipher.UNWRAP_MODE, privateKey);
Key key = cipher.unwrap(wrappedKey, " AES " , Cipher.SECRET_KEY);
OutputStream out = new FileOutputStream(args[ 2 ]);
cipher = Cipher.getInstance( " AES " );
cipher.init(Cipher.ENCRYPT_MODE, key);
crypt(in,out,cipher);
in.close();
out.close();
}
} catch (Exception e) ... {
e.printStackTrace();
}
}
private static void crypt(InputStream in, OutputStream out, Cipher cipher) throws IOException,GeneralSecurityException ... {
// TODO Auto-generated method stub
int blockSize = cipher.getBlockSize();
int outputSize = cipher.getOutputSize(blockSize);
byte [] inBytes = new byte [blockSize];
byte [] outputBytes = 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,outputBytes);
out.write(outputBytes, 0 , outLength);
} else
more = false ;
}
if (inLength > 0 )
outputBytes = cipher.doFinal(inBytes, 0 , inLength);
else
outputBytes = cipher.doFinal();
out.write(outputBytes);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -