增加对象加密和解密方法。
This commit is contained in:
parent
a3bd83ff03
commit
37a6942020
|
@ -48,9 +48,10 @@ namespace Falcon.SugarApi.Test.Encryption
|
||||||
IAESEncryption r = new AESProvider(config);
|
IAESEncryption r = new AESProvider(config);
|
||||||
var k = r.GenerateKey();
|
var k = r.GenerateKey();
|
||||||
|
|
||||||
|
//测试字符串加密
|
||||||
for (int i = 0; i < 1000; i++) {
|
for (int i = 0; i < 1000; i++) {
|
||||||
var mingw = GenerateStr();
|
var mingw = GenerateStr();
|
||||||
Console.WriteLine(mingw);
|
//Console.WriteLine(mingw);
|
||||||
var p = new AESProvider(config);
|
var p = new AESProvider(config);
|
||||||
var miwen = p.Encrypt(k, mingw);
|
var miwen = p.Encrypt(k, mingw);
|
||||||
|
|
||||||
|
@ -60,6 +61,18 @@ namespace Falcon.SugarApi.Test.Encryption
|
||||||
Assert.AreEqual(mingw, mingw1, "解密后明文不同");
|
Assert.AreEqual(mingw, mingw1, "解密后明文不同");
|
||||||
|
|
||||||
}
|
}
|
||||||
|
//测试对象加密
|
||||||
|
var obj = new TestObj { Id = 1, Name = "abc" };
|
||||||
|
var ma = r.Encrypt(k, obj);
|
||||||
|
Assert.IsNotNull(ma);
|
||||||
|
var obj2 = r.Decrypt<TestObj>(k, ma);
|
||||||
|
Assert.IsNotNull(obj2);
|
||||||
|
Assert.IsTrue(obj.Id == obj2.Id);
|
||||||
|
Assert.IsTrue(obj.Name == obj2.Name);
|
||||||
|
Assert.ThrowsException<ArgumentNullException>(() => r.Encrypt<TestObj>(k, null));
|
||||||
|
Assert.ThrowsException<ArgumentNullException>(() => r.Encrypt<TestObj>(null, obj));
|
||||||
|
Assert.ThrowsException<ArgumentNullException>(() => r.Decrypt<TestObj>(k, null));
|
||||||
|
Assert.ThrowsException<ArgumentNullException>(() => r.Decrypt<TestObj>(null, ma));
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
@ -76,5 +89,11 @@ namespace Falcon.SugarApi.Test.Encryption
|
||||||
}
|
}
|
||||||
return sb.ToString();
|
return sb.ToString();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
class TestObj
|
||||||
|
{
|
||||||
|
public int Id { get; set; }
|
||||||
|
public string Name { get; set; }
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
47
Falcon.SugarApi/Encryption/IEncryptionExtend.cs
Normal file
47
Falcon.SugarApi/Encryption/IEncryptionExtend.cs
Normal file
|
@ -0,0 +1,47 @@
|
||||||
|
using Falcon.SugarApi.JsonSerialize;
|
||||||
|
using System;
|
||||||
|
|
||||||
|
namespace Falcon.SugarApi.Encryption
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// IEncryption接口扩展
|
||||||
|
/// </summary>
|
||||||
|
public static class IEncryptionExtend
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// 对象序列化接口
|
||||||
|
/// </summary>
|
||||||
|
public static IJsonSerialize JsonSerialize { get; set; } = new JsonSerializeFactory().CreateJsonSerialize();
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 加密对象
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="T">对象类型</typeparam>
|
||||||
|
/// <param name="encryption">加密接口</param>
|
||||||
|
/// <param name="obj">要加密的对象</param>
|
||||||
|
/// <param name="key">加密用的key</param>
|
||||||
|
/// <returns>加密后结果</returns>
|
||||||
|
/// <exception cref="ArgumentNullException">参数为null</exception>
|
||||||
|
public static string Encrypt<T>(this IEncryption encryption, string key, T obj) {
|
||||||
|
obj.ThrowNullExceptionWhenNull();
|
||||||
|
key.ThrowNullExceptionWhenNull();
|
||||||
|
return encryption.Encrypt(key, JsonSerialize.Serialize(obj));
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 解密对象
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="T">对象类型</typeparam>
|
||||||
|
/// <param name="encryption">加密接口</param>
|
||||||
|
/// <param name="key">加密用的key</param>
|
||||||
|
/// <param name="strObj">对象密文</param>
|
||||||
|
/// <returns>解密后的对象</returns>
|
||||||
|
/// <exception cref="ArgumentNullException">参数为null</exception>
|
||||||
|
public static T? Decrypt<T>(this IEncryption encryption, string key, string strObj)
|
||||||
|
where T : class {
|
||||||
|
key.ThrowNullExceptionWhenNull();
|
||||||
|
strObj.ThrowNullExceptionWhenNull();
|
||||||
|
return JsonSerialize.Deserialize<T>(encryption.Decrypt(key, strObj));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user