phongnguyend/CryptographyHelper

GitHub: phongnguyend/CryptographyHelper

该库通过扩展方法为 .NET 提供了一套简洁易用的密码学 API,涵盖哈希、对称加密、非对称加密及签名等常用加密操作。

Stars: 38 | Forks: 7

# Nuget https://www.nuget.org/packages/CryptographyHelper # .Net 中的密码学对象继承 **哈希算法:** ![替代文本](/docs/imgs/HashAlgorithm.png) **对称算法:** ![替代文本](/docs/imgs/SymmetricAlgorithm.png) **非对称算法:** ![替代文本](/docs/imgs/AsymmetricAlgorithm.png) ## 哈希算法 支持使用 MD5、SHA-1、SHA-256、SHA-384 或 SHA-512 对来自 `string`、`byte[]`、`Stream` 或 `FileInfo` 的数据进行哈希处理。可以通过 `.WithKey()` 提供可选的 HMAC 密钥。结果可以检索为 `byte[]`、十六进制字符串或 Base64 字符串。 ### 支持的算法 | 方法 | 算法 | |---|---| | `UseMd5()` | MD5 | | `UseSha1()` | SHA-1 | | `UseSha256()` | SHA-256 | | `UseSha384()` | SHA-384 | | `UseSha512()` | SHA-512 | ### 对字符串进行哈希处理 ``` using CryptographyHelper.HashAlgorithms; // Hex string output var hashed = "Original Message to hash".UseSha256().ComputeHash().ToHexString(); // Base64 string output var hashed = "Original Message to hash".UseSha256().ComputeHashedString(Hash.StringFormat.Base64); ``` ### 对字节数组进行哈希处理 ``` using CryptographyHelper.HashAlgorithms; byte[] data = "Original Message to hash".GetBytes(); var hashed = data.UseSha256().ComputeHash().ToHexString(); ``` ### 对 Stream 进行哈希处理 ``` using CryptographyHelper.HashAlgorithms; using var stream = File.OpenRead("file.txt"); var hashed = stream.UseSha256().ComputeHash().ToHexString(); ``` ### 计算文件校验和 ``` using CryptographyHelper.HashAlgorithms; var checksum = "path/to/file.xlsx".ToFileInfo().UseMd5().ComputeHashedString(); ``` ### HMAC(带密钥的哈希) ``` using CryptographyHelper.HashAlgorithms; var key = "mysecretkey".GetBytes(); var hashed = "Original Message to hash".UseSha512().WithKey(key).ComputeHash().ToHexString(); ``` ### PBKDF2(基于密码的密钥派生) 使用 `PBKDF2` 从密码派生加密密钥。您可以提供 `byte[]` 形式的盐值(salt),设置迭代次数,并可选择哈希算法。 ``` using CryptographyHelper.HashAlgorithms; using System.Security.Cryptography; var salt = "65QuFYgSxqIW0d9Y/QKRX9veWK0DOyX0g7+nbr9yux8=".FromBase64String(); var key = "MyVeryComplexPassword" .UsePBKDF2(salt, iterations: 500000) .GetBytes(32) .ToBase64String(); ``` ## 对称算法 支持使用 DES、Triple DES 或 AES 对来自 `string`、`byte[]` 或 `Stream` 的数据进行加密和解密。可以通过流式 API 配置密码模式、填充模式和 IV。 ### 支持的算法 | 方法 | 算法 | |---|---| | `UseDES(key)` | DES(64 位密钥) | | `UseTripleDES(key)` | Triple DES(128 位或 192 位密钥) | | `UseAES(key)` | AES(128、192 或 256 位密钥) | ### 生成随机密钥 ``` using CryptographyHelper.SymmetricAlgorithms; byte[] key = SymmetricCrypto.GenerateKey(32); // 32 bytes = 256-bit key ``` ### 加密和解密字符串 ``` using CryptographyHelper.SymmetricAlgorithms; using System.Security.Cryptography; var key = "MeNFuKVG63Ks7dChmDvA67lSN6eKDE1QVuZT1dGCYlI=".FromBase64String(); var iv = "bXhlXQwu0R9qMjbCfEo7GA==".FromBase64String(); // Encrypt var encrypted = "Text to encrypt" .UseAES(key) .WithCipher(CipherMode.CBC) .WithPadding(PaddingMode.PKCS7) .WithIV(iv) .Encrypt(); // Decrypt var decrypted = encrypted .UseAES(key) .WithCipher(CipherMode.CBC) .WithPadding(PaddingMode.PKCS7) .WithIV(iv) .Decrypt() .GetString(); ``` ### 加密和解密字节数组 ``` using CryptographyHelper.SymmetricAlgorithms; using System.Security.Cryptography; byte[] data = "Text to encrypt".GetBytes(); var key = "MeNFuKVG63Ks7dChmDvA67lSN6eKDE1QVuZT1dGCYlI=".FromBase64String(); var iv = "bXhlXQwu0R9qMjbCfEo7GA==".FromBase64String(); var encrypted = data.UseAES(key).WithCipher(CipherMode.CBC).WithIV(iv).Encrypt(); var decrypted = encrypted.UseAES(key).WithCipher(CipherMode.CBC).WithIV(iv).Decrypt().GetString(); ``` ### 加密和解密文件 (Stream) ``` using CryptographyHelper.SymmetricAlgorithms; using System.Security.Cryptography; var key = "MeNFuKVG63Ks7dChmDvA67lSN6eKDE1QVuZT1dGCYlI=".FromBase64String(); var iv = "bXhlXQwu0R9qMjbCfEo7GA==".FromBase64String(); // Encrypt using var inputStream = File.OpenRead("original.xlsx"); using var outputStream = File.OpenWrite("encrypted.xlsx"); inputStream.UseAES(key).WithCipher(CipherMode.CBC).WithIV(iv).Encrypt(outputStream); // Decrypt using var inputStream2 = File.OpenRead("encrypted.xlsx"); using var outputStream2 = File.OpenWrite("decrypted.xlsx"); inputStream2.UseAES(key).WithCipher(CipherMode.CBC).WithIV(iv).Decrypt(outputStream2); ``` ### DES 和 Triple DES 同样的流式 API 也适用于 DES 和 Triple DES: ``` // DES var encrypted = "Text to encrypt".UseDES(key).WithCipher(CipherMode.CBC).WithIV(iv).Encrypt(); // Triple DES var encrypted = "Text to encrypt".UseTripleDES(key).WithCipher(CipherMode.CBC).WithIV(iv).Encrypt(); ``` ## 非对称算法 支持使用 XML 密钥、PEM 密钥或 X.509 证书进行 RSA 加密/解密以及 RSA 签名/验证。 ### 生成并导出 RSA 密钥 ``` using CryptographyHelper.AsymmetricAlgorithms; // Generate an XML-formatted private key (includes public key) string privateKey = RSACrypto.GenerateKey(size: 2048); // Export just the public key portion string publicKey = RSACrypto.ExportPublicKey(privateKey); ``` ### 加密和解密 ``` using CryptographyHelper.AsymmetricAlgorithms; string privateKey = RSACrypto.GenerateKey(); string publicKey = RSACrypto.ExportPublicKey(privateKey); // Encrypt with public key byte[] encrypted = "super secret text".UseRSA(publicKey).Encrypt(); // Decrypt with private key string decrypted = encrypted.UseRSA(privateKey).Decrypt().GetString(); ``` ### 使用 PEM 密钥进行加密和解密 ``` using CryptographyHelper.AsymmetricAlgorithms; string pemPublicKey = "-----BEGIN PUBLIC KEY-----\n...\n-----END PUBLIC KEY-----"; string pemPrivateKey = "-----BEGIN PRIVATE KEY-----\n...\n-----END PRIVATE KEY-----"; byte[] encrypted = "super secret text".UseRSA(pemPublicKey, KeyFormat.Pem).Encrypt(); string decrypted = encrypted.UseRSA(pemPrivateKey, KeyFormat.Pem).Decrypt().GetString(); ``` ### 使用证书进行加密和解密 ``` using CryptographyHelper.AsymmetricAlgorithms; using CryptographyHelper.Certificates; var cert = CertificateFile.Find("path/to/cert.pfx", "password"); byte[] encrypted = "super secret text".UseRSA(cert).Encrypt(); string decrypted = encrypted.UseRSA(cert).Decrypt().GetString(); ``` ### 对哈希进行签名和验证 ``` using CryptographyHelper.AsymmetricAlgorithms; using CryptographyHelper.HashAlgorithms; using System.Security.Cryptography; string privateKey = RSACrypto.GenerateKey(); string publicKey = RSACrypto.ExportPublicKey(privateKey); byte[] hash = "super secret text".UseSha256().ComputeHash(); byte[] signature = hash.UseRSA(privateKey).SignHash(HashAlgorithmName.SHA256); bool verified = hash.UseRSA(publicKey).VerifyHash(signature, HashAlgorithmName.SHA256); ``` ### 对数据进行签名和验证 ``` using CryptographyHelper.AsymmetricAlgorithms; using System.Security.Cryptography; string privateKey = RSACrypto.GenerateKey(); string publicKey = RSACrypto.ExportPublicKey(privateKey); byte[] signature = "super secret text".UseRSA(privateKey).SignData(HashAlgorithmName.SHA512); bool verified = "super secret text".UseRSA(publicKey).VerifyData(signature, HashAlgorithmName.SHA512); ``` ### 使用证书进行签名和验证 ``` using CryptographyHelper.AsymmetricAlgorithms; using System.Security.Cryptography; using System.Security.Cryptography.X509Certificates; var cert = new X509Certificate2("path/to/cert.pfx", "password"); byte[] signature = "super secret text".UseRSA(cert).SignData(HashAlgorithmName.SHA512); bool verified = "super secret text".UseRSA(cert).VerifyData(signature, HashAlgorithmName.SHA512); ```
标签:SOC Prime, 加密库, 哈希算法, 多人体追踪, 密码学, 开发工具, 手动系统调用, 扩展方法