Skip to content

Commit

Permalink
增加DES和AES加密算法
Browse files Browse the repository at this point in the history
  • Loading branch information
liukuo362573 committed May 8, 2021
1 parent cf1e070 commit 6d5c898
Show file tree
Hide file tree
Showing 4 changed files with 394 additions and 26 deletions.
4 changes: 2 additions & 2 deletions YiSha.Business/YiSha.Business/OrganizationManage/UserBLL.cs
Expand Up @@ -329,8 +329,8 @@ public async Task<TData> ImportUser(ImportParam param, List<UserEntity> list)
/// <returns></returns>
private string EncryptUserPassword(string password, string salt)
{
string md5Password = SecurityHelper.MD5Encrypt(password);
string encryptPassword = SecurityHelper.MD5Encrypt(md5Password + salt);
string md5Password = SecurityHelper.MD5ToHex(password);
string encryptPassword = SecurityHelper.MD5ToHex(md5Password.ToLower() + salt).ToLower();
return encryptPassword;
}

Expand Down
33 changes: 29 additions & 4 deletions YiSha.Test/YiSha.UtilTest/SecurityHelperTest.cs
Expand Up @@ -6,13 +6,38 @@ namespace YiSha.UtilTest
{
public class SecurityHelperTest
{
private string input = "我是谁 ABCD 1234 *=/.";

[Test]
public void TestMD5()
{
string result = SecurityHelper.MD5ToHex(input);

Assert.AreEqual("a7783d564da97a3846f5bf0f6b923d7f", result.ToLower());
}

[Test]
public void TestDES()
{
string ciperText = SecurityHelper.DESEncryptToBase64(input);
string result = SecurityHelper.DESDecryptFromBase64(ciperText);
Assert.AreEqual(input, result);

ciperText = SecurityHelper.DESEncryptToHex(input);
result = SecurityHelper.DESDecryptFromHex(ciperText);
Assert.AreEqual(input, result);
}

[Test]
public void TestMD5Encrypt()
public void TestAES()
{
string password = "123456";
string result = SecurityHelper.MD5Encrypt(password);
string ciperText = SecurityHelper.AESEncryptToBase64(input);
string result = SecurityHelper.AESDecryptFromBase64(ciperText);
Assert.AreEqual(input, result);

Assert.AreEqual("e10adc3949ba59abbe56e057f20f883e", result);
ciperText = SecurityHelper.AESEncryptToHex(input);
result = SecurityHelper.AESDecryptFromHex(ciperText);
Assert.AreEqual(input, result);
}
}
}
60 changes: 60 additions & 0 deletions YiSha.Util/YiSha.Util/EncodingHelper.cs
@@ -0,0 +1,60 @@
using System;
using System.Collections.Generic;
using System.Text;

namespace YiSha.Util
{
public class EncodingHelper
{
private static string HexStr = "0123456789abcdef";
private static char[] HexCharArr = HexStr.ToCharArray();

public static string ByteArrToHex(byte[] btArr)
{
char[] strArr = new char[btArr.Length * 2];
int i = 0;
foreach (byte bt in btArr)
{
strArr[i++] = HexCharArr[bt >> 4 & 0xf];
strArr[i++] = HexCharArr[bt & 0xf];
}
return new string(strArr);
}

public static byte[] HexToByteArr(string hexStr)
{
char[] charArr = hexStr.ToCharArray();
byte[] btArr = new byte[charArr.Length / 2];
int index = 0;
for (int i = 0; i < charArr.Length; i++)
{
int highBit = HexStr.IndexOf(charArr[i]);
int lowBit = HexStr.IndexOf(charArr[++i]);
btArr[index] = (byte)(highBit << 4 | lowBit);
index++;
}
return btArr;
}

public static string ByteArrToHexDefault(byte[] btArr)
{
StringBuilder sb = new StringBuilder();
foreach (byte b in btArr)
{
sb.Append(b.ToString("X2"));
}
return sb.ToString();
}

public static byte[] HexToByteArrDefault(string hexStr)
{
byte[] inputArr = new byte[hexStr.Length / 2];
for (int i = 0; i < hexStr.Length / 2; i++)
{
int v = Convert.ToInt32(hexStr.Substring(i * 2, 2), 16);
inputArr[i] = (byte)v;
}
return inputArr;
}
}
}

1 comment on commit 6d5c898

@veigarwang
Copy link

@veigarwang veigarwang commented on 6d5c898 May 10, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi, SecurityHelper.cs中的方法GetGuid(bool replaceDash = false)
replaceDash默认是false的话不会替换掉GUID中的“-”,字符串长度为36位,数据库里WebToken是varchar(32)的,登录时会出现“将截断字符串或二进制数据。 语句已终止。”的提示。是不是默认值设反了或者忘记修改相关调用了~ :)

Please sign in to comment.