(#9)添加加解密方法的单元测试。修改了一处加密算法错误

This commit is contained in:
falcon 2019-03-07 16:10:37 +08:00
parent 629a549cdd
commit 34003dd3da
3 changed files with 42 additions and 1 deletions

View File

@ -90,7 +90,7 @@ namespace Cmdjy.Bll
/// <param name="IV">初始化向量</param> /// <param name="IV">初始化向量</param>
/// <param name="key">加密关键字</param> /// <param name="key">加密关键字</param>
public static DesHelper GetHelper(PaddingMode padding,CipherMode cipher,byte[] IV,string key) { public static DesHelper GetHelper(PaddingMode padding,CipherMode cipher,byte[] IV,string key) {
if(key.Length <= 8) { if(key.Length < 8) {
throw new Exception("加密用的key长度为8位"); throw new Exception("加密用的key长度为8位");
} }
return new DesHelper { return new DesHelper {

View File

@ -0,0 +1,40 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Cmdjy.Bll;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Security.Cryptography;
namespace Cmdjy.Bll.Tests
{
[TestClass()]
public class DesHelperTests
{
[TestMethod()]
public void EncryptyTest() {
byte[] IV = { 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08 };
var des = DesHelper.GetHelper(PaddingMode.PKCS7,CipherMode.CBC,IV,"12365478");
Console.WriteLine($"Key:{des.Key}");
string mingwen = getMingWen();
Console.WriteLine($"明文:{mingwen}");
var miwen = des.Encrypty(mingwen);
Console.WriteLine($"密文:{miwen}");
var mingwen2 = des.DesCrypty(miwen);
Assert.AreEqual(mingwen,mingwen2);
}
public string getMingWen() {
string str = "abcdefghijklmnopqrstuvwxyz123456789,./*-+";
var result = new StringBuilder();
int len = 1000;
var rean = new Random();
for(int i = 0;i < len;i++) {
var p = rean.Next(str.Length);
result.Append(str.ToCharArray()[p]);
}
return result.ToString();
}
}
}

View File

@ -51,6 +51,7 @@
</Otherwise> </Otherwise>
</Choose> </Choose>
<ItemGroup> <ItemGroup>
<Compile Include="Bll\DesHelperTests.cs" />
<Compile Include="Bll\ObjExtendTests.cs" /> <Compile Include="Bll\ObjExtendTests.cs" />
<Compile Include="Properties\AssemblyInfo.cs" /> <Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup> </ItemGroup>