C#-java RSA加密解密

using Org.BouncyCastle.Math;
using Org.BouncyCastle.Crypto.Parameters;
using Org.BouncyCastle.Security;
using Org.BouncyCastle.Asn1.X509;
using Org.BouncyCastle.X509;
using Org.BouncyCastle.Asn1.Pkcs;
using Org.BouncyCastle.Pkcs;
using System;
using System.Security.Cryptography;
using System.Text;
using System.Xml;

namespace UtilityHelper
{
/// <summary>
/// RSA加密解密&数字签名
/// </summary>
public sealed class RSA
{
#region RSA 加密解密

#region RSA 的密钥产生
/// <summary>
/// RSA产生密钥
/// </summary>
/// <param name="xmlKeys">私钥</param>
/// <param name="xmlPublicKey">公钥</param>
static public void RSAKey(out string xmlKeys, out string xmlPublicKey)
{
try
{
System.Security.Cryptography.RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();
xmlKeys = rsa.ToXmlString(true);
xmlPublicKey = rsa.ToXmlString(false);
}
catch (Exception ex)
{
throw ex;
}
}
#endregion

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

/// <summary>
/// RSA的加密函数
/// </summary>
/// <param name="xmlPublicKey">公钥</param>
/// <param name="encryptString">待加密的字符串</param>
/// <returns></returns>
static public string RSAEncrypt(string xmlPublicKey, string encryptString)
{
try
{
byte[] PlainTextBArray;
byte[] CypherTextBArray;
string Result;
System.Security.Cryptography.RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();
rsa.FromXmlString(xmlPublicKey);
PlainTextBArray = (new UTF8Encoding()).GetBytes(encryptString);
CypherTextBArray = rsa.Encrypt(PlainTextBArray, false);
Result = Convert.ToBase64String(CypherTextBArray);
return Result;
}
catch (Exception ex)
{
throw ex;
}
}
/// <summary>
/// RSA的加密函数
/// </summary>
/// <param name="xmlPublicKey">公钥</param>
/// <param name="EncryptString">待加密的字节数组</param>
/// <returns></returns>
static public string RSAEncrypt(string xmlPublicKey, byte[] EncryptString)
{
try
{
byte[] CypherTextBArray;
string Result;
System.Security.Cryptography.RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();
rsa.FromXmlString(xmlPublicKey);
CypherTextBArray = rsa.Encrypt(EncryptString, false);
Result = Convert.ToBase64String(CypherTextBArray);
return Result;
}
catch (Exception ex)
{
throw ex;
}
}
#endregion

#region RSA解密函数
/// <summary>
/// RSA的解密函数
/// </summary>
/// <param name="xmlPrivateKey">私钥</param>
/// <param name="decryptString">待解密的字符串</param>
/// <returns></returns>
static public string RSADecrypt(string xmlPrivateKey, string decryptString)
{
try
{
byte[] PlainTextBArray;
byte[] DypherTextBArray;
string Result;
System.Security.Cryptography.RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();
rsa.FromXmlString(xmlPrivateKey);
PlainTextBArray = Convert.FromBase64String(decryptString);
DypherTextBArray = rsa.Decrypt(PlainTextBArray, false);
Result = (new UTF8Encoding()).GetString(DypherTextBArray);
return Result;
}
catch (Exception ex)
{
throw ex;
}
}
/// <summary>
/// RSA的解密函数
/// </summary>
/// <param name="xmlPrivateKey">私钥</param>
/// <param name="DecryptString">待解密的字节数组</param>
/// <returns></returns>
static public string RSADecrypt(string xmlPrivateKey, byte[] DecryptString)
{
try
{
byte[] DypherTextBArray;
string Result;
System.Security.Cryptography.RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();
rsa.FromXmlString(xmlPrivateKey);
DypherTextBArray = rsa.Decrypt(DecryptString, false);
Result = (new UnicodeEncoding()).GetString(DypherTextBArray);
return Result;
}
catch (Exception ex)
{
throw ex;
}
}
#endregion

static public string EncryptRSA(string publickey, string plaintext)
{
//RSA的加解密过程:
// 有 rsa1 和 rsa2 两个RSA对象。
// 现在要 rsa2 发送一段信息给 rsa1, 则先由 rsa1 发送“公钥”给 rsa2
// rsa2 获取得公钥之后,用来加密要发送的数据内容。
// rsa1 获取加密后的内容后,用自己的私钥解密,得出原始的数据内容。

RSACryptoServiceProvider rsa1 = new RSACryptoServiceProvider();
RSACryptoServiceProvider rsa2 = new RSACryptoServiceProvider();

publickey = rsa1.ToXmlString(false); //导出 rsa1 的公钥

rsa2.FromXmlString(publickey); //rsa2 导入 rsa1 的公钥,用于加密信息

//rsa2开始加密
byte[] cipherbytes;
cipherbytes = rsa2.Encrypt(Encoding.UTF8.GetBytes(plaintext), false);

string resut = string.Empty;
for (int i = 0; i < cipherbytes.Length; i++)
{
resut += string.Format("{0:X2} ", cipherbytes[i]);
}
return resut;
}

public void DecryptRSA(string publickey, string plaintext)
{
////RSA的加解密过程:
//// 有 rsa1 和 rsa2 两个RSA对象。
//// 现在要 rsa2 发送一段信息给 rsa1, 则先由 rsa1 发送“公钥”给 rsa2
//// rsa2 获取得公钥之后,用来加密要发送的数据内容。
//// rsa1 获取加密后的内容后,用自己的私钥解密,得出原始的数据内容。

//RSACryptoServiceProvider rsa1 = new RSACryptoServiceProvider();
//RSACryptoServiceProvider rsa2 = new RSACryptoServiceProvider();

//publickey = rsa1.ToXmlString(false); //导出 rsa1 的公钥

//rsa2.FromXmlString(publickey); //rsa2 导入 rsa1 的公钥,用于加密信息

////rsa2开始加密
//byte[] cipherbytes;
//cipherbytes = rsa2.Encrypt(Encoding.UTF8.GetBytes(plaintext), false);

//string resut = string.Empty;
//for (int i = 0; i < cipherbytes.Length; i++)
//{
// resut += string.Format("{0:X2} ", cipherbytes[i]);
//}
//return resut;
////rsa1开始解密
//byte[] plaintbytes;
//plaintbytes = rsa1.Decrypt(cipherbytes, false);

//Console.WriteLine("解密后的数据是:");
//Console.WriteLine(Encoding.UTF8.GetString(plaintbytes));

//Console.ReadLine();
}

#endregion

#region RSA 数字签名

#region 获取Hash描述表
/// <summary>
/// 获取Hash描述表
/// </summary>
/// <param name="strSource">待签名的字符串</param>
/// <param name="HashData">Hash描述</param>
/// <returns></returns>
static public bool GetHash(string strSource, ref byte[] HashData)
{
try
{
byte[] Buffer;
System.Security.Cryptography.HashAlgorithm MD5 = System.Security.Cryptography.HashAlgorithm.Create("MD5");
Buffer = System.Text.Encoding.GetEncoding("GB2312").GetBytes(strSource);
HashData = MD5.ComputeHash(Buffer);
return true;
}
catch (Exception ex)
{
throw ex;
}
}

/// <summary>
/// 获取Hash描述表
/// </summary>
/// <param name="strSource">待签名的字符串</param>
/// <param name="strHashData">Hash描述</param>
/// <returns></returns>
static public bool GetHash(string strSource, ref string strHashData)
{
try
{
//从字符串中取得Hash描述
byte[] Buffer;
byte[] HashData;
System.Security.Cryptography.HashAlgorithm MD5 = System.Security.Cryptography.HashAlgorithm.Create("MD5");
Buffer = System.Text.Encoding.GetEncoding("GB2312").GetBytes(strSource);
HashData = MD5.ComputeHash(Buffer);
strHashData = Convert.ToBase64String(HashData);
return true;
}
catch (Exception ex)
{
throw ex;
}
}

/// <summary>
/// 获取Hash描述表
/// </summary>
/// <param name="objFile">待签名的文件</param>
/// <param name="HashData">Hash描述</param>
/// <returns></returns>
static public bool GetHash(System.IO.FileStream objFile, ref byte[] HashData)
{
try
{
//从文件中取得Hash描述
System.Security.Cryptography.HashAlgorithm MD5 = System.Security.Cryptography.HashAlgorithm.Create("MD5");
HashData = MD5.ComputeHash(objFile);
objFile.Close();
return true;
}
catch (Exception ex)
{
throw ex;
}
}

/// <summary>
/// 获取Hash描述表
/// </summary>
/// <param name="objFile">待签名的文件</param>
/// <param name="strHashData">Hash描述</param>
/// <returns></returns>
static public bool GetHash(System.IO.FileStream objFile, ref string strHashData)
{
try
{
//从文件中取得Hash描述
byte[] HashData;
System.Security.Cryptography.HashAlgorithm MD5 = System.Security.Cryptography.HashAlgorithm.Create("MD5");
HashData = MD5.ComputeHash(objFile);
objFile.Close();
strHashData = Convert.ToBase64String(HashData);
return true;
}
catch (Exception ex)
{
throw ex;
}
}
#endregion

#region RSA签名
/// <summary>
/// RSA签名
/// </summary>
/// <param name="strKeyPrivate">私钥</param>
/// <param name="HashbyteSignature">待签名Hash描述</param>
/// <param name="EncryptedSignatureData">签名后的结果</param>
/// <returns></returns>
static public bool SignatureFormatter(string strKeyPrivate, byte[] HashbyteSignature, ref byte[] EncryptedSignatureData)
{
try
{
System.Security.Cryptography.RSACryptoServiceProvider RSA = new System.Security.Cryptography.RSACryptoServiceProvider();

RSA.FromXmlString(strKeyPrivate);
System.Security.Cryptography.RSAPKCS1SignatureFormatter RSAFormatter = new System.Security.Cryptography.RSAPKCS1SignatureFormatter(RSA);
//设置签名的算法为MD5
RSAFormatter.SetHashAlgorithm("MD5");
//执行签名
EncryptedSignatureData = RSAFormatter.CreateSignature(HashbyteSignature);
return true;
}
catch (Exception ex)
{
throw ex;
}
}

/// <summary>
/// RSA签名
/// </summary>
/// <param name="strKeyPrivate">私钥</param>
/// <param name="HashbyteSignature">待签名Hash描述</param>
/// <param name="m_strEncryptedSignatureData">签名后的结果</param>
/// <returns></returns>
static public bool SignatureFormatter(string strKeyPrivate, byte[] HashbyteSignature, ref string strEncryptedSignatureData)
{
try
{
byte[] EncryptedSignatureData;
System.Security.Cryptography.RSACryptoServiceProvider RSA = new System.Security.Cryptography.RSACryptoServiceProvider();
RSA.FromXmlString(strKeyPrivate);
System.Security.Cryptography.RSAPKCS1SignatureFormatter RSAFormatter = new System.Security.Cryptography.RSAPKCS1SignatureFormatter(RSA);
//设置签名的算法为MD5
RSAFormatter.SetHashAlgorithm("MD5");
//执行签名
EncryptedSignatureData = RSAFormatter.CreateSignature(HashbyteSignature);
strEncryptedSignatureData = Convert.ToBase64String(EncryptedSignatureData);
return true;
}
catch (Exception ex)
{
throw ex;
}
}

/// <summary>
/// RSA签名
/// </summary>
/// <param name="strKeyPrivate">私钥</param>
/// <param name="strHashbyteSignature">待签名Hash描述</param>
/// <param name="EncryptedSignatureData">签名后的结果</param>
/// <returns></returns>
static public bool SignatureFormatter(string strKeyPrivate, string strHashbyteSignature, ref byte[] EncryptedSignatureData)
{
try
{
byte[] HashbyteSignature;

HashbyteSignature = Convert.FromBase64String(strHashbyteSignature);
System.Security.Cryptography.RSACryptoServiceProvider RSA = new System.Security.Cryptography.RSACryptoServiceProvider();

RSA.FromXmlString(strKeyPrivate);
System.Security.Cryptography.RSAPKCS1SignatureFormatter RSAFormatter = new System.Security.Cryptography.RSAPKCS1SignatureFormatter(RSA);
//设置签名的算法为MD5
RSAFormatter.SetHashAlgorithm("MD5");
//执行签名
EncryptedSignatureData = RSAFormatter.CreateSignature(HashbyteSignature);

return true;
}
catch (Exception ex)
{
throw ex;
}
}

/// <summary>
/// RSA签名
/// </summary>
/// <param name="strKeyPrivate">私钥</param>
/// <param name="strHashbyteSignature">待签名Hash描述</param>
/// <param name="strEncryptedSignatureData">签名后的结果</param>
/// <returns></returns>
static public bool SignatureFormatter(string strKeyPrivate, string strHashbyteSignature, ref string strEncryptedSignatureData)
{
try
{
byte[] HashbyteSignature;
byte[] EncryptedSignatureData;
HashbyteSignature = Convert.FromBase64String(strHashbyteSignature);
System.Security.Cryptography.RSACryptoServiceProvider RSA = new System.Security.Cryptography.RSACryptoServiceProvider();
RSA.FromXmlString(strKeyPrivate);
System.Security.Cryptography.RSAPKCS1SignatureFormatter RSAFormatter = new System.Security.Cryptography.RSAPKCS1SignatureFormatter(RSA);
//设置签名的算法为MD5
RSAFormatter.SetHashAlgorithm("MD5");
//执行签名
EncryptedSignatureData = RSAFormatter.CreateSignature(HashbyteSignature);
strEncryptedSignatureData = Convert.ToBase64String(EncryptedSignatureData);
return true;
}
catch (Exception ex)
{
throw ex;
}
}
#endregion

#region RSA 签名验证
/// <summary>
/// RSA签名验证
/// </summary>
/// <param name="strKeyPublic">公钥</param>
/// <param name="HashbyteDeformatter">Hash描述</param>
/// <param name="DeformatterData">签名后的结果</param>
/// <returns></returns>
static public bool SignatureDeformatter(string strKeyPublic, byte[] HashbyteDeformatter, byte[] DeformatterData)
{
try
{
System.Security.Cryptography.RSACryptoServiceProvider RSA = new System.Security.Cryptography.RSACryptoServiceProvider();
RSA.FromXmlString(strKeyPublic);
System.Security.Cryptography.RSAPKCS1SignatureDeformatter RSADeformatter = new System.Security.Cryptography.RSAPKCS1SignatureDeformatter(RSA);
//指定解密的时候HASH算法为MD5
RSADeformatter.SetHashAlgorithm("MD5");
if (RSADeformatter.VerifySignature(HashbyteDeformatter, DeformatterData))
{
return true;
}
else
{
return false;
}
}
catch (Exception ex)
{
throw ex;
}
}
/// <summary>
/// RSA签名验证
/// </summary>
/// <param name="strKeyPublic">公钥</param>
/// <param name="strHashbyteDeformatter">Hash描述</param>
/// <param name="DeformatterData">签名后的结果</param>
/// <returns></returns>
static public bool SignatureDeformatter(string strKeyPublic, string strHashbyteDeformatter, byte[] DeformatterData)
{
try
{
byte[] HashbyteDeformatter;
HashbyteDeformatter = Convert.FromBase64String(strHashbyteDeformatter);
System.Security.Cryptography.RSACryptoServiceProvider RSA = new System.Security.Cryptography.RSACryptoServiceProvider();
RSA.FromXmlString(strKeyPublic);
System.Security.Cryptography.RSAPKCS1SignatureDeformatter RSADeformatter = new System.Security.Cryptography.RSAPKCS1SignatureDeformatter(RSA);
//指定解密的时候HASH算法为MD5
RSADeformatter.SetHashAlgorithm("MD5");
if (RSADeformatter.VerifySignature(HashbyteDeformatter, DeformatterData))
{
return true;
}
else
{
return false;
}
}
catch (Exception ex)
{
throw ex;
}
}
/// <summary>
/// RSA签名验证
/// </summary>
/// <param name="strKeyPublic">公钥</param>
/// <param name="HashbyteDeformatter">Hash描述</param>
/// <param name="strDeformatterData">签名后的结果</param>
/// <returns></returns>
static public bool SignatureDeformatter(string strKeyPublic, byte[] HashbyteDeformatter, string strDeformatterData)
{
try
{
byte[] DeformatterData;
System.Security.Cryptography.RSACryptoServiceProvider RSA = new System.Security.Cryptography.RSACryptoServiceProvider();
RSA.FromXmlString(strKeyPublic);
System.Security.Cryptography.RSAPKCS1SignatureDeformatter RSADeformatter = new System.Security.Cryptography.RSAPKCS1SignatureDeformatter(RSA);
//指定解密的时候HASH算法为MD5
RSADeformatter.SetHashAlgorithm("MD5");
DeformatterData = Convert.FromBase64String(strDeformatterData);
if (RSADeformatter.VerifySignature(HashbyteDeformatter, DeformatterData))
{
return true;
}
else
{
return false;
}
}
catch (Exception ex)
{
throw ex;
}
}
/// <summary>
/// RSA签名验证
/// </summary>
/// <param name="strKeyPublic">公钥</param>
/// <param name="strHashbyteDeformatter">Hash描述</param>
/// <param name="strDeformatterData">签名后的结果</param>
/// <returns></returns>
static public bool SignatureDeformatter(string strKeyPublic, string strHashbyteDeformatter, string strDeformatterData)
{
try
{
byte[] DeformatterData;
byte[] HashbyteDeformatter;
HashbyteDeformatter = Convert.FromBase64String(strHashbyteDeformatter);
System.Security.Cryptography.RSACryptoServiceProvider RSA = new System.Security.Cryptography.RSACryptoServiceProvider();
RSA.FromXmlString(strKeyPublic);
System.Security.Cryptography.RSAPKCS1SignatureDeformatter RSADeformatter = new System.Security.Cryptography.RSAPKCS1SignatureDeformatter(RSA);
//指定解密的时候HASH算法为MD5
RSADeformatter.SetHashAlgorithm("MD5");
DeformatterData = Convert.FromBase64String(strDeformatterData);
if (RSADeformatter.VerifySignature(HashbyteDeformatter, DeformatterData))
{
return true;
}
else
{
return false;
}
}
catch (Exception ex)
{
throw ex;
}
}
#endregion

#endregion

#region 公钥私钥DotNet和Java互转

/// <summary>
/// RSA私钥格式转换,java->.net
/// </summary>
/// <param name="privateKey">java生成的RSA私钥</param>
/// <returns></returns>
public static string RSAPrivateKeyJava2DotNet(string privateKey)
{
RsaPrivateCrtKeyParameters privateKeyParam = (RsaPrivateCrtKeyParameters)PrivateKeyFactory.CreateKey(Convert.FromBase64String(privateKey));

return string.Format("<RSAKeyValue><Modulus>{0}</Modulus><Exponent>{1}</Exponent><P>{2}</P><Q>{3}</Q><DP>{4}</DP><DQ>{5}</DQ><InverseQ>{6}</InverseQ><D>{7}</D></RSAKeyValue>",
Convert.ToBase64String(privateKeyParam.Modulus.ToByteArrayUnsigned()),
Convert.ToBase64String(privateKeyParam.PublicExponent.ToByteArrayUnsigned()),
Convert.ToBase64String(privateKeyParam.P.ToByteArrayUnsigned()),
Convert.ToBase64String(privateKeyParam.Q.ToByteArrayUnsigned()),
Convert.ToBase64String(privateKeyParam.DP.ToByteArrayUnsigned()),
Convert.ToBase64String(privateKeyParam.DQ.ToByteArrayUnsigned()),
Convert.ToBase64String(privateKeyParam.QInv.ToByteArrayUnsigned()),
Convert.ToBase64String(privateKeyParam.Exponent.ToByteArrayUnsigned()));
}

/// <summary>
/// RSA私钥格式转换,.net->java
/// </summary>
/// <param name="privateKey">.net生成的私钥</param>
/// <returns></returns>
public static string RSAPrivateKeyDotNet2Java(string privateKey)
{
XmlDocument doc = new XmlDocument();
doc.LoadXml(privateKey);
BigInteger m = new BigInteger(1, Convert.FromBase64String(doc.DocumentElement.GetElementsByTagName("Modulus")[0].InnerText));
BigInteger exp = new BigInteger(1, Convert.FromBase64String(doc.DocumentElement.GetElementsByTagName("Exponent")[0].InnerText));
BigInteger d = new BigInteger(1, Convert.FromBase64String(doc.DocumentElement.GetElementsByTagName("D")[0].InnerText));
BigInteger p = new BigInteger(1, Convert.FromBase64String(doc.DocumentElement.GetElementsByTagName("P")[0].InnerText));
BigInteger q = new BigInteger(1, Convert.FromBase64String(doc.DocumentElement.GetElementsByTagName("Q")[0].InnerText));
BigInteger dp = new BigInteger(1, Convert.FromBase64String(doc.DocumentElement.GetElementsByTagName("DP")[0].InnerText));
BigInteger dq = new BigInteger(1, Convert.FromBase64String(doc.DocumentElement.GetElementsByTagName("DQ")[0].InnerText));
BigInteger qinv = new BigInteger(1, Convert.FromBase64String(doc.DocumentElement.GetElementsByTagName("InverseQ")[0].InnerText));

RsaPrivateCrtKeyParameters privateKeyParam = new RsaPrivateCrtKeyParameters(m, exp, d, p, q, dp, dq, qinv);

PrivateKeyInfo privateKeyInfo = PrivateKeyInfoFactory.CreatePrivateKeyInfo(privateKeyParam);
byte[] serializedPrivateBytes = privateKeyInfo.ToAsn1Object().GetEncoded();
return Convert.ToBase64String(serializedPrivateBytes);
}

/// <summary>
/// RSA公钥格式转换,java->.net
/// </summary>
/// <param name="publicKey">java生成的公钥</param>
/// <returns></returns>
public static string RSAPublicKeyJava2DotNet(string publicKey)
{
RsaKeyParameters publicKeyParam = (RsaKeyParameters)PublicKeyFactory.CreateKey(Convert.FromBase64String(publicKey));
return string.Format("<RSAKeyValue><Modulus>{0}</Modulus><Exponent>{1}</Exponent></RSAKeyValue>",
Convert.ToBase64String(publicKeyParam.Modulus.ToByteArrayUnsigned()),
Convert.ToBase64String(publicKeyParam.Exponent.ToByteArrayUnsigned()));
}

/// <summary>
/// RSA公钥格式转换,.net->java
/// </summary>
/// <param name="publicKey">.net生成的公钥</param>
/// <returns></returns>
public static string RSAPublicKeyDotNet2Java(string publicKey)
{
XmlDocument doc = new XmlDocument();
doc.LoadXml(publicKey);
BigInteger m = new BigInteger(1, Convert.FromBase64String(doc.DocumentElement.GetElementsByTagName("Modulus")[0].InnerText));
BigInteger p = new BigInteger(1, Convert.FromBase64String(doc.DocumentElement.GetElementsByTagName("Exponent")[0].InnerText));
RsaKeyParameters pub = new RsaKeyParameters(false, m, p);

SubjectPublicKeyInfo publicKeyInfo = SubjectPublicKeyInfoFactory.CreateSubjectPublicKeyInfo(pub);
byte[] serializedPublicBytes = publicKeyInfo.ToAsn1Object().GetDerEncoded();
return Convert.ToBase64String(serializedPublicBytes);
}
#endregion
}

}

时间: 2024-08-02 19:40:29

C#-java RSA加密解密的相关文章

Java RSA 加密解密算法 入门

一.入门闲话 最近在学javase,想拿个小题目练习.拿到一个关于socket接口实现基于TCP协议的通信(准确的说是多进程程序中通信问题.).通信过程中需要用RSA算法进行加解密. 要求进程应用软件A 键盘输入数据后和第一端口号-1存入一SendDate对象sd1中,然后将sd1进行序列化后,将序列化后的数据进行RSA加密(此处的RSA加密用最终接收方D的公钥加密).将第一次RSA加密后的数据和第二端口号P2,存入第二SendDate对象sd2中,接着再将sd2进行序列化,再将sd2 序列化后

java RSA加密解密

该工具类中用到了BASE64,需要借助第三方类库:javabase64-1.3.1. jar 注意: RSA加密明文最大长度117字节,解密要求密文最大长度为128字节,所以在加密和解密的过程中需要分块进行. RSA加密对明文的长度是有限制的,如果加密数据过大会抛出如下异常: Exception in thread "main" javax.crypto.IllegalBlockSizeException: Data must not be longer than 117 bytes

C# Java间进行RSA加密解密交互

引用:http://blog.csdn.net/dslinmy/article/details/37362661 这里,讲一下RSA算法加解密在C#和Java之间交互的问题,这两天纠结了很久,也看了很多其他人写的文章,颇受裨益,但没能解决我的实际问题,终于,还是被我捣鼓出来了. 首先,介绍一下写这代码的目的:完成webService验证问题,服务器端采用C#开发,客户端采用Java开发.服务器端给客户端提供公钥,已进行数据加密,客户端加密后提数据提交给服务器,服务器用私钥对数据解密,进行验证.

JAVA实现RSA加密解密 非对称算法

首先RSA是一个非对称的加密算法,所以在使用该算法加密解密之前,必须先行生成密钥对,包括公钥和私钥 JDK中提供了生成密钥对的类KeyPairGenerator,实例如下: public static Map<String, Object> genKeyPair() throws Exception { // 获取公钥私钥密钥对的生成器 KeyPairGenerator kpg = KeyPairGenerator.getInstance("RSA"); // 初始化确定密

Java使用RSA加密解密签名及校验

由于项目要用到非对称加密解密签名校验什么的,于是参考<Java加密解密的艺术>写一个RSA进行加密解密签名及校验的Demo,代码很简单,特此分享! RSA加密解密类: package com.ihep; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; imp

RSA加密解密及数字签名Java实现--转

RSA公钥加密算法是1977年由罗纳德·李维斯特(Ron Rivest).阿迪·萨莫尔(Adi Shamir)和伦纳德·阿德曼(Leonard Adleman)一起提出的.当时他们三人都在麻省理工学院工作.RSA就是他们三人姓氏开头字母拼在一起组成的. RSA是目前最有影响力的公钥加密算法,它能够抵抗到目前为止已知的绝大多数密码攻击,已被ISO推荐为公钥数据加密算法. RSA算法是一种非对称密码算法,所谓非对称,就是指该算法需要一对密钥,使用其中一个加密,则需要用另一个才能解密. 关于RSA算法

RSA加密解密及数字签名Java实现

RSA公钥加密算法是1977年由罗纳德·李维斯特(Ron Rivest).阿迪·萨莫尔(Adi Shamir)和伦纳德·阿德曼(Leonard Adleman)一起提出的.当时他们三人都在麻省理工学院工作.RSA就是他们三人姓氏开头字母拼在一起组成的.RSA是目前最有影响力的公钥加密算法,它能够抵抗到目前为止已知的绝大多数密码攻击,已被ISO推荐为公钥数据加密算法.RSA算法是一种非对称密码算法,所谓非对称,就是指该算法需要一对密钥,使用其中一个加密,则需要用另一个才能解密.关于RSA算法的原理

C# 与JAVA 的RSA 加密解密交互,互通,C#使用BouncyCastle来实现私钥加密,公钥解密的方法

因为C#的RSA加密解密只有公钥加密,私钥解密,没有私钥加密,公钥解密.在网上查了很久也没有很好的实现.BouncyCastle的文档少之又少.很多人可能会说,C#也是可以的,通过Biginteger开源类来实现,不过那个是有一个文章,不过他加密出来的是16进制结果的.根本不能和JAVA互通.连加密出来的都不和C#原生的加密出来的结果格式一样.所以还是没有好的解决方法. 接下来还是不断的找资料,找方法.找朋友找同事.个个都找.问题是有的,方法也是有的,所以总结各路大神之后写了这个类.实现了私钥加

RSA加密解密及RSA加签验签

RSA安全性应用场景说明 在刚接触RSA的时候,会混淆RSA加密解密和RSA加签验签的概念.简单来说加密解密是公钥加密私钥解密,持有公钥(多人持有)可以对数据加密,但是只有持有私钥(一人持有)才可以解密并查看数据:加签验签是私钥加签公钥验签,持有私钥(一人持有)可以加签,持有公钥(多人持有)可以验签. 在金融行业在设计到数据交互传输的时候,需要考虑数据的安全性问题.下文通过介绍RSA的加密和加签两个特性,说明RSA加密技术在保障数据传输过程中的安全性以及实现数据的防篡改和防否机制的应用场景及代码