Java RSA加密

RSA类:

public class RSA {
	private Key key = null;
	private static final String PUBLIC_KEY_FILE = "PublicKey";
	/** 指定私钥存放文件 */
	private static final String PRIVATE_KEY_FILE = "PrivateKey";
	/**加密工具*/
	private Cipher encryptCipher = null;
	/** 解密工具 */
	private Cipher decryptCipher = null;

	public void createKey() throws NoSuchAlgorithmException,
			FileNotFoundException, IOException {
		/** 为RSA算法创建一个KeyPairGenerator对象 */
		KeyPairGenerator keyPairGen = KeyPairGenerator.getInstance("RSA");
		/** 利用上面的随机数据源初始化这个KeyPairGenerator对象 */
		keyPairGen.initialize(1024);
		/** 生成密匙对 */
		KeyPair keyPair = keyPairGen.generateKeyPair();
		/** 得到公钥 */
		Key publicKey = keyPair.getPublic();
		/** 得到私钥 */
		Key privateKey = keyPair.getPrivate();

		/** 用对象流将生成的密钥写入文件 */
		ObjectOutputStream oos1 = new ObjectOutputStream(new FileOutputStream(
				PUBLIC_KEY_FILE));
		ObjectOutputStream oos2 = new ObjectOutputStream(new FileOutputStream(
				PRIVATE_KEY_FILE));
		oos1.writeObject(publicKey);
		oos2.writeObject(privateKey);
		/** 清空缓存,关闭文件输出流 */
		oos1.close();
		oos2.close();
	}

	private Key readKey(String keyfilepath) throws Exception, ClassNotFoundException {
		ObjectInputStream objectInputStream = new ObjectInputStream(new FileInputStream(keyfilepath));
	    Key key = (Key) objectInputStream.readObject();
		return key;
	}
	public void initialize_encryptkey(String keyfilepath) throws Exception {
		encryptCipher = Cipher.getInstance("RSA");
		if(key == null) {
			key = readKey(keyfilepath);
		}
		encryptCipher.init(Cipher.ENCRYPT_MODE, key);
		key = null;
	}
	public byte[] encrypt(byte[] b) throws Exception {
		
		return encryptCipher.doFinal(b);
	}
	public void initalize_dencryptkey(String keyfilepath) throws Exception {
		decryptCipher = Cipher.getInstance("RSA");
		if (key == null) {
			key = readKey(keyfilepath);
		}
		decryptCipher.init(Cipher.DECRYPT_MODE, key);
		key = null;
	}
	public byte[] decrypt(byte[] buf) throws Exception {
		
		return decryptCipher.doFinal(buf);	
	}
    
	public void encryptFile(String sourceFileName, String diminationFileName) throws Exception {
		FileInputStream infile = new FileInputStream(sourceFileName);
		FileOutputStream outfile = new FileOutputStream(diminationFileName);
		byte[] b = new byte[117];
		byte[] newbyte;
		int a = infile.read(b);
		while (a != -1) {
			if(a == b.length) {
				newbyte = b;
			} else {
				newbyte = new byte[a];
				for(int i = 0; i<a; i++) {
					newbyte[i] = b[i];
				}
			}
			byte[] ee = encrypt(newbyte);
			outfile.write(ee);
			a = infile.read(b);
		}
		infile.close();
		outfile.close();
	}
    public void dencryptFile(String sourceFileName, String diminationFileName) throws Exception {
    	FileInputStream infile = new FileInputStream(sourceFileName);
		FileOutputStream outfile = new FileOutputStream(diminationFileName);
		byte[] b = new byte[128];
		byte[] newbyte;
		int a = infile.read(b);
		while (a != -1) {
			if(a == b.length) {
				newbyte = b;
			} else {
				newbyte = new byte[a];
				for(int i = 0; i<a; i++) {
					newbyte[i] = b[i];
				}
			}
			byte[] ee = decrypt(newbyte);
			outfile.write(ee);
			a = infile.read(b);
		}
		infile.close();
		outfile.close();
	}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值