最近在弄一个数字信封,对消息进行加密
然后将加密后的消息发送给服务器其中 已经从服务器段获得公钥跟私钥
请问如何用RSA进行加密 给个算法JAVA版本 谢谢String msg = "需要加密的字符串";
String public_key = "公钥"; //服务器给我的公钥是一个很长的字符串
String private_key = "私钥"; //服务器给我的私钥也是很长的一个字符串
请高人写个方法 将消息用RSA加密 并返回一个字符串 至于方法签名 你来定网上太多资料 什么欧拉定理 什么素数之类的 看到头都大了
然后将加密后的消息发送给服务器其中 已经从服务器段获得公钥跟私钥
请问如何用RSA进行加密 给个算法JAVA版本 谢谢String msg = "需要加密的字符串";
String public_key = "公钥"; //服务器给我的公钥是一个很长的字符串
String private_key = "私钥"; //服务器给我的私钥也是很长的一个字符串
请高人写个方法 将消息用RSA加密 并返回一个字符串 至于方法签名 你来定网上太多资料 什么欧拉定理 什么素数之类的 看到头都大了
解决方案 »
- JAVA中线程同步方法有哪些?
- 关闭jfreechart整个软件就跟着也关了。。。
- 请问怎样让 if(i!=i)成立???
- 求新手JAVA编程软件
- 仙人指路!请给小弟推荐几个学习java的网站,就是类似知识库的那种!不胜感激
- 5。1快要放假了,突然想起了原来的老版主-路人甲,曾经为我解答问题,现在消失了,大家知道它跑拿去了吗?
- 现在读unicode格式的文件进行操作,我使用了下面的方法,但如何按unicode格式操作,请帮忙看一下。
- 算法题:找出一个数组中前K个大数的位置
- 谁在用JBuilder开发Applet,能不能告诉我你倒底是怎么让IE显示出Applet的
- java RTTI 问题
- 关于容器的一个小问题
- 一个多线程的问题。。。
import java.io.BufferedWriter;
import java.io.FileInputStream;
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.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.SecureRandom;
import java.security.interfaces.RSAPrivateKey;
import java.security.interfaces.RSAPublicKey;public class RSA
{
public static void main(String[] args) throws NoSuchAlgorithmException, IOException, ClassNotFoundException
{
KeyPair pair = generateKeyPair();
PrivateKey privateKey = pair.getPrivate();
PublicKey publicKey = pair.getPublic(); // Save Pirvate Key
FileOutputStream f1 = new FileOutputStream("private.ppk");
ObjectOutputStream b1 = new ObjectOutputStream(f1);
b1.writeObject(privateKey); // Save Public Key
FileOutputStream f2 = new FileOutputStream("public.ppk");
ObjectOutputStream b2 = new ObjectOutputStream(f2);
b2.writeObject(publicKey); encrypt();
decrypt(); } public static void encrypt() throws IOException, ClassNotFoundException
{
String msg = "Hello World!"; FileInputStream f = new FileInputStream("public.ppk");
ObjectInputStream b = new ObjectInputStream(f);
RSAPublicKey pbk = (RSAPublicKey) b.readObject(); // RSA算法是使用整数进行加密的,在RSA公钥中包含有两个整数信息:e和n。对于明文数字m,计算密文的公式是m的e次方再与n求模。
BigInteger e = pbk.getPublicExponent();
BigInteger n = pbk.getModulus(); // 获取明文的大整数
byte ptext[] = msg.getBytes("utf-8");
BigInteger m = new BigInteger(ptext); // 加密明文
BigInteger c = m.modPow(e, n);
// 打印密文c
System.out.println("c= " + c);
// 将密文以字符串形式保存在文件中
String cs = c.toString();
BufferedWriter out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream("encrypt.dat"), "utf-8"));
out.write(cs, 0, cs.length());
out.close(); } public static void decrypt() throws IOException, ClassNotFoundException
{
BufferedReader in = new BufferedReader(new InputStreamReader(new FileInputStream("encrypt.dat"), "utf-8"));
String ctext = in.readLine();
BigInteger c = new BigInteger(ctext);
// 获取私钥
FileInputStream f = new FileInputStream("private.ppk");
ObjectInputStream b = new ObjectInputStream(f);
RSAPrivateKey prk = (RSAPrivateKey) b.readObject();
// 获取私钥的参数d,n
BigInteger d = prk.getPrivateExponent();
BigInteger n = prk.getModulus();
// 解密明文
BigInteger m = c.modPow(d, n);
// 计算明文对应的字符串并输出。
byte[] mt = m.toByteArray();
System.out.println("PlainText is " + new String(mt, "utf-8"));
} public static KeyPair generateKeyPair() throws NoSuchAlgorithmException
{
KeyPairGenerator keyPairGen = KeyPairGenerator.getInstance("RSA");
final int KEY_SIZE = 1024;
keyPairGen.initialize(KEY_SIZE, new SecureRandom());
KeyPair keyPair = keyPairGen.genKeyPair();
return keyPair;
}}
最近处理RSA算法,找了一些相关的资料和代码,整理了一下,汇总成这篇文章。http://www.zeali.net/entry/58
http://www.zeali.net/entry/58
可能不同的RSA规范会存在不兼容的情况,你可以把你的string转化为ByteArrayInputStream的形式,然后用ObjectInputStream的形式包装试试,再把它转换为PrivateKey和PublicKey,不过我估计行不通。
我猜是这样的 服务器在生成密钥对的时候 会不会按照某种编码方式将密钥对转换成字符串了呢???
如果这样 那我们得将字符串转换成相应的密钥对 还有一个问题 我要就加密的信息超过一定长度了 就会抛出异常 说加密的信息不能超过117byte 这个问题如何解决呢