添加AES加密算法及测试
This commit is contained in:
parent
2733612bce
commit
746685a901
|
@ -11,26 +11,6 @@ namespace Falcon.SugarApi.Test.Encryption
|
|||
[TestClass]
|
||||
public class EncryptionTest
|
||||
{
|
||||
/// <summary>
|
||||
/// 测试公钥、秘钥生成,加密解密
|
||||
/// </summary>
|
||||
[TestMethod]
|
||||
public void RSATest() {
|
||||
var config = new RSAConfig() {
|
||||
BlockSize = 1024
|
||||
};
|
||||
IRSAEncryption r = new RSAProvider(config);
|
||||
var k = r.GenerateKey();
|
||||
|
||||
var p = new RSAProvider(config);
|
||||
var mingw = " /// <summary>\r\n /// 测试公钥、秘钥生成,加密解密\r\n /// </summary>\r\n [TestMethod]\r\n public void EncryptionRSA() {\r\n var config = new RSAConfig();\r\n var r = new RSAProvider(config);";
|
||||
var miwen = p.Encrypt(mingw, k.PublicKey);
|
||||
|
||||
var p1 = new RSAProvider(config);
|
||||
var mingw1 = p1.Decrypt(miwen, k.PrivateKey);
|
||||
|
||||
Assert.AreEqual(mingw, mingw1, "解密后明文不同");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 测试公钥、秘钥生成,加密解密
|
||||
|
@ -59,6 +39,29 @@ namespace Falcon.SugarApi.Test.Encryption
|
|||
|
||||
}
|
||||
}
|
||||
/// <summary>
|
||||
/// 测试公钥、秘钥生成,加密解密
|
||||
/// </summary>
|
||||
[TestMethod("AES测试")]
|
||||
public void AESTest() {
|
||||
var config = new AESConfig() { };
|
||||
IAESEncryption r = new AESProvider(config);
|
||||
var k = r.GenerateKey();
|
||||
|
||||
for (int i = 0; i < 1000; i++) {
|
||||
var mingw = GenerateStr();
|
||||
Console.WriteLine(mingw);
|
||||
var p = new AESProvider(config);
|
||||
var miwen = p.Encrypt(k, mingw);
|
||||
|
||||
var p1 = new AESProvider(config);
|
||||
var mingw1 = p1.Decrypt(k, miwen);
|
||||
|
||||
Assert.AreEqual(mingw, mingw1, "解密后明文不同");
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 生成随机长度,由字符表内字符组成的字符串
|
||||
/// </summary>
|
||||
|
|
15
Falcon.SugarApi/Encryption/AESConfig.cs
Normal file
15
Falcon.SugarApi/Encryption/AESConfig.cs
Normal file
|
@ -0,0 +1,15 @@
|
|||
namespace Falcon.SugarApi.Encryption
|
||||
{
|
||||
/// <summary>
|
||||
/// AES加密算法配置
|
||||
/// </summary>
|
||||
public class AESConfig
|
||||
{
|
||||
private int keyLength = 32;
|
||||
|
||||
/// <summary>
|
||||
/// 秘钥长度。最大32
|
||||
/// </summary>
|
||||
public int KeyLength { get => keyLength; set => keyLength = value > 32 ? 32 : value; }
|
||||
}
|
||||
}
|
100
Falcon.SugarApi/Encryption/AESProvider.cs
Normal file
100
Falcon.SugarApi/Encryption/AESProvider.cs
Normal file
|
@ -0,0 +1,100 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Reflection.Metadata;
|
||||
using System.Security.Cryptography;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Falcon.SugarApi.Encryption
|
||||
{
|
||||
/// <summary>
|
||||
/// AES对称加密算法
|
||||
/// </summary>
|
||||
public class AESProvider : IEncryption, IAESEncryption
|
||||
{
|
||||
/// <summary>
|
||||
/// 通过提供配置构造AES加密实例
|
||||
/// </summary>
|
||||
/// <param name="config">配置文件</param>
|
||||
public AESProvider(AESConfig config) {
|
||||
Config = config;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 加密配置
|
||||
/// </summary>
|
||||
public AESConfig Config { get; }
|
||||
|
||||
/// <inheritdoc/>
|
||||
public string Decrypt(string key, string str) {
|
||||
var fullCipher = Convert.FromBase64String(str);
|
||||
var iv = new byte[16];
|
||||
var cipher = new byte[fullCipher.Length - iv.Length];
|
||||
Buffer.BlockCopy(fullCipher, 0, iv, 0, iv.Length);
|
||||
Buffer.BlockCopy(fullCipher, iv.Length, cipher, 0, fullCipher.Length - iv.Length);
|
||||
var decryptKey = GetDecryptKey(key);
|
||||
using (var aesAlg = Aes.Create()) {
|
||||
using (var decryptor = aesAlg.CreateDecryptor(decryptKey, iv)) {
|
||||
string result;
|
||||
using (var msDecrypt = new MemoryStream(cipher)) {
|
||||
using (var csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read)) {
|
||||
using (var srDecrypt = new StreamReader(csDecrypt)) {
|
||||
result = srDecrypt.ReadToEnd();
|
||||
}
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public string Encrypt(string key, string str) {
|
||||
var encryptKey = GetDecryptKey(key);
|
||||
using (var aesAlg = Aes.Create()) {
|
||||
var iv = aesAlg.IV;
|
||||
using (var encryptor = aesAlg.CreateEncryptor(encryptKey, iv)) {
|
||||
using (var msEncrypt = new MemoryStream()) {
|
||||
using (var csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write))
|
||||
using (var swEncrypt = new StreamWriter(csEncrypt)) {
|
||||
swEncrypt.Write(str);
|
||||
}
|
||||
var decryptedContent = msEncrypt.ToArray();
|
||||
var result = new byte[iv.Length + decryptedContent.Length];
|
||||
Buffer.BlockCopy(iv, 0, result, 0, iv.Length);
|
||||
Buffer.BlockCopy(decryptedContent, 0, result, iv.Length, decryptedContent.Length);
|
||||
return Convert.ToBase64String(result);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public string GenerateKey() {
|
||||
var chars = @"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ123456789,.!/*\";
|
||||
var r = new Random();
|
||||
var len = this.Config.KeyLength;
|
||||
var sb = new StringBuilder(len);
|
||||
for (int i = 0; i < len; i++) {
|
||||
sb.Append(chars[r.Next(0, chars.Length)]);
|
||||
}
|
||||
return sb.ToString();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 格式化加密key长度
|
||||
/// </summary>
|
||||
/// <param name="key">加密用的key</param>
|
||||
/// <returns>加密key字节数组</returns>
|
||||
protected byte[] GetDecryptKey(string key) {
|
||||
var buf = new byte[this.Config.KeyLength];
|
||||
var ek = Encoding.UTF8.GetBytes(key);
|
||||
var len = this.Config.KeyLength > ek.Length ? ek.Length : buf.Length;
|
||||
Buffer.BlockCopy(ek, 0, buf, 0, len);
|
||||
return buf;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
|
@ -34,5 +34,32 @@ namespace Falcon.SugarApi.Encryption
|
|||
services.AddSingleton<IDESEncryption>(new DESProvider(defConfig));
|
||||
return services;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 注册AES加密提供程序,通过IEncryption或IAESEncryption获取注册程序。
|
||||
/// </summary>
|
||||
/// <param name="services">服务集合</param>
|
||||
/// <param name="config">配置方法</param>
|
||||
/// <returns>服务集合</returns>
|
||||
public static IServiceCollection AddAesProvider(this IServiceCollection services, Action<AESConfig> config) {
|
||||
var defConfig = new AESConfig();
|
||||
config(defConfig);
|
||||
services.AddSingleton<IEncryption>(new AESProvider(defConfig));
|
||||
services.AddSingleton<IAESEncryption>(new AESProvider(defConfig));
|
||||
return services;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 使用默认配置注册AES加密提供程序,通过IEncryption或IAESEncryption获取注册程序。
|
||||
/// </summary>
|
||||
/// <param name="services">服务集合</param>
|
||||
/// <returns>服务集合</returns>
|
||||
public static IServiceCollection AddAesProvider(this IServiceCollection services) {
|
||||
var defConfig = new AESConfig();
|
||||
services.AddSingleton<IEncryption>(new AESProvider(defConfig));
|
||||
services.AddSingleton<IAESEncryption>(new AESProvider(defConfig));
|
||||
return services;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
|
7
Falcon.SugarApi/Encryption/IAESEncryption.cs
Normal file
7
Falcon.SugarApi/Encryption/IAESEncryption.cs
Normal file
|
@ -0,0 +1,7 @@
|
|||
namespace Falcon.SugarApi.Encryption
|
||||
{
|
||||
/// <summary>
|
||||
/// AES加密算法接口
|
||||
/// </summary>
|
||||
public interface IAESEncryption : IEncryption { }
|
||||
}
|
Loading…
Reference in New Issue
Block a user