如何在ubuntu中使用rsacryptserviceprovider()解密c#中的加密数据?
我正在使用c#中的如何在ubuntu中使用rsacryptserviceprovider()解密c#中的加密数据?,c#,encryption,mono,cryptography,rsa,C#,Encryption,Mono,Cryptography,Rsa,我正在使用c#中的rsacyptoserviceprovider()class对数据进行加密。我想解密ubuntu中的数据,这些数据是用c#加密的。你能告诉我解密需要遵循哪种机制吗。以下函数用于加密: public static void Encrypt(String PublicKey, String plainText, out String cipherText) { try { int dwKeySize = 1024;
rsacyptoserviceprovider()
class对数据进行加密。我想解密ubuntu中的数据,这些数据是用c#加密的。你能告诉我解密需要遵循哪种机制吗。以下函数用于加密:
public static void Encrypt(String PublicKey, String plainText, out String cipherText)
{
try
{
int dwKeySize = 1024;
// TODO: Add Proper Exception Handlers
RSACryptoServiceProvider rsaCryptoServiceProvider = new RSACryptoServiceProvider(dwKeySize);
rsaCryptoServiceProvider.FromXmlString(PublicKey);
int keySize = dwKeySize / 8;
byte[] bytes = Encoding.UTF32.GetBytes(plainText);
// The hash function in use by the .NET RSACryptoServiceProvider here is SHA1
// int maxLength = ( keySize ) - 2 - ( 2 * SHA1.Create().ComputeHash( rawBytes ).Length );
int maxLength = keySize - 42;
int dataLength = bytes.Length;
int iterations = dataLength / maxLength;
StringBuilder stringBuilder = new StringBuilder();
for (int i = 0; i <= iterations; i++)
{
byte[] tempBytes = new byte[(dataLength - maxLength * i > maxLength) ? maxLength : dataLength - maxLength * i];
Buffer.BlockCopy(bytes, maxLength * i, tempBytes, 0, tempBytes.Length);
byte[] encryptedBytes = rsaCryptoServiceProvider.Encrypt(tempBytes, true);
// Be aware the RSACryptoServiceProvider reverses the order
// of encrypted bytes after encryption and before decryption.
// If you do not require compatibility with Microsoft Cryptographic API
// (CAPI) and/or other vendors.
// Comment out the next line and the corresponding one in the
// DecryptString function.
Array.Reverse(encryptedBytes);
// Why convert to base 64?
// Because it is the largest power-of-two base printable using only ASCII characters
stringBuilder.Append(Convert.ToBase64String(encryptedBytes));
}
cipherText = stringBuilder.ToString();
}
catch (Exception e)
{
cipherText = "ERROR_STRING";
Console.WriteLine("Exception in RSA Encrypt: " + e.Message);
//throw new Exception("Exception occured while RSA Encryption" + e.Message);
}
}
公共静态无效加密(字符串公钥、字符串明文、字符串外密文)
{
尝试
{
int-dwKeySize=1024;
//TODO:添加适当的异常处理程序
rsacryptserviceprovider rsacryptserviceprovider=新的rsacryptserviceprovider(dwKeySize);
rsacyptoserviceprovider.FromXmlString(公钥);
int keySize=dwKeySize/8;
byte[]bytes=Encoding.UTF32.GetBytes(纯文本);
//此处.NET RSACryptoServiceProvider使用的哈希函数是SHA1
//int maxLength=(keySize)-2-(2*SHA1.Create().ComputeHash(rawBytes.Length);
int maxLength=keySize-42;
int dataLength=bytes.Length;
int迭代次数=dataLength/maxLength;
StringBuilder StringBuilder=新的StringBuilder();
对于(int i=0;i maxLength)?maxLength:dataLength-maxLength*i];
Buffer.BlockCopy(字节,maxLength*i,tempBytes,0,tempBytes.Length);
byte[]encryptedBytes=RSACryptServiceProvider.Encrypt(tempBytes,true);
//请注意,RSACryptServiceProvider会撤销订单
//加密后和解密前的加密字节数。
//如果不要求与Microsoft加密API兼容
//(CAPI)和/或其他供应商。
//注释掉下一行和表中相应的一行
//解密字符串函数。
Array.Reverse(encryptedBytes);
//为什么要转换为基数64?
//因为它是仅使用ASCII字符的两个基本可打印字符的最大幂
追加(Convert.ToBase64String(encryptedBytes));
}
cipherText=stringBuilder.ToString();
}
捕获(例外e)
{
cipherText=“错误\字符串”;
Console.WriteLine(“RSA加密中的异常:+e.Message”);
//抛出新异常(“RSA加密时发生异常”+e.Message);
}
}
不要那样使用RSA。它不应该这样使用,而且它太慢了
正确的方法是使用对称算法,例如AES,并对RSA使用的密钥进行加密。请参阅my for C#代码以了解如何做到这一点。您需要相同的机制,但相反。先试试,以后再问。考虑过在Ubuntu安装中使用Mono,并在C#中创建一个等效的解密方法?这里的实际问题是什么?使用RSA加密多个块有什么原因吗?它慢得像地狱,有一些甜蜜的攻击。。。