java 的RSA加密, 怎么在 .net(C#) 实现

陈阿弟 2016-01-31 11:45:16
java的代码,如下:


BigInteger Module = new BigInteger("module数据", 16);
BigInteger publicExponent = new BigInteger("exponent", 16);
RSAPublicKeySpec localRSAPublicKeySpec = new RSAPublicKeySpec(Module, publicExponent);
KeyFactory fac = java.security.KeyFactory.getInstance("RSA");
PublicKey pk = fac.generatePublic(localRSAPublicKeySpec);

String text = "123"; //明文
Cipher localCipher = Cipher.getInstance("RSA/ECB/NoPadding");
localCipher.init(1, pk);
byte[] arr = localCipher.doFinal(text.getBytes()); //生成的密文
...全文
261 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
HerveyHall 2016-02-01
  • 打赏
  • 举报
回复
引用 4 楼 陈阿弟的回复:
.net 自带的RSA库加密结果每次都会不一样..
貌似只有这自带的了
陈阿弟 2016-01-31
  • 打赏
  • 举报
回复
.net 自带的RSA库加密结果每次都会不一样..
陈阿弟 2016-01-31
  • 打赏
  • 举报
回复
引用 2 楼 hhv_01 的回复:
using System;
using System.Text;
using System.Security.Cryptography;

namespace RSA算法
{
    class Program
    {
        static void Main(string[] args)
        {
            string s = "helloworld123467";
            UnicodeEncoding ue = new UnicodeEncoding();
            byte[] b1 = ue.GetBytes(s);
            byte[] b2;
            byte[] b3;
            RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();
            b2 = rsa.Encrypt(b1, true);//加密
            Console.WriteLine(ue.GetString(b2));

            b3 = rsa.Decrypt(b2, true);//解密
            Console.WriteLine(ue.GetString(b3));

            Console.ReadLine();
        }
    }
}
BigInteger Module = new BigInteger("module数据", 16); BigInteger publicExponent = new BigInteger("exponent", 16); 这两个没用上....
HerveyHall 2016-01-31
  • 打赏
  • 举报
回复
using System;
using System.Text;
using System.Security.Cryptography;

namespace RSA算法
{
    class Program
    {
        static void Main(string[] args)
        {
            string s = "helloworld123467";
            UnicodeEncoding ue = new UnicodeEncoding();
            byte[] b1 = ue.GetBytes(s);
            byte[] b2;
            byte[] b3;
            RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();
            b2 = rsa.Encrypt(b1, true);//加密
            Console.WriteLine(ue.GetString(b2));

            b3 = rsa.Decrypt(b2, true);//解密
            Console.WriteLine(ue.GetString(b3));

            Console.ReadLine();
        }
    }
}
xuggzu 2016-01-31
  • 打赏
  • 举报
回复
看你的代码里很明显带有加密库,自然使用起来很简单; C#据我所知本身是不带加密库的,要使用RSA加密只有到网上找是否有人或公司写过加密类,如果有现成的拿来主义即可。 (自己写加密类库是不明智的,除非你这方面有很深研究,时间也很多)

111,129

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Creator Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧