C#与JAVA平台RSA算法交互示例

算法:RSA/ECB/PKCS1Padding
========================java==========================================================
import org.junit.Test;
  import sun.misc.BASE64Decoder;
  import sun.misc.BASE64Encoder;
 
  import javax.crypto.Cipher;
  import java.io.IOException;
  import java.math.BigInteger;
  import java.security.KeyFactory;
  import java.security.PrivateKey;
  import java.security.PublicKey;
  import java.security.spec.RSAPrivateKeySpec;
  import java.security.spec.RSAPublicKeySpec;
 

  public class RSATest {
         String e = "65537";
         String n = "123410773237385713572440 712840019405878257600213 906351775134402766524785 605776353635515879438375 969303333340691224323217 379791619946464100287854 264933660919378485000299 257054039555887477610831 829409144592603086784397 675690934246422666689022 312589317493002336070775 714030955737435316659994 026756956753416169929";
         String d = "102299672961750099570264 341757280532275542401837 445663770632643616494888 426681205847125069294737 520917541038900032845310 395266178574112466427178 094767628391398378924953 787043659322148498169017 989730953803803890989295 625028193153480552247210 183981959942844345362118 479513739632952091865360 095551345350474614233";
 
         @Test
         public void testDecrypt() {
                 BigInteger bn = new BigInteger(n);
                 BigInteger be = new BigInteger(e);
                 BigInteger bd = new BigInteger(d);
                 RSAPublicKeySpec pub = new RSAPublicKeySpec(bn, be);
                 RSAPrivateKeySpec prv = new RSAPrivateKeySpec(bn, bd);
                 try {
                         KeyFactory kf = KeyFactory.getInstance("RSA");
                         PublicKey pubkey = kf.generatePublic(pub);
                         PrivateKey prvkey = kf.generatePrivate(prv);
                         Cipher rsa = Cipher.getInstance("RSA/ECB/PKCS1Padding");
                         //加密
                         rsa.init(Cipher.ENCRYPT_MODE, pubkey);
                         String txt = "Welcome,RSA!from 博客园";
                         byte[] encdata = rsa.doFinal(txt.getBytes());
                         System.out.printf("RSA加密:\n明文:%s\n密文:%s\n", txt, convertToBase64(encdata));
                         //解密
                         rsa.init(Cipher.DECRYPT_MODE, prvkey);
                         rsa.update(encdata);
                         byte[] res = rsa.doFinal();
                         System.out.printf("解密:%s", new String(res));
                 } catch (Exception e1) {
                         e1.printStackTrace();
                 }
         }
 
         public byte[] convertFromBase64(String s) throws IOException {
                 s = s.replaceAll("\\\\n", "\n");
                 BASE64Decoder decoder = new BASE64Decoder();
                 return decoder.decodeBuffer(s);
         }
 
         public String convertToBase64(byte[] data) {
                 BASE64Encoder encoder = new BASE64Encoder();
                 return encoder.encode(data);
         }
 
 
  }
========================================c#=======================================
using System.Text.RegularExpressions;
  using System.Text;
  using System.IO;
  using System.Net;
  using System.Xml;
  using System.Runtime.InteropServices;
  using System;
  using System.Security;
  using System.Security.Cryptography;
  public class RSATestForJava
  {
         private String p,q,e,n,d,dp,dq,crt;
         private RSAParameters param;
         public void Init(){
                 p="127943902265443016140766 504356029930365580566785 531125406982972499633654 346285053931166534228183 653442283798282317643925 217315846844352480937870 27949139091";
                 q="964569401528394079724485 156154356999252509841605 532282522011604665543586 072736174089675316153449 241550079927206364763389 411220591842030754163454 4631799219";
                 e="65537";
                 n="123410773237385713572440 712840019405878257600213 906351775134402766524785 605776353635515879438375 969303333340691224323217 379791619946464100287854 264933660919378485000299 257054039555887477610831 829409144592603086784397 675690934246422666689022 312589317493002336070775 714030955737435316659994 026756956753416169929";
                 d="102299672961750099570264 341757280532275542401837 445663770632643616494888 426681205847125069294737 520917541038900032845310 395266178574112466427178 094767628391398378924953 787043659322148498169017 989730953803803890989295 625028193153480552247210 183981959942844345362118 479513739632952091865360 095551345350474614233";
                 dp="152996988274452128949324 365570320058024483101438 608332669259434438504806 309693145499267914116647 892339774192767219032827 552085736563954772588017 3612423563";
                 dq="769453717928871362558494 956494038236735297839680 443531596667023084892788 499361386413907037681040 120057954721673997128797 449053397950796768659923 391901465";
                 crt="125835408516608196307969 102732581437489468577633 067034392745717171965074 025363982346160147356039 159811418075430552506582 516703408106112516841693 56826070785";
 
                 param=new RSAParameters();
                 byte[] bdata=GetBytes(e);
                 param.Exponent=bdata;
                 param.P=GetBytes(p);
                 param.Q=GetBytes(q);
                 param.Modulus=GetBytes(n);
                 param.D=GetBytes(d);
                 param.DP=GetBytes(dp);
                 param.DQ=GetBytes(dq);
                 param.InverseQ=GetBytes(crt);
         }
         public void DoTest(){
                 RSACryptoServiceProvider  rsa = new RSACryptoServiceProvider ();
                 rsa.ImportParameters(param);
                 String edata="E3F+Qwyk8MJkHv7zEOb4Fdg0CX4p QOKghyOmMew8jPDUbxNJhDSl V/aS5nnVuiEtkWU1BmCrPSEi";
                 edata+="swhuEjC7eRJnq5pEkyf0GILH FNkuoqD1o4FjKnl/oTXg83wn5JDgi5Bog5lJM8Yc QcExEMsH9IF+";
                 edata+="gbeq6rWYrbb4Qj2oM8c=";
                 //解密JAVA加密的数据
                 Byte[] encdata=Convert.FromBase64String(edata);
                 byte[] dedata=rsa.Decrypt(encdata,false);
                 Console.WriteLine("\n解密JAVA加密的数据:"+Encoding.Default.GetString(dedata));
         }
         public static void Main(String[] args)
         {
                 RSATestForJava obj=new RSATestForJava();
                 obj.Init();
                 obj.DoTest();
         }
          public static byte[] GetBytes(String num){
                 BigInteger n=new BigInteger(num,10);
                 String s=n.ToString(2);
                 if(s.Length%8>0){
                         s=new String('0',8-s.Length%8)+s;
                 }
                 byte[] data=new byte[s.Length/8];
                 String ocetstr;
                 for(int i=0;i<data.Length;i++){
                         ocetstr=s.Substring(8*i,8);
                         data[i]=Convert.ToByte(ocetstr , 2 ) ;
                 }
                 return data;
         }
 
         public String ConvByteArrayToHex(byte[] data){
                 String s="";
                 for(int i=0;i<data.Length;i++){
                         s+=Convert.ToString(data[i],16);
                 }
                 return s.ToUpper();
         }
 
 

 }



转:http://blog.sina.com.cn/s/blog_76550fd7010141z4.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。 经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。 经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。 经导师精心指导并认可、获 98 分的毕业设计项目!【项目资源】:微信小程序。【项目说明】:聚焦计算机相关专业毕设及实战操练,可作课程设计与期末大作业,含全部源码,能直用于毕设,经严格调试,运行有保障!【项目服务】:有任何使用上的问题,欢迎随时与博主沟通,博主会及时解答。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值