增加DES加密方法,并完成测试

This commit is contained in:
falcon 2022-09-05 12:36:33 +08:00
parent aad7cb83f2
commit 2733612bce
6 changed files with 265 additions and 0 deletions

View File

@ -0,0 +1,77 @@
using Falcon.SugarApi.Encryption;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.Text;
namespace Falcon.SugarApi.Test.Encryption
{
/// <summary>
/// RSA测试类
/// </summary>
[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>
/// 测试公钥、秘钥生成,加密解密
/// </summary>
[TestMethod("DES测试")]
public void DESTest() {
var config = new DESConfig() { };
IDESEncryption r = new DESProvider(config);
var k = r.GenerateKey();
//var bs = Encoding.UTF8.GetBytes(mingw);
//var a1 = Convert.ToBase64String(bs);
//var a2 = Convert.FromBase64String(a1);
//Assert.AreEqual(bs.Length, a2.Length);
for (int i = 0; i < 1000; i++) {
var mingw = GenerateStr();
Console.WriteLine(mingw);
var p = new DESProvider(config);
var miwen = p.Encrypt(k, mingw);
var p1 = new DESProvider(config);
var mingw1 = p1.Decrypt(k, miwen);
Assert.AreEqual(mingw, mingw1, "解密后明文不同");
}
}
/// <summary>
/// 生成随机长度,由字符表内字符组成的字符串
/// </summary>
/// <returns></returns>
private string GenerateStr() {
var chars = @"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ123456789,.!/*\";
var r = new Random();
var len = r.Next(100);
var sb = new StringBuilder(len);
for (int i = 0; i < len; i++) {
sb.Append(chars[r.Next(0, chars.Length)]);
}
return sb.ToString();
}
}
}

View File

@ -0,0 +1,32 @@
using Microsoft.Extensions.Options;
using System.Security.Cryptography;
using System.Text;
namespace Falcon.SugarApi.Encryption
{
/// <summary>
/// DES加密配置
/// </summary>
public class DESConfig:IOptions<DESConfig>
{
/// <summary>
/// 加解密编码方式
/// </summary>
public Encoding Encoding { get; set; } = Encoding.UTF8;
/// <summary>
/// 填充方式
/// </summary>
public PaddingMode Padding { get; set; } = PaddingMode.PKCS7;
/// <summary>
/// 加密模式
/// </summary>
public CipherMode Mode { get; set; } = CipherMode.CBC;
/// <summary>
/// 加密用的IV
/// </summary>
public byte[] IV { get; set; } = { 123, 221, 221, 111, 4, 6, 7, 22 };
/// <inheritdoc/>
public DESConfig Value => this;
}
}

View File

@ -0,0 +1,83 @@
using System;
using System.IO;
using System.Security.Cryptography;
using System.Text;
namespace Falcon.SugarApi.Encryption
{
/// <summary>
/// DES对称加密
/// </summary>
public class DESProvider : IEncryption, IDESEncryption
{
/// <summary>
/// 配置文件
/// </summary>
public DESConfig Config { get; set; }
/// <summary>
/// 通过提供配置创建DES提供程序
/// </summary>
/// <param name="config"></param>
public DESProvider(DESConfig config) {
Config = config;
}
/// <summary>
/// 创建加解密提供程序
/// </summary>
/// <param name="key">加密用的key</param>
/// <returns>加解密提供器</returns>
private DESCryptoServiceProvider createProvider(string key) {
if (key.Length < 8) {
throw new Exception("加密用的key长度为8位");
}
byte[] bKey = this.Config.Encoding.GetBytes(key.Substring(0, 8));
return new DESCryptoServiceProvider() {
Padding = Config.Padding,
Mode = Config.Mode,
IV=Config.IV,
Key = bKey,
};
}
/// <inheritdoc/>
public string GenerateKey() {
var p = new DESCryptoServiceProvider() {
Padding = Config.Padding,
Mode = Config.Mode,
IV = Config.IV,
};
p.GenerateKey();
return Convert.ToBase64String(p.Key);
}
/// <inheritdoc/>
public string Decrypt(string key, string str) {
var bStr = Convert.FromBase64String(str);
var desc = createProvider(key);
using (var mStream = new MemoryStream()) {
using (var cStream = new CryptoStream(mStream, desc.CreateDecryptor(), CryptoStreamMode.Write)) {
cStream.Write(bStr, 0, bStr.Length);
cStream.FlushFinalBlock();
var bs = mStream.ToArray();
return this.Config.Encoding.GetString(bs);
}
}
}
/// <inheritdoc/>
public string Encrypt(string key, string str) {
byte[] bStr = this.Config.Encoding.GetBytes(str);
var desc = createProvider(key);
using (var mStream = new MemoryStream()) {
using (var cStream = new CryptoStream(mStream, desc.CreateEncryptor(), CryptoStreamMode.Write)) {
cStream.Write(bStr, 0, bStr.Length);
cStream.FlushFinalBlock();
var bs = mStream.ToArray();
return Convert.ToBase64String(bs);
}
}
}
}
}

View File

@ -0,0 +1,38 @@
using Microsoft.Extensions.DependencyInjection;
using System;
namespace Falcon.SugarApi.Encryption
{
/// <summary>
/// 安全组件注册
/// </summary>
public static class EncryptionProviderRegirst
{
/// <summary>
/// 注册DES加密提供程序通过IEncryption或IDESEncryption获取注册程序。
/// </summary>
/// <param name="services">服务集合</param>
/// <param name="config">配置方法</param>
/// <returns>服务集合</returns>
public static IServiceCollection AddDesProvider(this IServiceCollection services, Action<DESConfig> config) {
var defConfig = new DESConfig();
config(defConfig);
services.AddSingleton<IEncryption>(new DESProvider(defConfig));
services.AddSingleton<IDESEncryption>(new DESProvider(defConfig));
return services;
}
/// <summary>
/// 使用默认配置注册DES加密提供程序通过IEncryption或IDESEncryption获取注册程序。
/// </summary>
/// <param name="services">服务集合</param>
/// <returns>服务集合</returns>
public static IServiceCollection AddDesProvider(this IServiceCollection services) {
var defConfig = new DESConfig();
services.AddSingleton<IEncryption>(new DESProvider(defConfig));
services.AddSingleton<IDESEncryption>(new DESProvider(defConfig));
return services;
}
}
}

View File

@ -0,0 +1,7 @@
namespace Falcon.SugarApi.Encryption
{
/// <summary>
/// DES加密算法接口
/// </summary>
public interface IDESEncryption : IEncryption { }
}

View File

@ -0,0 +1,28 @@
namespace Falcon.SugarApi.Encryption
{
/// <summary>
/// 基础加密解密接口
/// </summary>
public interface IEncryption
{
/// <summary>
/// 通过key解密字符串
/// </summary>
/// <param name="key">key</param>
/// <param name="str">密文</param>
/// <returns>明文</returns>
string Decrypt(string key, string str);
/// <summary>
/// 通过key加密字符串
/// </summary>
/// <param name="key">key</param>
/// <param name="str">明文</param>
/// <returns>密文</returns>
string Encrypt(string key, string str);
/// <summary>
/// 生成加密key
/// </summary>
/// <returns>key</returns>
string GenerateKey();
}
}