Unity使用本地PFX证书对字符串进行SHA256WithRSA加密的C#代码示例
在Unity中,我们可以通过使用本地PFX证书来实现对字符串的SHA256WithRSA加密。下面是一个C#代码示例,已封装成工具类,可以直接调用使用:
// 导入所需的命名空间
using System.Security.Cryptography;
using System.Security.Cryptography.X509Certificates;
using System.Text;
public class RSAEncryptionTool
{
// 使用本地PFX证书对字符串进行SHA256WithRSA加密的方法
public static string EncryptWithPFX(string pfxFilePath, string pfxPassword, string data)
{
// 加载PFX证书
X509Certificate2 certificate = new X509Certificate2(pfxFilePath, pfxPassword);
using (RSA rsa = certificate.GetRSAPrivateKey())
{
// 将字符串转换为字节数组
byte[] dataBytes = Encoding.UTF8.GetBytes(data);
// 执行SHA256WithRSA加密
byte[] encryptedBytes = rsa.SignData(dataBytes, HashAlgorithmName.SHA256, RSASignaturePadding.Pkcs1);
// 返回加密后的Base64字符串
return Convert.ToBase64String(encryptedBytes);
}
}
}
// 调用示例
string pfxFilePath = "path/to/pfx/file.pfx";
string pfxPassword = "your_password";
string data = "需要加密的字符串";
string encryptedData = RSAEncryptionTool.EncryptWithPFX(pfxFilePath, pfxPassword, data);
Debug.Log("加密后的字符串:" + encryptedData);
下载地址
用户评论