mirror of
https://github.com/ldqk/Masuit.Tools.git
synced 2026-10-29 04:37:01 +08:00
1572 lines
58 KiB
C#
1572 lines
58 KiB
C#
using Masuit.Tools.Systems;
|
|
using System;
|
|
using System.IO;
|
|
using System.Security.Cryptography;
|
|
using System.Text;
|
|
using System.Text.RegularExpressions;
|
|
|
|
namespace Masuit.Tools.Security;
|
|
|
|
/// <summary>
|
|
/// 常用的加密解密算法
|
|
/// </summary>
|
|
public static class Encrypt
|
|
{
|
|
#region DES对称加密解密
|
|
|
|
/// <summary>
|
|
/// 加密字符串
|
|
/// 加密密钥必须为8位
|
|
/// </summary>
|
|
/// <param name="strText">被加密的字符串</param>
|
|
/// <param name="strEncrKey">8位长度密钥</param>
|
|
/// <returns>加密后的数据</returns>
|
|
public static string DesEncrypt(this string strText, string strEncrKey)
|
|
{
|
|
if (strEncrKey.Length < 8)
|
|
{
|
|
throw new Exception("密钥长度无效,密钥必须是8位!");
|
|
}
|
|
|
|
using var des = DES.Create();
|
|
byte[] inputByteArray = Encoding.Default.GetBytes(strText);
|
|
des.Key = Encoding.ASCII.GetBytes(strEncrKey.Substring(0, 8));
|
|
des.IV = Encoding.ASCII.GetBytes(strEncrKey.Substring(0, 8));
|
|
using var encryptor = des.CreateEncryptor();
|
|
return ToHexString(encryptor.TransformFinalBlock(inputByteArray, 0, inputByteArray.Length));
|
|
}
|
|
|
|
/// <summary>
|
|
/// 加密字符串
|
|
/// 加密密钥必须为8位
|
|
/// </summary>
|
|
/// <param name="strText">被加密的字符串</param>
|
|
/// <param name="desKey"></param>
|
|
/// <param name="desIV"></param>
|
|
/// <returns>加密后的数据</returns>
|
|
public static string DesEncrypt(this string strText, byte[] desKey, byte[] desIV)
|
|
{
|
|
using var des = DES.Create();
|
|
byte[] inputByteArray = Encoding.Default.GetBytes(strText);
|
|
des.Key = desKey;
|
|
des.IV = desIV;
|
|
using var encryptor = des.CreateEncryptor();
|
|
return ToHexString(encryptor.TransformFinalBlock(inputByteArray, 0, inputByteArray.Length));
|
|
}
|
|
|
|
/// <summary>
|
|
/// DES加密文件
|
|
/// </summary>
|
|
/// <param name="fin">文件输入流</param>
|
|
/// <param name="outFilePath">文件输出路径</param>
|
|
/// <param name="strEncrKey">加密密钥</param>
|
|
public static void DesEncrypt(this FileStream fin, string outFilePath, string strEncrKey)
|
|
{
|
|
byte[] iv = { 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF };
|
|
var byKey = Encoding.UTF8.GetBytes(strEncrKey.Substring(0, 8));
|
|
using var fout = new FileStream(outFilePath, FileMode.OpenOrCreate, FileAccess.Write);
|
|
fout.SetLength(0);
|
|
using var des = DES.Create();
|
|
using var encStream = new CryptoStream(fout, des.CreateEncryptor(byKey, iv), CryptoStreamMode.Write);
|
|
fin.CopyTo(encStream);
|
|
}
|
|
|
|
/// <summary>
|
|
/// DES加密文件
|
|
/// </summary>
|
|
/// <param name="fin">文件输入流</param>
|
|
/// <param name="outFilePath">文件输出路径</param>
|
|
/// <param name="desKey"></param>
|
|
/// <param name="desIV"></param>
|
|
public static void DesEncrypt(this FileStream fin, string outFilePath, byte[] desKey, byte[] desIV)
|
|
{
|
|
using var fout = new FileStream(outFilePath, FileMode.Create, FileAccess.Write);
|
|
using var des = DES.Create();
|
|
using var encStream = new CryptoStream(fout, des.CreateEncryptor(desKey, desIV), CryptoStreamMode.Write);
|
|
fin.CopyTo(encStream);
|
|
}
|
|
|
|
/// <summary>
|
|
/// DES解密文件
|
|
/// </summary>
|
|
/// <param name="fin">输入文件流</param>
|
|
/// <param name="outFilePath">文件输出路径</param>
|
|
/// <param name="sDecrKey">解密密钥</param>
|
|
public static void DesDecrypt(this FileStream fin, string outFilePath, string sDecrKey)
|
|
{
|
|
byte[] iv = { 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF };
|
|
var byKey = Encoding.UTF8.GetBytes(sDecrKey.Substring(0, 8));
|
|
using var fout = new FileStream(outFilePath, FileMode.Create, FileAccess.Write);
|
|
using var des = DES.Create();
|
|
using var encStream = new CryptoStream(fout, des.CreateDecryptor(byKey, iv), CryptoStreamMode.Write);
|
|
fin.CopyTo(encStream);
|
|
}
|
|
|
|
/// <summary>
|
|
/// DES解密文件
|
|
/// </summary>
|
|
/// <param name="fin">输入文件流</param>
|
|
/// <param name="outFilePath">文件输出路径</param>
|
|
/// <param name="desKey"></param>
|
|
/// <param name="desIV"></param>
|
|
public static void DesDecrypt(this FileStream fin, string outFilePath, byte[] desKey, byte[] desIV)
|
|
{
|
|
using var fout = new FileStream(outFilePath, FileMode.Create, FileAccess.Write);
|
|
using var des = DES.Create();
|
|
using var encStream = new CryptoStream(fout, des.CreateDecryptor(desKey, desIV), CryptoStreamMode.Write);
|
|
fin.CopyTo(encStream);
|
|
}
|
|
|
|
/// <summary>
|
|
/// DES解密算法
|
|
/// 密钥为8位
|
|
/// </summary>
|
|
/// <param name="pToDecrypt">需要解密的字符串</param>
|
|
/// <param name="sKey">密钥</param>
|
|
/// <returns>解密后的数据</returns>
|
|
public static string DesDecrypt(this string pToDecrypt, string sKey)
|
|
{
|
|
if (sKey.Length < 8)
|
|
{
|
|
throw new Exception("密钥长度无效,密钥必须是8位!");
|
|
}
|
|
|
|
using var des = DES.Create();
|
|
byte[] inputByteArray = FromHexString(pToDecrypt);
|
|
des.Key = Encoding.ASCII.GetBytes(sKey.Substring(0, 8));
|
|
des.IV = Encoding.ASCII.GetBytes(sKey.Substring(0, 8));
|
|
using var decryptor = des.CreateDecryptor();
|
|
return Encoding.Default.GetString(decryptor.TransformFinalBlock(inputByteArray, 0, inputByteArray.Length));
|
|
}
|
|
|
|
/// <summary>
|
|
/// DES解密算法
|
|
/// 密钥为8位
|
|
/// </summary>
|
|
/// <param name="pToDecrypt">需要解密的字符串</param>
|
|
/// <param name="desKey"></param>
|
|
/// <param name="desIV"></param>
|
|
/// <returns>解密后的数据</returns>
|
|
public static string DesDecrypt(this string pToDecrypt, byte[] desKey, byte[] desIV)
|
|
{
|
|
using var des = DES.Create();
|
|
byte[] inputByteArray = FromHexString(pToDecrypt);
|
|
des.Key = desKey;
|
|
des.IV = desIV;
|
|
using var decryptor = des.CreateDecryptor();
|
|
return Encoding.Default.GetString(decryptor.TransformFinalBlock(inputByteArray, 0, inputByteArray.Length));
|
|
}
|
|
|
|
private static string ToHexString(byte[] data) => BitConverter.ToString(data).Replace("-", string.Empty);
|
|
|
|
private static byte[] FromHexString(string hex)
|
|
{
|
|
if (hex.Length % 2 != 0)
|
|
{
|
|
throw new FormatException("加密数据必须由成对的十六进制字符组成。");
|
|
}
|
|
|
|
var bytes = new byte[hex.Length / 2];
|
|
for (int i = 0; i < bytes.Length; i++)
|
|
{
|
|
bytes[i] = Convert.ToByte(hex.Substring(i * 2, 2), 16);
|
|
}
|
|
|
|
return bytes;
|
|
}
|
|
|
|
#endregion DES对称加密解密
|
|
|
|
#region 对称加密算法AES加密解密
|
|
|
|
private static readonly byte[] AesEnvelopeMagic = Encoding.ASCII.GetBytes("MTA1");
|
|
private static readonly byte[] AesAuthenticationContext = Encoding.UTF8.GetBytes("Masuit.Tools AES-CBC-HMAC v1");
|
|
private const int AesIvSize = 16;
|
|
private const int AesTagSize = 32;
|
|
private const int AesEnvelopeHeaderSize = 4 + AesIvSize;
|
|
|
|
/// <summary>
|
|
/// 生成符合AES加密规则的密钥
|
|
/// </summary>
|
|
/// <param name="length"></param>
|
|
/// <returns></returns>
|
|
public static string GenerateAesKey(int length)
|
|
{
|
|
using var aes = Aes.Create();
|
|
aes.KeySize = length;
|
|
aes.GenerateKey();
|
|
return Convert.ToBase64String(aes.Key);
|
|
}
|
|
|
|
#if NET8_0_OR_GREATER
|
|
private const int AesGcmNonceSize = 12;
|
|
private const int AesGcmTagSize = 16;
|
|
|
|
/// <summary>
|
|
/// 使用 AES-GCM 加密字符串。返回值为 Base64 编码的 nonce、认证标签和密文。
|
|
/// </summary>
|
|
/// <remarks>该方法仅在 .NET Standard 2.1 及以上目标框架中提供。</remarks>
|
|
/// <param name="plainText">待加密字符串</param>
|
|
/// <param name="key">UTF-8 编码后长度为 16、24 或 32 字节的 AES 密钥</param>
|
|
/// <param name="associatedData">可选的附加认证数据,解密时必须提供相同数据</param>
|
|
/// <returns>Base64 编码的加密结果</returns>
|
|
public static string AesGcmEncrypt(this string plainText, string key, byte[] associatedData = null)
|
|
{
|
|
if (plainText == null)
|
|
{
|
|
throw new ArgumentNullException(nameof(plainText));
|
|
}
|
|
|
|
if (key == null)
|
|
{
|
|
throw new ArgumentNullException(nameof(key));
|
|
}
|
|
|
|
byte[] keyBytes = Encoding.UTF8.GetBytes(key);
|
|
byte[] nonce = new byte[AesGcmNonceSize];
|
|
using (var randomNumberGenerator = RandomNumberGenerator.Create())
|
|
{
|
|
randomNumberGenerator.GetBytes(nonce);
|
|
}
|
|
|
|
byte[] plainBytes = Encoding.UTF8.GetBytes(plainText);
|
|
byte[] cipherBytes = new byte[plainBytes.Length];
|
|
byte[] tag = new byte[AesGcmTagSize];
|
|
using (var aesGcm = new AesGcm(keyBytes, AesGcmTagSize))
|
|
{
|
|
aesGcm.Encrypt(nonce, plainBytes, cipherBytes, tag, associatedData ?? Array.Empty<byte>());
|
|
}
|
|
|
|
byte[] result = new byte[nonce.Length + tag.Length + cipherBytes.Length];
|
|
Buffer.BlockCopy(nonce, 0, result, 0, nonce.Length);
|
|
Buffer.BlockCopy(tag, 0, result, nonce.Length, tag.Length);
|
|
Buffer.BlockCopy(cipherBytes, 0, result, nonce.Length + tag.Length, cipherBytes.Length);
|
|
return Convert.ToBase64String(result);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 使用 AES-GCM 解密字符串。输入须为 AES-GCM 加密方法生成的 Base64 字符串。
|
|
/// </summary>
|
|
/// <remarks>该方法仅在 .NET Standard 2.1 及以上目标框架中提供。</remarks>
|
|
/// <param name="cipherText">Base64 编码的 nonce、认证标签和密文</param>
|
|
/// <param name="key">UTF-8 编码后长度为 16、24 或 32 字节的 AES 密钥</param>
|
|
/// <param name="associatedData">加密时使用的附加认证数据</param>
|
|
/// <returns>解密后的字符串;认证失败时抛出 <see cref="CryptographicException"/></returns>
|
|
public static string AesGcmDecrypt(this string cipherText, string key, byte[] associatedData = null)
|
|
{
|
|
if (cipherText == null)
|
|
{
|
|
throw new ArgumentNullException(nameof(cipherText));
|
|
}
|
|
|
|
if (key == null)
|
|
{
|
|
throw new ArgumentNullException(nameof(key));
|
|
}
|
|
|
|
byte[] keyBytes = Encoding.UTF8.GetBytes(key);
|
|
byte[] encryptedBytes = Convert.FromBase64String(cipherText);
|
|
if (encryptedBytes.Length < AesGcmNonceSize + AesGcmTagSize)
|
|
{
|
|
throw new CryptographicException("AES-GCM 密文格式无效。");
|
|
}
|
|
|
|
byte[] nonce = new byte[AesGcmNonceSize];
|
|
byte[] tag = new byte[AesGcmTagSize];
|
|
int cipherLength = encryptedBytes.Length - nonce.Length - tag.Length;
|
|
byte[] cipherBytes = new byte[cipherLength];
|
|
Buffer.BlockCopy(encryptedBytes, 0, nonce, 0, nonce.Length);
|
|
Buffer.BlockCopy(encryptedBytes, nonce.Length, tag, 0, tag.Length);
|
|
Buffer.BlockCopy(encryptedBytes, nonce.Length + tag.Length, cipherBytes, 0, cipherLength);
|
|
byte[] plainBytes = new byte[cipherLength];
|
|
using (var aesGcm = new AesGcm(keyBytes, AesGcmTagSize))
|
|
{
|
|
aesGcm.Decrypt(nonce, cipherBytes, tag, plainBytes, associatedData ?? Array.Empty<byte>());
|
|
}
|
|
|
|
return Encoding.UTF8.GetString(plainBytes);
|
|
}
|
|
|
|
#endif
|
|
|
|
/// <summary>
|
|
/// 对称加密算法AES(块式加密算法)
|
|
/// </summary>
|
|
/// <param name="encryptString">待加密字符串</param>
|
|
/// <param name="encryptKey">加密密钥,须半角字符</param>
|
|
/// <param name="mode">加密模式</param>
|
|
/// <returns>包含版本标识、随机IV、密文和认证标签的Base64字符串</returns>
|
|
/// <remarks>仅支持CBC模式;输出格式与旧版固定IV密文不兼容。</remarks>
|
|
public static string AESEncrypt(this string encryptString, string encryptKey, CipherMode mode = CipherMode.CBC)
|
|
{
|
|
return AesEncrypt(Encoding.UTF8.GetBytes(encryptString), Encoding.UTF8.GetBytes(encryptKey), mode);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 对称加密算法AES加密(块式加密算法)
|
|
/// </summary>
|
|
/// <param name="encryptString">待加密字符串</param>
|
|
/// <param name="options">加密选项</param>
|
|
/// <returns>包含版本标识、随机IV、密文和认证标签的Base64字符串</returns>
|
|
/// <remarks>仅支持CBC模式;输出格式与旧版固定IV密文不兼容。</remarks>
|
|
public static string AESEncrypt(this string encryptString, Aes options)
|
|
{
|
|
return AesEncrypt(Encoding.UTF8.GetBytes(encryptString), options.Key, options.Mode);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 对称加密算法AES加密(块式加密算法)
|
|
/// </summary>
|
|
/// <param name="encryptString">待加密字符串</param>
|
|
/// <param name="encryptKey">加密密钥,须半角字符</param>
|
|
/// <param name="mode">加密模式</param>
|
|
/// <returns>包含版本标识、随机IV、密文和认证标签的Base64字符串</returns>
|
|
/// <remarks>仅支持CBC模式;输出格式与旧版固定IV密文不兼容。</remarks>
|
|
public static string AESEncrypt(this string encryptString, byte[] encryptKey, CipherMode mode = CipherMode.CBC)
|
|
{
|
|
return AesEncrypt(Encoding.UTF8.GetBytes(encryptString), encryptKey, mode);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 对称加密算法AES解密字符串
|
|
/// </summary>
|
|
/// <param name="decryptString">待解密的字符串</param>
|
|
/// <param name="decryptKey">解密密钥,和加密密钥相同</param>
|
|
/// <param name="mode">加密模式</param>
|
|
/// <returns>解密后的字符串</returns>
|
|
/// <remarks>仅接受当前版本的认证密文;旧版固定IV密文需在升级前迁移。</remarks>
|
|
public static string AESDecrypt(this string decryptString, string decryptKey, CipherMode mode = CipherMode.CBC)
|
|
{
|
|
return Encoding.UTF8.GetString(AesDecrypt(decryptString, Encoding.UTF8.GetBytes(decryptKey), mode));
|
|
}
|
|
|
|
/// <summary>
|
|
/// 对称加密算法AES解密字符串
|
|
/// </summary>
|
|
/// <param name="decryptString">待解密的字符串</param>
|
|
/// <param name="options">加密选项</param>
|
|
/// <returns>解密后的字符串</returns>
|
|
/// <remarks>仅接受当前版本的认证密文;旧版固定IV密文需在升级前迁移。</remarks>
|
|
public static string AESDecrypt(this string decryptString, Aes options)
|
|
{
|
|
return Encoding.UTF8.GetString(AesDecrypt(decryptString, options.Key, options.Mode));
|
|
}
|
|
|
|
/// <summary>
|
|
/// 对称加密算法AES解密字符串
|
|
/// </summary>
|
|
/// <param name="decryptString">待解密的字符串</param>
|
|
/// <param name="decryptKey">解密密钥,和加密密钥相同</param>
|
|
/// <param name="mode">加密模式</param>
|
|
/// <returns>解密后的字符串</returns>
|
|
/// <remarks>仅接受当前版本的认证密文;旧版固定IV密文需在升级前迁移。</remarks>
|
|
public static string AESDecrypt(this string decryptString, byte[] decryptKey, CipherMode mode = CipherMode.CBC)
|
|
{
|
|
return Encoding.UTF8.GetString(AesDecrypt(decryptString, decryptKey, mode));
|
|
}
|
|
|
|
private static string AesEncrypt(byte[] plainText, byte[] key, CipherMode mode)
|
|
{
|
|
using var aes = Aes.Create();
|
|
aes.Key = key;
|
|
aes.Mode = mode;
|
|
aes.Padding = PaddingMode.PKCS7;
|
|
var iv = new byte[AesIvSize];
|
|
using (var randomNumberGenerator = RandomNumberGenerator.Create())
|
|
{
|
|
randomNumberGenerator.GetBytes(iv);
|
|
}
|
|
|
|
aes.IV = iv;
|
|
byte[] cipherText;
|
|
using (var encryptor = aes.CreateEncryptor())
|
|
{
|
|
cipherText = encryptor.TransformFinalBlock(plainText, 0, plainText.Length);
|
|
}
|
|
|
|
var authenticatedData = new byte[AesEnvelopeHeaderSize + cipherText.Length];
|
|
Buffer.BlockCopy(AesEnvelopeMagic, 0, authenticatedData, 0, AesEnvelopeMagic.Length);
|
|
Buffer.BlockCopy(iv, 0, authenticatedData, AesEnvelopeMagic.Length, iv.Length);
|
|
Buffer.BlockCopy(cipherText, 0, authenticatedData, AesEnvelopeHeaderSize, cipherText.Length);
|
|
byte[] tag = ComputeAesAuthenticationTag(authenticatedData, key);
|
|
var envelope = new byte[authenticatedData.Length + tag.Length];
|
|
Buffer.BlockCopy(authenticatedData, 0, envelope, 0, authenticatedData.Length);
|
|
Buffer.BlockCopy(tag, 0, envelope, authenticatedData.Length, tag.Length);
|
|
return Convert.ToBase64String(envelope);
|
|
}
|
|
|
|
private static byte[] AesDecrypt(string encryptedText, byte[] key, CipherMode mode)
|
|
{
|
|
byte[] envelope = Convert.FromBase64String(encryptedText);
|
|
int cipherLength = envelope.Length - AesEnvelopeHeaderSize - AesTagSize;
|
|
if (cipherLength < AesIvSize || cipherLength % AesIvSize != 0 || !HasAesEnvelopeMagic(envelope))
|
|
{
|
|
throw new CryptographicException("AES密文格式无效。");
|
|
}
|
|
|
|
int authenticatedLength = envelope.Length - AesTagSize;
|
|
var authenticatedData = new byte[authenticatedLength];
|
|
Buffer.BlockCopy(envelope, 0, authenticatedData, 0, authenticatedLength);
|
|
byte[] expectedTag = ComputeAesAuthenticationTag(authenticatedData, key);
|
|
var actualTag = new byte[AesTagSize];
|
|
Buffer.BlockCopy(envelope, authenticatedLength, actualTag, 0, actualTag.Length);
|
|
if (!FixedTimeEquals(expectedTag, actualTag))
|
|
{
|
|
throw new CryptographicException("AES密文认证失败。");
|
|
}
|
|
|
|
var iv = new byte[AesIvSize];
|
|
Buffer.BlockCopy(envelope, AesEnvelopeMagic.Length, iv, 0, iv.Length);
|
|
var cipherText = new byte[cipherLength];
|
|
Buffer.BlockCopy(envelope, AesEnvelopeHeaderSize, cipherText, 0, cipherText.Length);
|
|
using var aes = Aes.Create();
|
|
aes.Key = key;
|
|
aes.IV = iv;
|
|
aes.Mode = mode;
|
|
aes.Padding = PaddingMode.PKCS7;
|
|
using var decryptor = aes.CreateDecryptor();
|
|
return decryptor.TransformFinalBlock(cipherText, 0, cipherText.Length);
|
|
}
|
|
|
|
private static byte[] ComputeAesAuthenticationTag(byte[] authenticatedData, byte[] key)
|
|
{
|
|
using var hmac = new HMACSHA256(DeriveAesAuthenticationKey(key));
|
|
return hmac.ComputeHash(authenticatedData);
|
|
}
|
|
|
|
private static byte[] DeriveAesAuthenticationKey(byte[] key)
|
|
{
|
|
if (key == null)
|
|
{
|
|
throw new ArgumentNullException(nameof(key));
|
|
}
|
|
|
|
using var hmac = new HMACSHA256(key);
|
|
return hmac.ComputeHash(AesAuthenticationContext);
|
|
}
|
|
|
|
private static bool HasAesEnvelopeMagic(byte[] envelope)
|
|
{
|
|
if (envelope.Length < AesEnvelopeHeaderSize)
|
|
{
|
|
return false;
|
|
}
|
|
|
|
for (int i = 0; i < AesEnvelopeMagic.Length; i++)
|
|
{
|
|
if (envelope[i] != AesEnvelopeMagic[i])
|
|
{
|
|
return false;
|
|
}
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
private static bool FixedTimeEquals(byte[] left, byte[] right)
|
|
{
|
|
if (left.Length != right.Length)
|
|
{
|
|
return false;
|
|
}
|
|
|
|
int difference = 0;
|
|
for (int i = 0; i < left.Length; i++)
|
|
{
|
|
difference |= left[i] ^ right[i];
|
|
}
|
|
|
|
return difference == 0;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 按字节长度(按字节,一个汉字为2个字节)取得某字符串的一部分
|
|
/// </summary>
|
|
/// <param name="sourceString">源字符串</param>
|
|
/// <param name="length">所取字符串字节长度</param>
|
|
/// <param name="tailString">附加字符串(当字符串不够长时,尾部所添加的字符串,一般为"...")</param>
|
|
/// <returns>某字符串的一部分</returns>
|
|
private static string GetSubString(this string sourceString, int length, string tailString)
|
|
{
|
|
return GetSubString(sourceString, 0, length, tailString);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 按字节长度(按字节,一个汉字为2个字节)取得某字符串的一部分
|
|
/// </summary>
|
|
/// <param name="sourceString">源字符串</param>
|
|
/// <param name="startIndex">索引位置,以0开始</param>
|
|
/// <param name="length">所取字符串字节长度</param>
|
|
/// <param name="tailString">附加字符串(当字符串不够长时,尾部所添加的字符串,一般为"...")</param>
|
|
/// <returns>某字符串的一部分</returns>
|
|
private static string GetSubString(this string sourceString, int startIndex, int length, string tailString)
|
|
{
|
|
//当是日文或韩文时(注:中文的范围:\u4e00 - \u9fa5, 日文在\u0800 - \u4e00, 韩文为\xAC00-\xD7A3)
|
|
if (Regex.IsMatch(sourceString, "[\u0800-\u4e00]+") || Regex.IsMatch(sourceString, "[\xAC00-\xD7A3]+"))
|
|
{
|
|
//当截取的起始位置超出字段串长度时
|
|
if (startIndex >= sourceString.Length)
|
|
{
|
|
return string.Empty;
|
|
}
|
|
|
|
return sourceString.Substring(startIndex, length + startIndex > sourceString.Length ? (sourceString.Length - startIndex) : length);
|
|
}
|
|
|
|
//中文字符,如"中国人民abcd123"
|
|
if (length <= 0)
|
|
{
|
|
return string.Empty;
|
|
}
|
|
|
|
byte[] bytesSource = Encoding.Default.GetBytes(sourceString);
|
|
|
|
//当字符串长度大于起始位置
|
|
if (bytesSource.Length > startIndex)
|
|
{
|
|
int endIndex = bytesSource.Length;
|
|
|
|
//当要截取的长度在字符串的有效长度范围内
|
|
if (bytesSource.Length > (startIndex + length))
|
|
{
|
|
endIndex = length + startIndex;
|
|
}
|
|
else
|
|
{
|
|
//当不在有效范围内时,只取到字符串的结尾
|
|
length = bytesSource.Length - startIndex;
|
|
tailString = "";
|
|
}
|
|
|
|
var anResultFlag = new int[length];
|
|
int nFlag = 0;
|
|
|
|
//字节大于127为双字节字符
|
|
for (int i = startIndex; i < endIndex; i++)
|
|
{
|
|
if (bytesSource[i] > 127)
|
|
{
|
|
nFlag++;
|
|
if (nFlag == 3)
|
|
{
|
|
nFlag = 1;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
nFlag = 0;
|
|
}
|
|
|
|
anResultFlag[i] = nFlag;
|
|
}
|
|
|
|
//最后一个字节为双字节字符的一半
|
|
if ((bytesSource[endIndex - 1] > 127) && (anResultFlag[length - 1] == 1))
|
|
{
|
|
length++;
|
|
}
|
|
|
|
byte[] bsResult = new byte[length];
|
|
Array.Copy(bytesSource, startIndex, bsResult, 0, length);
|
|
var myResult = Encoding.Default.GetString(bsResult);
|
|
myResult += tailString;
|
|
return myResult;
|
|
}
|
|
|
|
return string.Empty;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 加密文件流
|
|
/// </summary>
|
|
/// <param name="fs">需要加密的文件流</param>
|
|
/// <param name="decryptKey">加密密钥</param>
|
|
/// <param name="mode">加密模式</param>
|
|
/// <returns>加密流</returns>
|
|
public static CryptoStream AESEncryptStrream(this FileStream fs, string decryptKey, CipherMode mode = CipherMode.CBC)
|
|
{
|
|
using var aes = Aes.Create();
|
|
aes.Key = Encoding.UTF8.GetBytes(decryptKey);
|
|
aes.IV = AesEnvelopeMagic;
|
|
aes.Mode = mode;
|
|
return new CryptoStream(fs, aes.CreateEncryptor(), CryptoStreamMode.Write);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 加密文件流
|
|
/// </summary>
|
|
/// <param name="fs">需要加密的文件流</param>
|
|
/// <param name="decryptKey">加密密钥</param>
|
|
/// <param name="mode">加密模式</param>
|
|
/// <returns>加密流</returns>
|
|
public static Stream AESEncryptStrream(this FileStream fs, byte[] decryptKey, CipherMode mode = CipherMode.CBC)
|
|
{
|
|
using var aes = Aes.Create();
|
|
aes.Key = decryptKey;
|
|
aes.Mode = mode;
|
|
aes.Padding = PaddingMode.PKCS7;
|
|
var iv = new byte[AesIvSize];
|
|
using (var randomNumberGenerator = RandomNumberGenerator.Create())
|
|
{
|
|
randomNumberGenerator.GetBytes(iv);
|
|
}
|
|
|
|
aes.IV = iv;
|
|
var output = new PooledMemoryStream();
|
|
output.Write(AesEnvelopeMagic, 0, AesEnvelopeMagic.Length);
|
|
output.Write(iv, 0, iv.Length);
|
|
using (var encryptedStream = new CryptoStream(output, aes.CreateEncryptor(), CryptoStreamMode.Write))
|
|
{
|
|
fs.CopyTo(encryptedStream);
|
|
encryptedStream.FlushFinalBlock();
|
|
}
|
|
|
|
long authenticatedLength = output.Position;
|
|
output.Position = 0;
|
|
byte[] tag;
|
|
using (var hmac = new HMACSHA256(DeriveAesAuthenticationKey(decryptKey)))
|
|
{
|
|
tag = hmac.ComputeHash(output);
|
|
}
|
|
|
|
output.Position = authenticatedLength;
|
|
output.Write(tag, 0, tag.Length);
|
|
return output;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 解密文件流
|
|
/// </summary>
|
|
/// <param name="input">需要解密的文件流</param>
|
|
/// <param name="decryptKey">解密密钥</param>
|
|
/// <param name="mode">加密模式</param>
|
|
/// <returns>加密流</returns>
|
|
public static Stream AESDecryptStream(this FileStream input, string decryptKey, CipherMode mode = CipherMode.CBC)
|
|
{
|
|
byte[] keyBytes = Encoding.UTF8.GetBytes(decryptKey);
|
|
return input.AESDecryptStream(keyBytes, mode);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 解密文件流
|
|
/// </summary>
|
|
/// <param name="input">需要解密的文件流</param>
|
|
/// <param name="decryptKey">解密密钥</param>
|
|
/// <param name="mode">加密模式</param>
|
|
/// <returns>加密流</returns>
|
|
public static Stream AESDecryptStream(this FileStream input, byte[] decryptKey, CipherMode mode = CipherMode.CBC)
|
|
{
|
|
using var aes = Aes.Create();
|
|
aes.Key = decryptKey;
|
|
aes.Mode = mode;
|
|
aes.Padding = PaddingMode.PKCS7;
|
|
long startPosition = input.Position;
|
|
long envelopeLength = input.Length - startPosition;
|
|
if (envelopeLength < AesEnvelopeHeaderSize + AesIvSize + AesTagSize)
|
|
{
|
|
throw new CryptographicException("AES密文格式无效。");
|
|
}
|
|
|
|
var header = new byte[AesEnvelopeHeaderSize];
|
|
ReadExactly(input, header, 0, header.Length);
|
|
if (!HasAesEnvelopeMagic(header))
|
|
{
|
|
throw new CryptographicException("AES密文格式无效。");
|
|
}
|
|
|
|
long cipherLength = envelopeLength - AesEnvelopeHeaderSize - AesTagSize;
|
|
if (cipherLength < AesIvSize || cipherLength % AesIvSize != 0)
|
|
{
|
|
throw new CryptographicException("AES密文格式无效。");
|
|
}
|
|
|
|
long authenticatedLength = envelopeLength - AesTagSize;
|
|
byte[] expectedTag = ComputeAesAuthenticationTag(input, startPosition, authenticatedLength, decryptKey);
|
|
input.Position = startPosition + authenticatedLength;
|
|
var actualTag = new byte[AesTagSize];
|
|
ReadExactly(input, actualTag, 0, actualTag.Length);
|
|
if (!FixedTimeEquals(expectedTag, actualTag))
|
|
{
|
|
throw new CryptographicException("AES密文认证失败。");
|
|
}
|
|
|
|
var iv = new byte[AesIvSize];
|
|
Buffer.BlockCopy(header, AesEnvelopeMagic.Length, iv, 0, iv.Length);
|
|
input.Position = startPosition + AesEnvelopeHeaderSize;
|
|
using var boundedInput = new LimitedReadStream(input, cipherLength);
|
|
var output = new PooledMemoryStream();
|
|
aes.IV = iv;
|
|
using var decryptedStream = new CryptoStream(boundedInput, aes.CreateDecryptor(), CryptoStreamMode.Read);
|
|
decryptedStream.CopyTo(output);
|
|
return output;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 对指定文件AES加密
|
|
/// </summary>
|
|
/// <param name="input">源文件流</param>
|
|
/// <param name="key">加密密钥</param>
|
|
/// <param name="mode">加密模式</param>
|
|
/// <param name="outputPath">输出文件路径</param>
|
|
/// <remarks>输出采用带随机IV和认证标签的版本化格式。</remarks>
|
|
public static void AESEncryptFile(this FileStream input, string outputPath, string key, CipherMode mode = CipherMode.CBC)
|
|
{
|
|
EnsureDifferentFile(input, outputPath);
|
|
byte[] keyBytes = Encoding.UTF8.GetBytes(key);
|
|
using var aes = Aes.Create();
|
|
aes.Key = keyBytes;
|
|
aes.Mode = mode;
|
|
aes.Padding = PaddingMode.PKCS7;
|
|
var iv = new byte[AesIvSize];
|
|
using (var randomNumberGenerator = RandomNumberGenerator.Create())
|
|
{
|
|
randomNumberGenerator.GetBytes(iv);
|
|
}
|
|
|
|
aes.IV = iv;
|
|
using var output = new FileStream(outputPath, FileMode.Create, FileAccess.ReadWrite, FileShare.None);
|
|
output.Write(AesEnvelopeMagic, 0, AesEnvelopeMagic.Length);
|
|
output.Write(iv, 0, iv.Length);
|
|
using (var encryptedStream = new CryptoStream(new NonClosingStream(output), aes.CreateEncryptor(), CryptoStreamMode.Write))
|
|
{
|
|
input.CopyTo(encryptedStream);
|
|
encryptedStream.FlushFinalBlock();
|
|
}
|
|
|
|
long authenticatedLength = output.Position;
|
|
output.Position = 0;
|
|
byte[] tag;
|
|
using (var hmac = new HMACSHA256(DeriveAesAuthenticationKey(keyBytes)))
|
|
{
|
|
tag = hmac.ComputeHash(output);
|
|
}
|
|
|
|
output.Position = authenticatedLength;
|
|
output.Write(tag, 0, tag.Length);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 对指定的文件AES解密
|
|
/// </summary>
|
|
/// <param name="input">源文件流</param>
|
|
/// <param name="key">解密密钥</param>
|
|
/// <param name="mode">加密模式</param>
|
|
/// <param name="outputPath">输出文件路径</param>
|
|
/// <remarks>仅接受带认证标签的版本化密文,输入流必须可读取且可定位。</remarks>
|
|
public static void AESDecryptFile(this FileStream input, string outputPath, string key, CipherMode mode = CipherMode.CBC)
|
|
{
|
|
EnsureDifferentFile(input, outputPath);
|
|
if (!input.CanRead || !input.CanSeek)
|
|
{
|
|
throw new ArgumentException("The encrypted file stream must be readable and seekable.", nameof(input));
|
|
}
|
|
|
|
byte[] keyBytes = Encoding.UTF8.GetBytes(key);
|
|
using var aes = Aes.Create();
|
|
aes.Key = keyBytes;
|
|
aes.Mode = mode;
|
|
aes.Padding = PaddingMode.PKCS7;
|
|
long startPosition = input.Position;
|
|
long envelopeLength = input.Length - startPosition;
|
|
if (envelopeLength < AesEnvelopeHeaderSize + AesIvSize + AesTagSize)
|
|
{
|
|
throw new CryptographicException("AES密文格式无效。");
|
|
}
|
|
|
|
var header = new byte[AesEnvelopeHeaderSize];
|
|
ReadExactly(input, header, 0, header.Length);
|
|
if (!HasAesEnvelopeMagic(header))
|
|
{
|
|
throw new CryptographicException("AES密文格式无效。");
|
|
}
|
|
|
|
long cipherLength = envelopeLength - AesEnvelopeHeaderSize - AesTagSize;
|
|
if (cipherLength < AesIvSize || cipherLength % AesIvSize != 0)
|
|
{
|
|
throw new CryptographicException("AES密文格式无效。");
|
|
}
|
|
|
|
long authenticatedLength = envelopeLength - AesTagSize;
|
|
byte[] expectedTag = ComputeAesAuthenticationTag(input, startPosition, authenticatedLength, keyBytes);
|
|
input.Position = startPosition + authenticatedLength;
|
|
var actualTag = new byte[AesTagSize];
|
|
ReadExactly(input, actualTag, 0, actualTag.Length);
|
|
if (!FixedTimeEquals(expectedTag, actualTag))
|
|
{
|
|
throw new CryptographicException("AES密文认证失败。");
|
|
}
|
|
|
|
var iv = new byte[AesIvSize];
|
|
Buffer.BlockCopy(header, AesEnvelopeMagic.Length, iv, 0, iv.Length);
|
|
input.Position = startPosition + AesEnvelopeHeaderSize;
|
|
using var boundedInput = new LimitedReadStream(input, cipherLength);
|
|
using var output = new FileStream(outputPath, FileMode.Create, FileAccess.Write, FileShare.None);
|
|
aes.IV = iv;
|
|
using var decryptedStream = new CryptoStream(boundedInput, aes.CreateDecryptor(), CryptoStreamMode.Read);
|
|
decryptedStream.CopyTo(output);
|
|
}
|
|
|
|
private static byte[] ComputeAesAuthenticationTag(Stream input, long startPosition, long length, byte[] key)
|
|
{
|
|
input.Position = startPosition;
|
|
using var hmac = new HMACSHA256(DeriveAesAuthenticationKey(key));
|
|
var buffer = new byte[81920];
|
|
long remaining = length;
|
|
while (remaining > 0)
|
|
{
|
|
int bytesToRead = (int)Math.Min(buffer.Length, remaining);
|
|
int bytesRead = input.Read(buffer, 0, bytesToRead);
|
|
if (bytesRead == 0)
|
|
{
|
|
throw new CryptographicException("AES密文格式无效。");
|
|
}
|
|
|
|
hmac.TransformBlock(buffer, 0, bytesRead, buffer, 0);
|
|
remaining -= bytesRead;
|
|
}
|
|
|
|
hmac.TransformFinalBlock(Array.Empty<byte>(), 0, 0);
|
|
return hmac.Hash;
|
|
}
|
|
|
|
private static void ReadExactly(Stream input, byte[] buffer, int offset, int count)
|
|
{
|
|
while (count > 0)
|
|
{
|
|
int bytesRead = input.Read(buffer, offset, count);
|
|
if (bytesRead == 0)
|
|
{
|
|
throw new CryptographicException("AES密文格式无效。");
|
|
}
|
|
|
|
offset += bytesRead;
|
|
count -= bytesRead;
|
|
}
|
|
}
|
|
|
|
private static void EnsureDifferentFile(FileStream input, string outputPath)
|
|
{
|
|
if (input == null)
|
|
{
|
|
throw new ArgumentNullException(nameof(input));
|
|
}
|
|
|
|
var comparison = Path.DirectorySeparatorChar == '\\' ? StringComparison.OrdinalIgnoreCase : StringComparison.Ordinal;
|
|
if (string.Equals(Path.GetFullPath(input.Name), Path.GetFullPath(outputPath), comparison))
|
|
{
|
|
throw new ArgumentException("The input and output files must be different.", nameof(outputPath));
|
|
}
|
|
}
|
|
|
|
private sealed class LimitedReadStream : Stream
|
|
{
|
|
private readonly Stream input;
|
|
private long remaining;
|
|
|
|
public LimitedReadStream(Stream input, long length)
|
|
{
|
|
this.input = input;
|
|
remaining = length;
|
|
}
|
|
|
|
public override bool CanRead => input.CanRead;
|
|
|
|
public override bool CanSeek => false;
|
|
|
|
public override bool CanWrite => false;
|
|
|
|
public override long Length => throw new NotSupportedException();
|
|
|
|
public override long Position
|
|
{
|
|
get => throw new NotSupportedException();
|
|
set => throw new NotSupportedException();
|
|
}
|
|
|
|
public override void Flush()
|
|
{
|
|
}
|
|
|
|
public override int Read(byte[] buffer, int offset, int count)
|
|
{
|
|
if (remaining == 0)
|
|
{
|
|
return 0;
|
|
}
|
|
|
|
int bytesToRead = (int)Math.Min(count, remaining);
|
|
int bytesRead = input.Read(buffer, offset, bytesToRead);
|
|
remaining -= bytesRead;
|
|
return bytesRead;
|
|
}
|
|
|
|
public override long Seek(long offset, SeekOrigin origin) => throw new NotSupportedException();
|
|
|
|
public override void SetLength(long value) => throw new NotSupportedException();
|
|
|
|
public override void Write(byte[] buffer, int offset, int count) => throw new NotSupportedException();
|
|
|
|
protected override void Dispose(bool disposing)
|
|
{
|
|
base.Dispose(disposing);
|
|
}
|
|
}
|
|
|
|
private sealed class NonClosingStream : Stream
|
|
{
|
|
private readonly Stream inner;
|
|
|
|
public NonClosingStream(Stream inner)
|
|
{
|
|
this.inner = inner;
|
|
}
|
|
|
|
public override bool CanRead => inner.CanRead;
|
|
|
|
public override bool CanSeek => inner.CanSeek;
|
|
|
|
public override bool CanWrite => inner.CanWrite;
|
|
|
|
public override long Length => inner.Length;
|
|
|
|
public override long Position
|
|
{
|
|
get => inner.Position;
|
|
set => inner.Position = value;
|
|
}
|
|
|
|
public override void Flush() => inner.Flush();
|
|
|
|
public override int Read(byte[] buffer, int offset, int count) => inner.Read(buffer, offset, count);
|
|
|
|
public override long Seek(long offset, SeekOrigin origin) => inner.Seek(offset, origin);
|
|
|
|
public override void SetLength(long value) => inner.SetLength(value);
|
|
|
|
public override void Write(byte[] buffer, int offset, int count) => inner.Write(buffer, offset, count);
|
|
|
|
protected override void Dispose(bool disposing)
|
|
{
|
|
base.Dispose(disposing);
|
|
}
|
|
}
|
|
|
|
#endregion 对称加密算法AES加密解密
|
|
|
|
#region Base64加密解密
|
|
|
|
/// <summary>
|
|
/// Base64加密
|
|
/// </summary>
|
|
/// <param name="str">需要加密的字符串</param>
|
|
/// <returns>加密后的数据</returns>
|
|
public static string Base64Encrypt(this string str)
|
|
{
|
|
byte[] encbuff = Encoding.UTF8.GetBytes(str);
|
|
return Convert.ToBase64String(encbuff);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Base64解密
|
|
/// </summary>
|
|
/// <param name="str">需要解密的字符串</param>
|
|
/// <returns>解密后的数据</returns>
|
|
public static string Base64Decrypt(this string str)
|
|
{
|
|
try
|
|
{
|
|
byte[] decbuff = Convert.FromBase64String(str);
|
|
return Encoding.UTF8.GetString(decbuff);
|
|
}
|
|
catch
|
|
{
|
|
return str;
|
|
}
|
|
}
|
|
|
|
#endregion Base64加密解密
|
|
|
|
/// <summary>
|
|
/// SHA256函数
|
|
/// </summary>
|
|
/// <param name="str">原始字符串</param>
|
|
/// <returns>SHA256结果(返回长度为44字节的字符串)</returns>
|
|
public static string SHA256(this string str)
|
|
{
|
|
byte[] sha256Data = Encoding.UTF8.GetBytes(str);
|
|
using var sha256 = System.Security.Cryptography.SHA256.Create();
|
|
byte[] result = sha256.ComputeHash(sha256Data);
|
|
return Convert.ToBase64String(result); //返回长度为44字节的字符串
|
|
}
|
|
|
|
/// <summary>
|
|
/// SHA512函数
|
|
/// </summary>
|
|
/// <param name="str">原始字符串</param>
|
|
/// <returns>SHA512结果(返回长度为44字节的字符串)</returns>
|
|
public static string SHA512(this string str)
|
|
{
|
|
byte[] shaData = Encoding.UTF8.GetBytes(str);
|
|
using var sha512 = System.Security.Cryptography.SHA512.Create();
|
|
byte[] result = sha512.ComputeHash(shaData);
|
|
return Convert.ToBase64String(result); //返回长度为44字节的字符串
|
|
}
|
|
|
|
#region MD5摘要算法
|
|
|
|
#region 对字符串进行MD5摘要
|
|
|
|
/// <summary>
|
|
/// 对字符串进行MD5摘要
|
|
/// </summary>
|
|
/// <param name="message">需要摘要的字符串</param>
|
|
/// <returns>MD5摘要字符串</returns>
|
|
public static string MDString(this string message)
|
|
{
|
|
MD5 md5 = MD5.Create();
|
|
byte[] buffer = Encoding.Default.GetBytes(message);
|
|
byte[] bytes = md5.ComputeHash(buffer);
|
|
return GetHexString(bytes);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 对字符串进行MD5二次摘要
|
|
/// </summary>
|
|
/// <param name="message">需要摘要的字符串</param>
|
|
/// <returns>MD5摘要字符串</returns>
|
|
public static string MDString2(this string message) => MDString(MDString(message));
|
|
|
|
/// <summary>
|
|
/// MD5 三次摘要算法
|
|
/// </summary>
|
|
/// <param name="s">需要摘要的字符串</param>
|
|
/// <returns>MD5摘要字符串</returns>
|
|
public static string MDString3(this string s)
|
|
{
|
|
using MD5 md5 = MD5.Create();
|
|
byte[] bytes = Encoding.ASCII.GetBytes(s);
|
|
byte[] bytes1 = md5.ComputeHash(bytes);
|
|
byte[] bytes2 = md5.ComputeHash(bytes1);
|
|
byte[] bytes3 = md5.ComputeHash(bytes2);
|
|
return GetHexString(bytes3);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 对字符串进行MD5加盐摘要
|
|
/// </summary>
|
|
/// <param name="message">需要摘要的字符串</param>
|
|
/// <param name="salt">盐</param>
|
|
/// <returns>MD5摘要字符串</returns>
|
|
public static string MDString(this string message, string salt) => MDString(message + salt);
|
|
|
|
/// <summary>
|
|
/// 对字符串进行MD5二次加盐摘要
|
|
/// </summary>
|
|
/// <param name="message">需要摘要的字符串</param>
|
|
/// <param name="salt">盐</param>
|
|
/// <returns>MD5摘要字符串</returns>
|
|
public static string MDString2(this string message, string salt) => MDString(MDString(message + salt), salt);
|
|
|
|
/// <summary>
|
|
/// MD5 三次摘要算法
|
|
/// </summary>
|
|
/// <param name="s">需要摘要的字符串</param>
|
|
/// <param name="salt">盐</param>
|
|
/// <returns>MD5摘要字符串</returns>
|
|
public static string MDString3(this string s, string salt)
|
|
{
|
|
using MD5 md5 = MD5.Create();
|
|
byte[] bytes = Encoding.ASCII.GetBytes(s + salt);
|
|
byte[] bytes1 = md5.ComputeHash(bytes);
|
|
byte[] bytes2 = md5.ComputeHash(bytes1);
|
|
byte[] bytes3 = md5.ComputeHash(bytes2);
|
|
return GetHexString(bytes3);
|
|
}
|
|
|
|
#endregion 对字符串进行MD5摘要
|
|
|
|
#region 获取文件的MD5值
|
|
|
|
/// <summary>
|
|
/// 获取文件的MD5值
|
|
/// </summary>
|
|
/// <param name="fileName">需要求MD5值的文件的文件名及路径</param>
|
|
/// <returns>MD5摘要字符串</returns>
|
|
public static string MDFile(this string fileName)
|
|
{
|
|
var fs = new BufferedStream(File.Open(fileName, FileMode.Open, FileAccess.Read), 1048576);
|
|
using MD5 md5 = MD5.Create();
|
|
byte[] bytes = md5.ComputeHash(fs);
|
|
return GetHexString(bytes);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 计算文件的sha256
|
|
/// </summary>
|
|
/// <param name="stream"></param>
|
|
/// <returns></returns>
|
|
public static string SHA256(this Stream stream)
|
|
{
|
|
var fs = new BufferedStream(stream, 1048576);
|
|
using var sha = System.Security.Cryptography.SHA256.Create();
|
|
byte[] checksum = sha.ComputeHash(fs);
|
|
return BitConverter.ToString(checksum).Replace("-", string.Empty);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 计算文件的sha512
|
|
/// </summary>
|
|
/// <param name="stream"></param>
|
|
/// <returns></returns>
|
|
public static string SHA512(this Stream stream)
|
|
{
|
|
var fs = new BufferedStream(stream, 1048576);
|
|
using var sha = System.Security.Cryptography.SHA512.Create();
|
|
byte[] checksum = sha.ComputeHash(fs);
|
|
return BitConverter.ToString(checksum).Replace("-", string.Empty);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 获取数据流的MD5摘要值
|
|
/// </summary>
|
|
/// <param name="stream"></param>
|
|
/// <returns>MD5摘要字符串</returns>
|
|
public static string MDString(this Stream stream)
|
|
{
|
|
var fs = new BufferedStream(stream, 1048576);
|
|
using MD5 md5 = MD5.Create();
|
|
byte[] bytes = md5.ComputeHash(fs);
|
|
var mdstr = GetHexString(bytes);
|
|
stream.Position = 0;
|
|
return mdstr;
|
|
}
|
|
|
|
public static string GetHexString(byte[] bytes)
|
|
{
|
|
var hexArray = new char[bytes.Length << 1];
|
|
for (var i = 0; i < hexArray.Length; i += 2)
|
|
{
|
|
var b = bytes[i >> 1];
|
|
hexArray[i] = GetHexValue(b >> 4); // b / 16
|
|
hexArray[i + 1] = GetHexValue(b & 0xF); // b % 16
|
|
}
|
|
return new string(hexArray, 0, hexArray.Length);
|
|
|
|
static char GetHexValue(int i)
|
|
{
|
|
if (i < 10)
|
|
{
|
|
return (char)(i + '0');
|
|
}
|
|
return (char)(i - 10 + 'a');
|
|
}
|
|
}
|
|
|
|
#endregion 获取文件的MD5值
|
|
|
|
#endregion MD5摘要算法
|
|
}
|
|
|
|
/// <summary>
|
|
/// RC2加密解密算法
|
|
/// </summary>
|
|
public static class RC2Crypt
|
|
{
|
|
private static ASCIIEncoding _asciiEncoding;
|
|
private static byte[] _iv;
|
|
private static byte[] _key;
|
|
private static RC2 _rc2Csp;
|
|
private static UnicodeEncoding _textConverter;
|
|
|
|
static RC2Crypt()
|
|
{
|
|
InitializeComponent();
|
|
}
|
|
|
|
private static void InitializeComponent()
|
|
{
|
|
_key = new byte[]
|
|
{
|
|
106,
|
|
51,
|
|
25,
|
|
141,
|
|
157,
|
|
142,
|
|
23,
|
|
111,
|
|
234,
|
|
159,
|
|
187,
|
|
154,
|
|
215,
|
|
34,
|
|
37,
|
|
204
|
|
};
|
|
_iv = new byte[]
|
|
{
|
|
135,
|
|
186,
|
|
133,
|
|
136,
|
|
184,
|
|
149,
|
|
153,
|
|
144
|
|
};
|
|
_asciiEncoding = new ASCIIEncoding();
|
|
_textConverter = new UnicodeEncoding();
|
|
_rc2Csp = RC2.Create();
|
|
}
|
|
|
|
#region 将文本数据加密后写入一个文件
|
|
|
|
/// <summary>
|
|
/// 将文本数据加密后写入一个文件,其中,这个文件是用InitBinFile建立的,这个文件将被分成十块,
|
|
/// 用来分别保存10组不同的数据,第一个byte位保留,第2位到第21位分别用来存放每块数据的长度,但
|
|
/// 一个byte的取值为0-127,所以,用两个byte来存放一个长度。
|
|
/// </summary>
|
|
/// <param name="toEncryptText">要加密的文本数据</param>
|
|
/// <param name="filePath">要写入的文件</param>
|
|
/// <param name="dataIndex">写入第几块,取值为1--10</param>
|
|
/// <returns>是否操作成功</returns>
|
|
public static bool EncryptToFile(this string toEncryptText, string filePath, int dataIndex)
|
|
{
|
|
var r = false;
|
|
if (dataIndex is > 10 or < 1)
|
|
{
|
|
return r;
|
|
}
|
|
|
|
//打开要写入的文件,主要是为了保持原文件的内容不丢失
|
|
var tmpFileStream = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.None, 1024, true);
|
|
var index = new byte[10261];
|
|
|
|
//将读取的内容写到byte数组
|
|
tmpFileStream.Read(index, 0, 10261);
|
|
tmpFileStream.Close();
|
|
|
|
//定义基本的加密转换运算
|
|
using var encryptor = _rc2Csp.CreateEncryptor(_key, _iv);
|
|
using var msEncrypt = new PooledMemoryStream();
|
|
|
|
//在此加密转换流中,加密将从csEncrypt,加密后,结果在msEncrypt流中。
|
|
using var csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write);
|
|
|
|
//将要加密的文本转换成UTF-16 编码,保存在tmp数组。
|
|
var tmp = _textConverter.GetBytes(toEncryptText);
|
|
|
|
//将tmp输入csEncrypt,将通过Encryptor来加密。
|
|
csEncrypt.Write(tmp, 0, tmp.Length);
|
|
|
|
//输出到msEnctypt
|
|
csEncrypt.FlushFinalBlock();
|
|
|
|
//将流转成byte[]
|
|
var encrypted = msEncrypt.ToArray();
|
|
if (encrypted.Length > 1024)
|
|
return false;
|
|
|
|
//得到加密后数据的大小,将结果存在指定的位置。
|
|
index[dataIndex * 2 - 1] = Convert.ToByte(Convert.ToString(encrypted.Length / 128));
|
|
index[dataIndex * 2] = Convert.ToByte(Convert.ToString(encrypted.Length % 128));
|
|
|
|
//将加密后的结果写入index(覆盖)
|
|
for (var i = 0; i < encrypted.Length; i++)
|
|
index[1024 * (dataIndex - 1) + 21 + i] = encrypted[i];
|
|
|
|
//建立文件流
|
|
tmpFileStream = new FileStream(filePath, FileMode.Truncate, FileAccess.Write, FileShare.None, 1024, true);
|
|
|
|
//写文件
|
|
tmpFileStream.Write(index, 0, 10261);
|
|
tmpFileStream.Flush();
|
|
r = true;
|
|
tmpFileStream.Close();
|
|
return r;
|
|
}
|
|
|
|
#endregion 将文本数据加密后写入一个文件
|
|
|
|
#region 从一个文件中解密出一段文本,其中,这个文件是由InitBinFile建立的,并且由 EncryptToFile加密的
|
|
|
|
/// <summary>
|
|
/// 从一个文件中解密出一段文本,其中,这个文件是由InitBinFile建立的,并且由 EncryptToFile加密的
|
|
/// </summary>
|
|
/// <param name="filePath">要解密的文件</param>
|
|
/// <param name="dataIndex">要从哪一个块中解密</param>
|
|
/// <returns>解密后的文本</returns>
|
|
public static string DecryptFromFile(this string filePath, int dataIndex)
|
|
{
|
|
if (dataIndex is > 10 or < 1)
|
|
{
|
|
return "";
|
|
}
|
|
|
|
using var tmpFileStream = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.None, 1024, true);
|
|
using var decryptor = _rc2Csp.CreateDecryptor(_key, _iv);
|
|
using var msDecrypt = new PooledMemoryStream();
|
|
using var csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Write);
|
|
var index = new byte[10261];
|
|
tmpFileStream.Read(index, 0, 10261);
|
|
var count = index[dataIndex * 2 - 1] * 128 + index[dataIndex * 2];
|
|
var tmp = new byte[count];
|
|
Array.Copy(index, 1024 * (dataIndex - 1) + 21, tmp, 0, count);
|
|
csDecrypt.Write(tmp, 0, count);
|
|
csDecrypt.FlushFinalBlock();
|
|
var decrypted = msDecrypt.ToArray();
|
|
return _textConverter.GetString(decrypted, 0, decrypted.Length);
|
|
}
|
|
|
|
#endregion 从一个文件中解密出一段文本,其中,这个文件是由InitBinFile建立的,并且由 EncryptToFile加密的
|
|
|
|
#region 将一段文本加密后保存到一个文件
|
|
|
|
/// <summary>
|
|
/// 将一段文本加密后保存到一个文件
|
|
/// </summary>
|
|
/// <param name="toEncryptText">要加密的文本数据</param>
|
|
/// <param name="filePath">要保存的文件</param>
|
|
/// <returns>是否加密成功</returns>
|
|
public static void EncryptToFile(this string toEncryptText, string filePath)
|
|
{
|
|
using var tmpFileStream = new FileStream(filePath, FileMode.OpenOrCreate, FileAccess.Write, FileShare.None, 1024, true);
|
|
using var encryptor = _rc2Csp.CreateEncryptor(_key, _iv);
|
|
using var msEncrypt = new PooledMemoryStream();
|
|
using var csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write);
|
|
var tmp = _textConverter.GetBytes(toEncryptText);
|
|
csEncrypt.Write(tmp, 0, tmp.Length);
|
|
csEncrypt.FlushFinalBlock();
|
|
var encrypted = msEncrypt.ToArray();
|
|
tmpFileStream.Write(encrypted, 0, encrypted.Length);
|
|
}
|
|
|
|
#endregion 将一段文本加密后保存到一个文件
|
|
|
|
#region 将一个被加密的文件解密
|
|
|
|
/// <summary>
|
|
/// 将一个被加密的文件解密
|
|
/// </summary>
|
|
/// <param name="filePath">要解密的文件</param>
|
|
/// <returns>解密后的文本</returns>
|
|
public static string DecryptFromFile(this string filePath)
|
|
{
|
|
using var tmpFileStream = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.None, 1024, true);
|
|
using var decryptor = _rc2Csp.CreateDecryptor(_key, _iv);
|
|
using var msDecrypt = new PooledMemoryStream();
|
|
using var csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Write);
|
|
var tmp = new byte[tmpFileStream.Length];
|
|
tmpFileStream.Read(tmp, 0, tmp.Length);
|
|
csDecrypt.Write(tmp, 0, tmp.Length);
|
|
csDecrypt.FlushFinalBlock();
|
|
var decrypted = msDecrypt.ToArray();
|
|
return _textConverter.GetString(decrypted, 0, decrypted.Length);
|
|
}
|
|
|
|
#endregion 将一个被加密的文件解密
|
|
|
|
#region 将文本数据加密后写入一个文件
|
|
|
|
/// <summary>
|
|
/// 将文本数据加密后写入一个文件,其中,这个文件是用InitBinFile建立的,这个文件将被分成十块,
|
|
/// 用来分别保存10组不同的数据,第一个byte位保留,第2位到第21位分别用来存放每块数据的长度,但
|
|
/// 一个byte的取值为0-127,所以,用两个byte来存放一个长度。
|
|
/// </summary>
|
|
/// <param name="toEncryptText">要加密的文本数据</param>
|
|
/// <param name="filePath">要写入的文件</param>
|
|
/// <param name="dataIndex">写入第几块,取值为1--10</param>
|
|
/// <param name="IV">初始化向量</param>
|
|
/// <param name="Key">加密密匙</param>
|
|
/// <returns>是否操作成功</returns>
|
|
public static void EncryptToFile(this string toEncryptText, string filePath, int dataIndex, byte[] IV, byte[] Key)
|
|
{
|
|
if (dataIndex is > 10 or < 1)
|
|
{
|
|
return;
|
|
}
|
|
|
|
//打开要写入的文件,主要是为了保持原文件的内容不丢失
|
|
using var fs = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.None, 1024, true);
|
|
var index = new byte[10261];
|
|
|
|
//将读取的内容写到byte数组
|
|
fs.Read(index, 0, 10261);
|
|
|
|
//定义基本的加密转换运算
|
|
using var encryptor = _rc2Csp.CreateEncryptor(Key, IV);
|
|
using var msEncrypt = new PooledMemoryStream();
|
|
|
|
//在此加密转换流中,加密将从csEncrypt,加密后,结果在msEncrypt流中。
|
|
using var csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write);
|
|
var tmp = _textConverter.GetBytes(toEncryptText);
|
|
|
|
//将tmp输入csEncrypt,将通过Encryptor来加密。
|
|
csEncrypt.Write(tmp, 0, tmp.Length);
|
|
|
|
//输出到msEnctypt
|
|
csEncrypt.FlushFinalBlock();
|
|
|
|
//将流转成byte[]
|
|
var encrypted = msEncrypt.ToArray();
|
|
if (encrypted.Length > 1024)
|
|
{
|
|
return;
|
|
}
|
|
|
|
//得到加密后数据的大小,将结果存在指定的位置。
|
|
index[dataIndex * 2 - 1] = Convert.ToByte(Convert.ToString(encrypted.Length / 128));
|
|
index[dataIndex * 2] = Convert.ToByte(Convert.ToString(encrypted.Length % 128));
|
|
|
|
//将加密后的结果写入index(覆盖)
|
|
for (int i = 0; i < encrypted.Length; i++)
|
|
{
|
|
index[1024 * (dataIndex - 1) + 21 + i] = encrypted[i];
|
|
}
|
|
|
|
//建立文件流
|
|
using var newStream = new FileStream(filePath, FileMode.Truncate, FileAccess.Write, FileShare.None, 1024, true);
|
|
newStream.Write(index, 0, 10261);
|
|
newStream.Flush();
|
|
}
|
|
|
|
#endregion 将文本数据加密后写入一个文件
|
|
|
|
#region 从一个文件中解密出一段文本
|
|
|
|
/// <summary>
|
|
/// 从一个文件中解密出一段文本,其中,这个文件是由InitBinFile建立的,并且由 EncryptToFile加密的
|
|
/// </summary>
|
|
/// <param name="filePath">要解密的文件</param>
|
|
/// <param name="dataIndex">要从哪一个块中解密</param>
|
|
/// <param name="iv">初始化向量</param>
|
|
/// <param name="key">解密密匙</param>
|
|
/// <returns>解密后的文本</returns>
|
|
public static string DecryptFromFile(this string filePath, int dataIndex, byte[] iv, byte[] key)
|
|
{
|
|
if (dataIndex is > 10 or < 1)
|
|
{
|
|
return "";
|
|
}
|
|
|
|
using var tmpFileStream = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.None, 1024, true);
|
|
using var decryptor = _rc2Csp.CreateDecryptor(key, iv);
|
|
using var msDecrypt = new PooledMemoryStream();
|
|
using var csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Write);
|
|
var index = new byte[10261];
|
|
tmpFileStream.Read(index, 0, 10261);
|
|
var count = index[dataIndex * 2 - 1] * 128 + index[dataIndex * 2];
|
|
var tmp = new byte[count];
|
|
Array.Copy(index, 1024 * (dataIndex - 1) + 21, tmp, 0, count);
|
|
csDecrypt.Write(tmp, 0, count);
|
|
csDecrypt.FlushFinalBlock();
|
|
var decrypted = msDecrypt.ToArray();
|
|
return _textConverter.GetString(decrypted, 0, decrypted.Length);
|
|
}
|
|
|
|
#endregion 从一个文件中解密出一段文本
|
|
|
|
#region 将一段文本加密后保存到一个文件
|
|
|
|
/// <summary>
|
|
/// 将一段文本加密后保存到一个文件
|
|
/// </summary>
|
|
/// <param name="toEncryptText">要加密的文本数据</param>
|
|
/// <param name="filePath">要保存的文件</param>
|
|
/// <param name="iv">初始化向量</param>
|
|
/// <param name="key">加密密匙</param>
|
|
/// <returns>是否加密成功</returns>
|
|
public static void EncryptToFile(this string toEncryptText, string filePath, byte[] iv, byte[] key)
|
|
{
|
|
using var tmpFileStream = new FileStream(filePath, FileMode.OpenOrCreate, FileAccess.Write, FileShare.None, 1024, true);
|
|
using var encryptor = _rc2Csp.CreateEncryptor(key, iv);
|
|
using var msEncrypt = new PooledMemoryStream();
|
|
using var csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write);
|
|
var tmp = _textConverter.GetBytes(toEncryptText);
|
|
csEncrypt.Write(tmp, 0, tmp.Length);
|
|
csEncrypt.FlushFinalBlock();
|
|
var encrypted = msEncrypt.ToArray();
|
|
tmpFileStream.Write(encrypted, 0, encrypted.Length);
|
|
tmpFileStream.Flush();
|
|
}
|
|
|
|
#endregion 将一段文本加密后保存到一个文件
|
|
|
|
#region 将一个被加密的文件解密
|
|
|
|
/// <summary>
|
|
/// 将一个被加密的文件解密
|
|
/// </summary>
|
|
/// <param name="filePath">要解密的文件</param>
|
|
/// <param name="iv">初始化向量</param>
|
|
/// <param name="key">解密密匙</param>
|
|
/// <returns>解密后的文本</returns>
|
|
public static string DecryptFromFile(this string filePath, byte[] iv, byte[] key)
|
|
{
|
|
using var tmpFileStream = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.None, 1024, true);
|
|
using var decryptor = _rc2Csp.CreateDecryptor(key, iv);
|
|
using var msDecrypt = new PooledMemoryStream();
|
|
using var csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Write);
|
|
var tmp = new byte[tmpFileStream.Length];
|
|
tmpFileStream.Read(tmp, 0, tmp.Length);
|
|
csDecrypt.Write(tmp, 0, tmp.Length);
|
|
csDecrypt.FlushFinalBlock();
|
|
var decrypted = msDecrypt.ToArray();
|
|
return _textConverter.GetString(decrypted, 0, decrypted.Length);
|
|
}
|
|
|
|
#endregion 将一个被加密的文件解密
|
|
|
|
#region 设置加密或解密的初始化向量
|
|
|
|
/// <summary>
|
|
/// 设置加密或解密的初始化向量
|
|
/// </summary>
|
|
/// <param name="s">长度等于8的ASCII字符集的字符串</param>
|
|
public static void SetIV(this string s)
|
|
{
|
|
if (s.Length != 8)
|
|
{
|
|
_iv = null;
|
|
return;
|
|
}
|
|
|
|
try
|
|
{
|
|
_iv = _asciiEncoding.GetBytes(s);
|
|
}
|
|
catch (Exception)
|
|
{
|
|
_iv = null;
|
|
}
|
|
}
|
|
|
|
#endregion 设置加密或解密的初始化向量
|
|
|
|
#region 设置加密或解密的密匙
|
|
|
|
/// <summary>
|
|
/// 设置加密或解密的密匙
|
|
/// </summary>
|
|
/// <param name="s">长度等于16的ASCII字符集的字符串</param>
|
|
public static void SetKey(this string s)
|
|
{
|
|
if (s.Length != 16)
|
|
{
|
|
_key = null;
|
|
return;
|
|
}
|
|
|
|
try
|
|
{
|
|
_key = _asciiEncoding.GetBytes(s);
|
|
}
|
|
catch (Exception)
|
|
{
|
|
_key = null;
|
|
}
|
|
}
|
|
|
|
#endregion 设置加密或解密的密匙
|
|
} |