问个关于rsa加密的问题

阿东zh 2017-03-09 04:55:09
java那边给了 .net这边modulus和exponent 两个参数值
分别是
modulus = “00aaf5bded978566ffd79e6801e0fd922baf4171dcd5797d9dc6c2deb0da1bebc0265be9c345e7ca8c5f9ad220771fdbced7fbc706a3d29f1db337fb7ab21c49ac64319deb2292145b4ffcdb044a8e5339d4a8717183b165fad13083e368c6a03189e95b1df77fc7cda4488eac13098c8a11e57a5b632395a9fb2a4d75b94ed49f”
exponent = “10001”;

加密 字符串 “20001”
得到
87294da04beda7bcde49f0c830a283d58429b7df529ccf815af77e49c0f4ba88b94fd074aafbb8b6df8da93d22a6f31643b050d4bfbd7a225959eb6a1bea6f87d7a31eebd161918b3e842f5ccd2be4dd0e2a31d056fc19f0f3e82ea5ff1f98704db8ebf2d6716e40cea6a4c192d28ba57109a1cee48fc582161970b413d7d102

JAVA在处理的时候 对于加密长度有处理 最大加密长度是117,进行了分段处理
 
Cipher ci = Cipher.getInstance("RSA", new BouncyCastleProvider());
ci.init(Cipher.ENCRYPT_MODE, publicKey);

int inputLen = data.length;
ByteArrayOutputStream out = new ByteArrayOutputStream();
int offSet = 0;
byte[] cache;
int i = 0;
// MAX_ENCRYPT_BLOCK 117
// 对数据分段加密
while (inputLen - offSet > 0)
{
if (inputLen - offSet > 117)
{
cache = ci.doFinal(data, offSet, 117);
} else
{
cache = ci.doFinal(data, offSet, inputLen - offSet);
}
out.write(cache, 0, cache.length);
i++;
offSet = i * 117;
}
byte[] encryptedData = out.toByteArray();
out.close();



我想问的是 .net中 我们应该怎么做 得到和java一样的 加密结果呢
...全文
269 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
lqa475936988 2017-03-10
  • 打赏
  • 举报
回复
阿东,我发现你JAVA代码
Cipher ci = Cipher.getInstance("RSA", new BouncyCastleProvider());
中使用了Bouncy Castle Crypto APIs 该库有Java和C#两个版本,我尝试使用你提供的modulus和exponent 两个参数值在C#版本测试, 结果和你提供的Java版结果一致,测试代码如下:
var modulus =
	            "00aaf5bded978566ffd79e6801e0fd922baf4171dcd5797d9dc6c2deb0da1bebc0265be9c345e7ca8c5f9ad220771fdbced7fbc706a3d29f1db337fb7ab21c49ac64319deb2292145b4ffcdb044a8e5339d4a8717183b165fad13083e368c6a03189e95b1df77fc7cda4488eac13098c8a11e57a5b632395a9fb2a4d75b94ed49f";

	        var exponent = "10001";

	        var input = "20001";
            RsaKeyParameters pubKeySpec = new RsaKeyParameters(
	            false,
	            new BigInteger(modulus, 16),
	            new BigInteger(exponent, 16));

	        //			Cipher c = Cipher.GetInstance("RSA");
	        IBufferedCipher c = CipherUtilities.GetCipher("RSA");

	        AsymmetricKeyParameter pubKey = pubKeySpec;
	        //			c.init(Cipher.ENCRYPT_MODE, pubKey, rand);
	        c.Init(true, pubKey); // new ParametersWithRandom(pubKey, rand));

	        var outBytes = c.DoFinal(Encoding.UTF8.GetBytes(input));

	        var outstring = Hex.ToHexString(outBytes);

	        var excepts = "87294da04beda7bcde49f0c830a283d58429b7df529ccf815af77e49c0f4ba88b94fd074aafbb8b6df8da93d22a6f31643b050d4bfbd7a225959eb6a1bea6f87d7a31eebd161918b3e842f5ccd2be4dd0e2a31d056fc19f0f3e82ea5ff1f98704db8ebf2d6716e40cea6a4c192d28ba57109a1cee48fc582161970b413d7d102";

	        Console.WriteLine(outstring==excepts);

	        Console.WriteLine("modulus:"+modulus);

            Console.WriteLine("exponent:" + exponent);

            Console.WriteLine("InputString:"+input);
            Console.WriteLine("OutputString:" + outstring);
输出结果如下:True modulus:00aaf5bded978566ffd79e6801e0fd922baf4171dcd5797d9dc6c2deb0da1bebc0265be9c345e7ca8c5f9ad220771fdbced7fbc706a3d29f1db337fb7ab21c49ac64319deb2292145b4ffcdb044a8e5339d4a8717183b165fad13083e368c6a03189e95b1df77fc7cda4488eac13098c8a11e57a5b632395a9fb2a4d75b94ed49f exponent:10001 InputString:20001 OutputString:87294da04beda7bcde49f0c830a283d58429b7df529ccf815af77e49c0f4ba88b94fd074aafbb8b6df8da93d22a6f31643b050d4bfbd7a225959eb6a1bea6f87d7a31eebd161918b3e842f5ccd2be4dd0e2a31d056fc19f0f3e82ea5ff1f98704db8ebf2d6716e40cea6a4c192d28ba57109a1cee48fc582161970b413d7d102
阿东zh 2017-03-10
  • 打赏
  • 举报
回复
引用 5 楼 lqa475936988 的回复:
阿东,我发现你JAVA代码
Cipher ci = Cipher.getInstance("RSA", new BouncyCastleProvider());
中使用了Bouncy Castle Crypto APIs 该库有Java和C#两个版本,我尝试使用你提供的modulus和exponent 两个参数值在C#版本测试, 结果和你提供的Java版结果一致,测试代码如下:
var modulus =
	            "00aaf5bded978566ffd79e6801e0fd922baf4171dcd5797d9dc6c2deb0da1bebc0265be9c345e7ca8c5f9ad220771fdbced7fbc706a3d29f1db337fb7ab21c49ac64319deb2292145b4ffcdb044a8e5339d4a8717183b165fad13083e368c6a03189e95b1df77fc7cda4488eac13098c8a11e57a5b632395a9fb2a4d75b94ed49f";

	        var exponent = "10001";

	        var input = "20001";
            RsaKeyParameters pubKeySpec = new RsaKeyParameters(
	            false,
	            new BigInteger(modulus, 16),
	            new BigInteger(exponent, 16));

	        //			Cipher c = Cipher.GetInstance("RSA");
	        IBufferedCipher c = CipherUtilities.GetCipher("RSA");

	        AsymmetricKeyParameter pubKey = pubKeySpec;
	        //			c.init(Cipher.ENCRYPT_MODE, pubKey, rand);
	        c.Init(true, pubKey); // new ParametersWithRandom(pubKey, rand));

	        var outBytes = c.DoFinal(Encoding.UTF8.GetBytes(input));

	        var outstring = Hex.ToHexString(outBytes);

	        var excepts = "87294da04beda7bcde49f0c830a283d58429b7df529ccf815af77e49c0f4ba88b94fd074aafbb8b6df8da93d22a6f31643b050d4bfbd7a225959eb6a1bea6f87d7a31eebd161918b3e842f5ccd2be4dd0e2a31d056fc19f0f3e82ea5ff1f98704db8ebf2d6716e40cea6a4c192d28ba57109a1cee48fc582161970b413d7d102";

	        Console.WriteLine(outstring==excepts);

	        Console.WriteLine("modulus:"+modulus);

            Console.WriteLine("exponent:" + exponent);

            Console.WriteLine("InputString:"+input);
            Console.WriteLine("OutputString:" + outstring);
输出结果如下:True modulus:00aaf5bded978566ffd79e6801e0fd922baf4171dcd5797d9dc6c2deb0da1bebc0265be9c345e7ca8c5f9ad220771fdbced7fbc706a3d29f1db337fb7ab21c49ac64319deb2292145b4ffcdb044a8e5339d4a8717183b165fad13083e368c6a03189e95b1df77fc7cda4488eac13098c8a11e57a5b632395a9fb2a4d75b94ed49f exponent:10001 InputString:20001 OutputString:87294da04beda7bcde49f0c830a283d58429b7df529ccf815af77e49c0f4ba88b94fd074aafbb8b6df8da93d22a6f31643b050d4bfbd7a225959eb6a1bea6f87d7a31eebd161918b3e842f5ccd2be4dd0e2a31d056fc19f0f3e82ea5ff1f98704db8ebf2d6716e40cea6a4c192d28ba57109a1cee48fc582161970b413d7d102
分全给你了,多谢了
阿东zh 2017-03-09
  • 打赏
  • 举报
回复
引用 3 楼 starfd 的回复:
net下也要分段加密然后组合
我用了分段组合 加密出来的好像不一样呢

using (var rsaProvider = new RSACryptoServiceProvider())
            {
                var inputBytes = Encoding.UTF8.GetBytes(rawInput);//有含义的字符串转化为字节流
                rsaProvider.FromXmlString(publicKey);//载入公钥
                int bufferSize = 117; //(rsaProvider.KeySize / 8) - 11;//单块最大长度
                var buffer = new byte[bufferSize];
                using (MemoryStream inputStream = new MemoryStream(inputBytes), outputStream = new MemoryStream())
                {
                    while (true)
                    {
                        //分段加密
                        int readSize = inputStream.Read(buffer, 0, bufferSize);
                        if (readSize <= 0)
                        {
                            break;
                        }

                        var temp = new byte[readSize];
                        Array.Copy(buffer, 0, temp, 0, readSize);
                        var encryptedBytes = rsaProvider.Encrypt(temp, false);
                        outputStream.Write(encryptedBytes, 0, encryptedBytes.Length);
                    }
                    return Convert.ToBase64String(outputStream.ToArray());//转化为字节流方便传输
                }
            }
  • 打赏
  • 举报
回复
net下也要分段加密然后组合
阿东zh 2017-03-09
  • 打赏
  • 举报
回复
引用 1 楼 xdashewan 的回复:
随便一搜拿去试试http://www.cnblogs.com/wcp066/articles/2174843.html
我这些都试过了。。没成功的 还是多谢你了
xdashewan 2017-03-09
  • 打赏
  • 举报
回复
随便一搜拿去试试http://www.cnblogs.com/wcp066/articles/2174843.html

110,526

社区成员

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

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

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