java rsa enc 源码_Enc_RSA.java

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( );

}

}

一键复制

编辑

Web IDE

原始数据

按行查看

历史

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值