我需要一段JAVA与.NET通用的RSA2加解密.NET代码
时间: 2023-03-08 08:15:12 浏览: 91
JAVA实现RSA加解密
4星 · 用户满意度95%
这里有一段C#代码,可以用于在JAVA和.NET之间进行RSA2加解密: using System;
using System.Security.Cryptography; public class RSA2
{
//RSA2加密
public static string RSAEncrypt(string plainText, string publicKeyString)
{
byte[] plainBytes = System.Text.Encoding.UTF8.GetBytes(plainText);
byte[] publicKeyBytes = Convert.FromBase64String(publicKeyString);
RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();
rsa.ImportCspBlob(publicKeyBytes);
byte[] encryptedBytes = rsa.Encrypt(plainBytes, false);
return Convert.ToBase64String(encryptedBytes);
}
//RSA2解密
public static string RSADecrypt(string encryptedText, string privateKeyString)
{
byte[] encryptedBytes = Convert.FromBase64String(encryptedText);
byte[] privateKeyBytes = Convert.FromBase64String(privateKeyString);
RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();
rsa.ImportCspBlob(privateKeyBytes);
byte[] plainBytes = rsa.Decrypt(encryptedBytes, false);
return System.Text.Encoding.UTF8.GetString(plainBytes);
}
}
阅读全文