RSA非对称加密

原文链接:http://www.codefans.net/articles/1890.shtml


在Java中使用RSA加密算法的一个例子,觉得挺不错吧,简单完整的代码,通过这个代码你将对RSA加密算法在Java中的实现方法有一个初步的了解,这个类,你可以直接使用,水平高的,就自己修改完善下代码。


 

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
import java.math.BigInteger;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.NoSuchAlgorithmException;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.interfaces.RSAPrivateKey;
import java.security.interfaces.RSAPublicKey;

/**
 * 使用RSA进行非对称加密
 * 
 */
public class RSA_Demo {

	public static void main(String[] args) {

		generateKey();
		encrypt();
		decrypt();
	}

	public static void generateKey() {
		try {
			KeyPairGenerator kpg = KeyPairGenerator.getInstance("RSA");
			kpg.initialize(1024);
			KeyPair kp = kpg.genKeyPair();
			PublicKey pbkey = kp.getPublic();
			PrivateKey prkey = kp.getPrivate();
			// 保存公钥
			FileOutputStream f1 = new FileOutputStream("pubkey.dat");
			ObjectOutputStream b1 = new ObjectOutputStream(f1);
			b1.writeObject(pbkey);
			// 保存私钥
			FileOutputStream f2 = new FileOutputStream("privatekey.dat");
			ObjectOutputStream b2 = new ObjectOutputStream(f2);
			b2.writeObject(prkey);
		} catch (NoSuchAlgorithmException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public static void encrypt() {
		try {
			String s = "Hello World!";
			// 获取公钥及参数e,n
			FileInputStream f = new FileInputStream("pubkey.dat");
			ObjectInputStream b = new ObjectInputStream(f);
			RSAPublicKey pbk = (RSAPublicKey) b.readObject();
			BigInteger e = pbk.getPublicExponent();
			BigInteger n = pbk.getModulus();
			System.out.println("e= " + e);
			System.out.println("n= " + n);
			// 获取明文m
			byte ptext[] = s.getBytes("UTF-8");
			BigInteger m = new BigInteger(ptext);
			// 计算密文c
			BigInteger c = m.modPow(e, n);
			System.out.println("c= " + c);
			// 保存密文
			String cs = c.toString();
			BufferedWriter out = new BufferedWriter(new OutputStreamWriter(
					new FileOutputStream("encrypt.dat")));
			out.write(cs, 0, cs.length());
			out.close();
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public static void decrypt() {
		try {
			// 读取密文
			BufferedReader in = new BufferedReader(new InputStreamReader(
					new FileInputStream("encrypt.dat")));
			String ctext = in.readLine();
			BigInteger c = new BigInteger(ctext);
			// 读取私钥
			FileInputStream f = new FileInputStream("privatekey.dat");
			ObjectInputStream b = new ObjectInputStream(f);
			RSAPrivateKey prk = (RSAPrivateKey) b.readObject();
			BigInteger d = prk.getPrivateExponent();
			// 获取私钥参数及解密
			BigInteger n = prk.getModulus();
			System.out.println("d= " + d);
			System.out.println("n= " + n);
			BigInteger m = c.modPow(d, n);
			// 显示解密结果
			System.out.println("m= " + m);
			byte[] mt = m.toByteArray();
			System.out.println("PlainText is ");
			for (int i = 0; i < mt.length; i++) {
				System.out.print((char) mt[i]);
			}
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值