RSA加密解密

以下代码是从《java核心技术卷Ⅱ》第九章中修改而来的

import java.io.DataInputStream;
import java.io.DataOutputStream;
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.GeneralSecurityException;
import java.security.Key;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.SecureRandom;

import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;

public class RSATest {
private static final int KEYSIZE = 512;

/**
* 生成公钥和私钥
*
* @param priPath
* 私钥的位置
* @param pubPath
* 公钥的位置
* @throws Exception
*/
private static void genkey(String priPath, String pubPath) throws Exception {
KeyPairGenerator pairgen = KeyPairGenerator.getInstance(“RSA”);
SecureRandom random = new SecureRandom();
pairgen.initialize(KEYSIZE, random);
KeyPair keyPair = pairgen.generateKeyPair();
ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(
pubPath));
out.writeObject(keyPair.getPublic());
out.close();
out = new ObjectOutputStream(new FileOutputStream(priPath));
out.writeObject(keyPair.getPrivate());
out.close();
}

/**
* 加密
*
* @param srcPath
* 要加密的源文件的位置
* @param destPath
* 加密后的文件的位置
* @param keyPath
* 公钥/或私钥的位置
* @throws Exception
*/
private static void encrypt(String srcPath, String destPath, String keyPath)
throws Exception {
KeyGenerator keygen = KeyGenerator.getInstance(“AES”);
SecureRandom random = new SecureRandom();
keygen.init(random);
SecretKey key = keygen.generateKey();

// wrap with RSA public key
ObjectInputStream keyIn = new ObjectInputStream(new FileInputStream(
keyPath));
Key pkey = (Key) keyIn.readObject();
keyIn.close();

Cipher cipher = Cipher.getInstance(“RSA”);
cipher.init(Cipher.WRAP_MODE, pkey);
byte[] wrappedKey = cipher.wrap(key);
DataOutputStream out = new DataOutputStream(new FileOutputStream(
destPath));
out.writeInt(wrappedKey.length);
out.write(wrappedKey);

InputStream in = new FileInputStream(srcPath);
cipher = Cipher.getInstance(“AES”);
cipher.init(Cipher.ENCRYPT_MODE, key);
crypt(in, out, cipher);
in.close();
out.close();
}

/**
* 解密
*
* @param srcPath
* 要解密的源文件的位置
* @param destPath
* 解密后的文件的位置
* @param keyPath
* 私钥/或公钥的位置
* @throws Exception
*/
private static void decrypt(String srcPath, String destPath, String keyPath)
throws Exception {
DataInputStream in = new DataInputStream(new FileInputStream(srcPath));
int length = in.readInt();
byte[] wrappedKey = new byte[length];
in.read(wrappedKey, 0, length);

// unwrap with RSA private key
ObjectInputStream keyIn = new ObjectInputStream(new FileInputStream(
keyPath));
Key pkey = (Key) keyIn.readObject();
keyIn.close();

Cipher cipher = Cipher.getInstance(“RSA”);
cipher.init(Cipher.UNWRAP_MODE, pkey);
Key key = cipher.unwrap(wrappedKey, “AES”, Cipher.SECRET_KEY);

OutputStream out = new FileOutputStream(destPath);
cipher = Cipher.getInstance(“AES”);
cipher.init(Cipher.DECRYPT_MODE, key);

crypt(in, out, cipher);
in.close();
out.close();
}

public static void main(String[] arg) throws Exception {
// 第一步生成公钥和私钥
genkey(“D:\se\pub.txt”, “D:\se\pri.txt”);
// 第二步,用私钥加密
encrypt(“D:\se\1.txt”, “D:\se\2.txt”, “D:\se\pri.txt”);
// 第三步,用公钥解密
decrypt(“D:\se\2.txt”, “D:\se\0.txt”, “D:\se\pub.txt”);

// 第二步,用公钥加密
encrypt(“D:\se\1.txt”, “D:\se\3.txt”, “D:\se\pub.txt”);
// 第三步,用私钥解密
decrypt(“D:\se\3.txt”, “D:\se\4.txt”, “D:\se\pri.txt”);
}

private static void crypt(InputStream in, OutputStream out, Cipher cipher)
throws IOException, 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);
}
}

你可能感兴趣的:(加密,rsa,解密)