using System;
using System.IO;
using System.Security.Cryptography;
using System.Text;
namespace Masuit.Tools.Security;
///
/// RSA加密解密及RSA签名和验证
///
public static class RsaCrypt
{
private static RsaKey RsaKey;
#region RSA 加密解密
#region RSA 的密钥产生
///
/// 生成 RSA 公钥和私钥
///
/// 密钥类型
/// 密钥长度
///
public static RsaKey GenerateRsaKeys(RsaKeyType type = RsaKeyType.PKCS8, int length = 1024)
{
var rsa = new RSA(length);
return type switch
{
RsaKeyType.PKCS1 => RsaKey ??= new RsaKey
{
PrivateKey = rsa.ToPEM_PKCS1(),
PublicKey = rsa.ToPEM_PKCS1(true)
},
RsaKeyType.PKCS8 => RsaKey ??= new RsaKey
{
PrivateKey = rsa.ToPEM_PKCS8(),
PublicKey = rsa.ToPEM_PKCS8(true)
},
RsaKeyType.XML => RsaKey ??= new RsaKey
{
PrivateKey = rsa.ToXML(),
PublicKey = rsa.ToXML(true)
},
_ => throw new ArgumentOutOfRangeException(nameof(type), type, null)
};
}
#endregion RSA 的密钥产生
#region RSA的加密函数
///
/// RSA的加密函数 string
///
/// 公钥
/// 需要加密的字符串
/// 加密后的内容
/// The cryptographic service provider (CSP) cannot be acquired.
public static string RSAEncrypt(this string value, string publicKey)
{
var rsa = new RSA(publicKey);
return rsa.Encrypt(value);
}
///
/// RSA的加密函数 string
///
/// 需要加密的字符串
/// 加密后的内容
/// The cryptographic service provider (CSP) cannot be acquired.
public static string RSAEncrypt(this string value)
{
return RSAEncrypt(value, RsaKey.PublicKey);
}
///
/// RSA的加密函数 byte[]
///
/// 需要加密的字节数组
/// 公钥
/// 加密后的内容
/// The cryptographic service provider (CSP) cannot be acquired.
public static string RSAEncrypt(this byte[] data, string publicKey)
{
var rsa = new RSA(publicKey);
return Convert.ToBase64String(rsa.Encrypt(data));
}
///
/// RSA的加密函数 byte[]
///
/// 需要加密的字节数组
/// 加密后的内容
/// The cryptographic service provider (CSP) cannot be acquired.
public static string RSAEncrypt(this byte[] data)
{
return RSAEncrypt(data, RsaKey.PublicKey);
}
#endregion RSA的加密函数
#region RSA的解密函数
///
/// RSA的解密函数 string
///
/// 需要解密的字符串
/// 私钥
/// 解密后的内容
/// The cryptographic service provider (CSP) cannot be acquired.
public static string RSADecrypt(this string value, string privateKey)
{
var rsa = new RSA(privateKey);
return rsa.DecryptOrNull(value);
}
///
/// RSA的解密函数 string
///
/// 需要解密的字符串
/// 解密后的内容
/// The cryptographic service provider (CSP) cannot be acquired.
public static string RSADecrypt(this string value)
{
return RSADecrypt(value, RsaKey.PrivateKey);
}
///
/// RSA的解密函数 byte
///
/// 需要解密的字符串
/// 私钥
/// 解密后的内容
/// The cryptographic service provider (CSP) cannot be acquired.
public static string RSADecrypt(this byte[] data, string privateKey)
{
var rsa = new RSA(privateKey);
return new UnicodeEncoding().GetString(rsa.DecryptOrNull(data));
}
///
/// RSA的解密函数 byte
///
/// 需要解密的字符串
/// 解密后的内容
/// The cryptographic service provider (CSP) cannot be acquired.
public static string RSADecrypt(this byte[] data)
{
return RSADecrypt(data, RsaKey.PrivateKey);
}
#endregion RSA的解密函数
#endregion RSA 加密解密
#region RSA数字签名
#region 获取Hash描述表
///
/// 获取Hash描述表
///
/// 源数据
/// Hash描述表
public static byte[] GetHashBytes(this string value)
{
//从字符串中取得Hash描述
using var md5 = MD5.Create();
var buffer = Encoding.UTF8.GetBytes(value);
return md5.ComputeHash(buffer);
}
///
/// 获取Hash描述表
///
/// 源数据
/// Hash描述表
public static string GetHashString(this string value)
{
//从字符串中取得Hash描述
using var md5 = MD5.Create();
var buffer = Encoding.UTF8.GetBytes(value);
var hashData = md5.ComputeHash(buffer);
return Convert.ToBase64String(hashData);
}
///
/// 从文件流获取Hash描述表
///
/// 源文件
/// Hash描述表
public static byte[] GetHashBytes(this FileStream file)
{
//从文件中取得Hash描述
using var md5 = MD5.Create();
return md5.ComputeHash(file);
}
///
/// 从文件流获取Hash描述表
///
/// 源文件
/// Hash描述表
public static string GetHashString(this FileStream file)
{
//从文件中取得Hash描述
using var md5 = MD5.Create();
var hashData = md5.ComputeHash(file);
return Convert.ToBase64String(hashData);
}
#endregion 获取Hash描述表
#region RSA签名
///
/// RSA签名
///
/// 签名字节数据
/// 私钥
/// hash算法
/// 处理结果
/// The cryptographic service provider (CSP) cannot be acquired.
/// The key is null.-or- The hash algorithm is null.
public static byte[] SignatureBytes(this byte[] data, string privateKey, HashAlgo halg = HashAlgo.MD5)
{
var rsa = new RSA(privateKey);
return rsa.Sign(halg.ToString(), data);
}
///
/// RSA签名
///
/// 签名字节数据
/// 私钥
/// 处理结果
/// The cryptographic service provider (CSP) cannot be acquired.
/// The key is null.-or- The hash algorithm is null.
public static string SignatureString(this byte[] data, string privateKey)
{
return Convert.ToBase64String(SignatureBytes(data, privateKey));
}
///
/// RSA签名
///
/// 签名字符串数据
/// 私钥
/// hash算法
/// 处理结果
/// The cryptographic service provider (CSP) cannot be acquired.
/// The key is null.-or- The hash algorithm is null.
public static byte[] SignatureBytes(this string value, string privateKey, HashAlgo halg = HashAlgo.MD5)
{
var rsa = new RSA(privateKey);
return Encoding.UTF32.GetBytes(rsa.Sign(halg.ToString(), value));
}
///
/// RSA签名
///
/// 签名字符串数据
/// 私钥
/// hash算法
/// 处理结果
/// The cryptographic service provider (CSP) cannot be acquired.
/// The key is null.-or- The hash algorithm is null.
public static string SignatureString(this string value, string privateKey, HashAlgo halg = HashAlgo.MD5)
{
var rsa = new RSA(privateKey);
return rsa.Sign(halg.ToString(), value);
}
#endregion RSA签名
#region RSA 签名验证
///
/// RSA 签名验证
///
/// 反格式化字节数据
/// 公钥
/// 哈希字节数据
/// hash算法
/// 处理结果
/// The cryptographic service provider (CSP) cannot be acquired.
/// The key is null.-or- The hash algorithm is null.
public static bool SignatureDeformatter(this byte[] data, string publicKey, byte[] sign, HashAlgo halg = HashAlgo.MD5)
{
var rsa = new RSA(publicKey);
return rsa.Verify(halg.ToString(), sign, data);
}
///
/// RSA 签名验证
///
/// 反格式化字节数据
/// 公钥
/// 哈希字符串数据
/// hash算法
/// 处理结果
/// The cryptographic service provider (CSP) cannot be acquired.
/// The key is null.-or- The hash algorithm is null.
public static bool SignatureDeformatter(this byte[] data, string publicKey, string sign, HashAlgo halg = HashAlgo.MD5)
{
var rsa = new RSA(publicKey);
return rsa.Verify(halg.ToString(), Convert.FromBase64String(sign), data);
}
///
/// RSA 签名验证
///
/// 反格式化字符串数据
/// 公钥
/// 哈希字节数据
/// hash算法
/// 处理结果
/// The cryptographic service provider (CSP) cannot be acquired.
/// The key is null.-or- The hash algorithm is null.
public static bool SignatureDeformatter(this string value, string publicKey, byte[] sign, HashAlgo halg = HashAlgo.MD5)
{
var rsa = new RSA(publicKey);
return rsa.Verify(halg.ToString(), sign, Convert.FromBase64String(value));
}
///
/// RSA 签名验证
///
/// 格式字符串数据
/// 公钥
/// 哈希字符串数据
/// hash算法
/// 处理结果
/// The cryptographic service provider (CSP) cannot be acquired.
/// The key is null.-or- The hash algorithm is null.
public static bool SignatureDeformatter(this string value, string publicKey, string sign, HashAlgo halg = HashAlgo.MD5)
{
var rsa = new RSA(publicKey);
return rsa.Verify(halg.ToString(), sign, value);
}
#endregion RSA 签名验证
#endregion RSA数字签名
}