zoukankan      html  css  js  c++  java
  • Java和C# RSA加解密相互通信和使用公钥加密传输

    关于JAVA和C#加解密通讯的话,可以用这个BouncyCastle插件,会帮助你解决很多问题

    http://www.bouncycastle.org/

    //c#使用java给的公钥进行rsa加密
    public static byte[] RSAEncrypt(string publickey, string content)
        {
            RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();
            byte[] publicKeyBytes = Convert.FromBase64String(publickey);
            AsymmetricKeyParameter asymmetricKeyParameter = PublicKeyFactory.CreateKey(publicKeyBytes);
            RsaKeyParameters rsaKeyParameters = (RsaKeyParameters)asymmetricKeyParameter;
            RSAParameters rsaParameters = new RSAParameters();
            rsaParameters.Modulus = rsaKeyParameters.Modulus.ToByteArrayUnsigned();
            rsaParameters.Exponent = rsaKeyParameters.Exponent.ToByteArrayUnsigned();
            rsa.ImportParameters(rsaParameters);
            return rsa.Encrypt(Encoding.GetEncoding("gbk").GetBytes(content), false);
        }

    使用方法:

    string tempStr=Convert.ToBase64String(RSACoder.RSAEncrypt("公钥", "需要加密码的字符"))

    另一种写法:一般都是参数使用utf-8编码:

      public string RSAEncrypt2(string publickey, string content)
            {
                RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();
                byte[] publicKeyBytes = Convert.FromBase64String(publickey);
                AsymmetricKeyParameter privateKey = PublicKeyFactory.CreateKey(publicKeyBytes);
                IBufferedCipher c = CipherUtilities.GetCipher("RSA/ECB/PKCS1Padding");
                //加密 
                c.Init(true, privateKey);
                byte[] byteData = Encoding.UTF8.GetBytes(content);
                byteData = c.DoFinal(byteData, 0, byteData.Length);
               return Convert.ToBase64String(byteData);
            }

     c#通过rsa公钥解密java那边加密字符:

          public static string RSADeEncry222(string content, string publickey)
            {
                byte[] btPem = Convert.FromBase64String(publickey);
                int pemModulus = 128, pemPublicExponent = 3;
                byte[] btPemModulus = new byte[128];
                byte[] btPemPublicExponent = new byte[3];
                for (int i = 0; i < pemModulus; i++)
                {
                    btPemModulus[i] = btPem[29 + i];
                }
                for (int i = 0; i < pemPublicExponent; i++)
                {
                    btPemPublicExponent[i] = btPem[159 + i];
                }
                BigInteger biModulus = new BigInteger(1, btPemModulus);
                BigInteger biExponent = new BigInteger(1, btPemPublicExponent);
                RsaKeyParameters publicParameters = new RsaKeyParameters(false, biModulus, biExponent);
                IAsymmetricBlockCipher eng = new Pkcs1Encoding(new RsaEngine());
                eng.Init(false, publicParameters);
                // 解密已加密的数据
                byte[] encryptedData = Convert.FromBase64String(content);
                encryptedData = eng.ProcessBlock(encryptedData, 0, encryptedData.Length);
                string result = Encoding.UTF8.GetString(encryptedData, 0, encryptedData.Length);
                return result;
            }

    普通C#自身调用加解密:

    RSACryptoServiceProvider oRSA = new RSACryptoServiceProvider();
                string privatekey = oRSA.ToXmlString(true);//私钥 
                string publickey = oRSA.ToXmlString(false);//公钥 
                //这两个密钥需要保存下来 
                byte[] messagebytes = Encoding.UTF8.GetBytes("luo罗"); //需要加密的数据 
    
                //公钥加密 
                RSACryptoServiceProvider oRSA1 = new RSACryptoServiceProvider();
                oRSA1.FromXmlString(publickey); //加密要用到公钥所以导入公钥 
                byte[] AOutput = oRSA1.Encrypt(messagebytes, false); //AOutput 加密以后的数据 
    
                //私钥解密 
                RSACryptoServiceProvider oRSA2 = new RSACryptoServiceProvider();
                oRSA2.FromXmlString(privatekey);
                byte[] AInput = oRSA2.Decrypt(AOutput, false);
                string reslut = Encoding.UTF8.GetString(AInput);
  • 相关阅读:
    gzip是一种数据格式,deflate是一种压缩算法
    js 实现图片上传 续
    iframe 元素会创建包含另外一个文档的内联框架(即行内框架)
    HTTPS简介----
    回归测试
    HTTP 返回码 400
    js 实现 一张图片的上传
    121. Best Time to Buy and Sell Stock
    119. Pascal's Triangle II
    118. Pascal's Triangle
  • 原文地址:https://www.cnblogs.com/25miao/p/8401475.html
Copyright © 2011-2022 走看看