2 Star 0 Fork 0

CS-IMIS-23/fwq20172303_Programming

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
Enc_RSA.java 1.20 KB
一键复制 编辑 原始数据 按行查看 历史
package week10.ExperimentalLesson.RSA;
import java.security.*;
import java.security.spec.*;
import javax.crypto.*;
import javax.crypto.spec.*;
import javax.crypto.interfaces.*;
import java.security.interfaces.*;
import java.math.*;
import java.io.*;
public class Enc_RSA{
public static void main(String args[]) throws Exception{
String s="Hello World!";
// 获取公钥及参数e,n
FileInputStream f=new FileInputStream("Skey_RSA_pub.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("UTF8");
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("Enc_RSA.dat")));
out.write(cs,0,cs.length( ));
out.close( );
}
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Java
1
https://gitee.com/CS-IMIS-23/fwq20172303_Programming.git
git@gitee.com:CS-IMIS-23/fwq20172303_Programming.git
CS-IMIS-23
fwq20172303_Programming
fwq20172303_Programming
master

搜索帮助