c#rsa加密java解密_C#-java RSA加密解密

usingOrg.BouncyCastle.Math;usingOrg.BouncyCastle.Crypto.Parameters;usingOrg.BouncyCastle.Security;usingOrg.BouncyCastle.Asn1.X509;usingOrg.BouncyCastle.X509;usingOrg.BouncyCastle.Asn1.Pkcs;usingOrg.BouncyCastle.Pkcs;usingSystem;usingSystem.Security.Cryptography;usingSystem.Text;usingSystem.Xml;namespaceUtilityHelper

{///

///RSA加密解密&数字签名///

public sealed classRSA

{#region RSA 加密解密

#region RSA 的密钥产生

///

///RSA产生密钥///

/// 私钥

/// 公钥

static public void RSAKey(out string xmlKeys, out stringxmlPublicKey)

{try{

System.Security.Cryptography.RSACryptoServiceProvider rsa= newRSACryptoServiceProvider();

xmlKeys= rsa.ToXmlString(true);

xmlPublicKey= rsa.ToXmlString(false);

}catch(Exception ex)

{throwex;

}

}#endregion

#region RSA加密函数

//##############################################################################//RSA 方式加密//KEY必须是XML的形式,返回的是字符串//该加密方式有长度限制的!//##############################################################################

///

///RSA的加密函数///

/// 公钥

/// 待加密的字符串

///

static public string RSAEncrypt(string xmlPublicKey, stringencryptString)

{try{byte[] PlainTextBArray;byte[] CypherTextBArray;stringResult;

System.Security.Cryptography.RSACryptoServiceProvider rsa= newRSACryptoServiceProvider();

rsa.FromXmlString(xmlPublicKey);

PlainTextBArray= (newUTF8Encoding()).GetBytes(encryptString);

CypherTextBArray= rsa.Encrypt(PlainTextBArray, false);

Result=Convert.ToBase64String(CypherTextBArray);returnResult;

}catch(Exception ex)

{throwex;

}

}///

///RSA的加密函数///

/// 公钥

/// 待加密的字节数组

///

static public string RSAEncrypt(string xmlPublicKey, byte[] EncryptString)

{try{byte[] CypherTextBArray;stringResult;

System.Security.Cryptography.RSACryptoServiceProvider rsa= newRSACryptoServiceProvider();

rsa.FromXmlString(xmlPublicKey);

CypherTextBArray= rsa.Encrypt(EncryptString, false);

Result=Convert.ToBase64String(CypherTextBArray);returnResult;

}catch(Exception ex)

{throwex;

}

}#endregion

#region RSA解密函数

///

///RSA的解密函数///

/// 私钥

/// 待解密的字符串

///

static public string RSADecrypt(string xmlPrivateKey, stringdecryptString)

{try{byte[] PlainTextBArray;byte[] DypherTextBArray;stringResult;

System.Security.Cryptography.RSACryptoServiceProvider rsa= newRSACryptoServiceProvider();

rsa.FromXmlString(xmlPrivateKey);

PlainTextBArray=Convert.FromBase64String(decryptString);

DypherTextBArray= rsa.Decrypt(PlainTextBArray, false);

Result= (newUTF8Encoding()).GetString(DypherTextBArray);returnResult;

}catch(Exception ex)

{throwex;

}

}///

///RSA的解密函数///

/// 私钥

/// 待解密的字节数组

///

static public string RSADecrypt(string xmlPrivateKey, byte[] DecryptString)

{try{byte[] DypherTextBArray;stringResult;

System.Security.Cryptography.RSACryptoServiceProvider rsa= newRSACryptoServiceProvider();

rsa.FromXmlString(xmlPrivateKey);

DypherTextBArray= rsa.Decrypt(DecryptString, false);

Result= (newUnicodeEncoding()).GetString(DypherTextBArray);returnResult;

}catch(Exception ex)

{throwex;

}

}#endregion

static public string EncryptRSA(string publickey, stringplaintext)

{//RSA的加解密过程://有 rsa1 和 rsa2 两个RSA对象。//现在要 rsa2 发送一段信息给 rsa1, 则先由 rsa1 发送“公钥”给 rsa2//rsa2 获取得公钥之后,用来加密要发送的数据内容。//rsa1 获取

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值