作业帮 > 综合 > 作业

RSA算法加解密 写出写出简单加解密过程给我

来源:学生作业帮 编辑:神马作文网作业帮 分类:综合作业 时间:2024/11/10 20:20:46
RSA算法加解密 写出写出简单加解密过程给我
RSA算法加解密 写出写出简单加解密过程给我
import java.security.*;
import java.security.interfaces.*;
import java.math.*;
import java.io.*;
public class Password_Test {
public static void main(String[] args) {
try {
new Password_Test();
Encryption_RSA();
} catch (Exception e) {
e.printStackTrace();
}
}
public Password_Test() throws Exception {// 构造方法 创建公钥和私钥
KeyPairGenerator kpg = KeyPairGenerator.getInstance("RSA");//生成实现RSA算法的KeyPairGenerator对象.
kpg.initialize(1024);// 初始化确定密钥的大小
KeyPair kp = kpg.genKeyPair();// 生成密钥对
PublicKey pbkey = kp.getPublic();// 创建公钥
PrivateKey prkey = kp.getPrivate();// 创建私钥
// 保存公钥
FileOutputStream file1 = new FileOutputStream("D:/temp/Skey_RSA_pub.dat");
ObjectOutputStream ob1 = new ObjectOutputStream(file1);//创建ObjectOutputStream对象
ob1.writeObject(pbkey); //将指定的对象写入 ObjectOutputStream.
// 保存私钥
FileOutputStream file2 = new FileOutputStream("D:/temp/Skey_RSA_priv.dat");
ObjectOutputStream ob2 = new ObjectOutputStream(file2);
ob2.writeObject(prkey);
}
public static void Encryption_RSA() throws Exception {
System.out.println("根据公钥生成密文:"+"\n");
String string = "I am a student";
// 获取公钥及参数e,n
FileInputStream f_in = new FileInputStream("D:/temp/Skey_RSA_pub.dat");
ObjectInputStream o_in = new ObjectInputStream(f_in);
RSAPublicKey pbk = (RSAPublicKey) o_in.readObject();
BigInteger e = pbk.getPublicExponent();//返回此公钥的指数
BigInteger n = pbk.getModulus();//返回此公钥的模
System.out.println("公钥的指数 e= " + e);
System.out.println("公钥的模 n= " + n);
// 明文 bit
byte bt[] = string.getBytes("UTF8");
BigInteger bit = new BigInteger(bt);
// 计算密文c,打印
BigInteger mi = bit.modPow(e, n);//生成密文
System.out.println("生成密文为: " + mi+"\n\n");//打印密文
// 保存密文
String save = mi.toString();
BufferedWriter out = new BufferedWriter(new OutputStreamWriter(
new FileOutputStream("D:/temp/Enc_RSA.dat")));
out.write(save, 0, save.length());
out.close();
Decrypt_RSA();
}
public static void Decrypt_RSA() throws Exception {
System.out.println("根据私钥破解密文:"+"\n");
// 读取密文
BufferedReader in = new BufferedReader(new InputStreamReader(
new FileInputStream("D:/temp/Enc_RSA.dat")));
String ctext = in.readLine();
BigInteger mi = new BigInteger(ctext);
// 读取私钥
FileInputStream f = new FileInputStream("D:/temp/Skey_RSA_priv.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 jie = mi.modPow(d, n);//进行解密操作
System.out.println("m= " + jie); // 显示解密结果
byte[] mt = jie.toByteArray();
System.out.println("解密后的文本内容为: ");
for (int i = 0; i < mt.length; i++) {
System.out.print((char) mt[i]);
}
}
}