-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
将MD5移动位置 新增SHA1 SHA256哈希算法
- Loading branch information
Showing
12 changed files
with
313 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
using Jx.Toolbox.Cryptography; | ||
|
||
namespace Jx.Toolbox.Test; | ||
|
||
public class AesTest | ||
{ | ||
[Fact] | ||
public void EncryptTest() | ||
{ | ||
var key = "1234567890ABCDEF1234567890ABCDEF"; | ||
var iv = "1234567890ABCDEF1234567890ABCDEF"; | ||
var data = "test"; | ||
var encrypted = AesEncryption.Encrypt(data, key, iv); | ||
var decrypted = AesEncryption.Decrypt(encrypted, key, iv); | ||
Assert.Equal(data, decrypted); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
using Jx.Toolbox.Cryptography; | ||
|
||
namespace Jx.Toolbox.Test; | ||
|
||
public class DesTest | ||
{ | ||
[Fact] | ||
public void EncryptTest() | ||
{ | ||
var key = "2B7E151628AED2A6"; | ||
var iv = "AABB09182736CCDD"; | ||
var data = "test"; | ||
var encrypted = DesEncryption.Encrypt(data, key, iv); | ||
var decrypted = DesEncryption.Decrypt(encrypted, key, iv); | ||
Assert.Equal(data, decrypted); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
using Jx.Toolbox.Cryptography; | ||
using Jx.Toolbox.Hash; | ||
|
||
namespace Jx.Toolbox.Test; | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
using System.IO; | ||
using System.Security.Cryptography; | ||
using System.Text; | ||
using Jx.Toolbox.Extensions; | ||
using Jx.Toolbox.Utils; | ||
|
||
namespace Jx.Toolbox.Cryptography | ||
{ | ||
/// <summary> | ||
/// Aes加解密 | ||
/// </summary> | ||
public class AesEncryption | ||
{ | ||
public static string Encrypt(string plainText, string key, string iv, CipherMode cipherMode = CipherMode.CBC, | ||
PaddingMode paddingMode = PaddingMode.PKCS7) | ||
{ | ||
return Base64.Encode(Encrypt(plainText, key.HexStringToBytes(), iv.HexStringToBytes(), cipherMode, paddingMode)); | ||
} | ||
|
||
/// <summary> | ||
/// Aes加密 | ||
/// </summary> | ||
/// <param name="plainText"></param> | ||
/// <param name="key"></param> | ||
/// <param name="iv"></param> | ||
/// <param name="cipherMode"></param> | ||
/// <param name="paddingMode"></param> | ||
/// <returns></returns> | ||
public static byte[] Encrypt(string plainText, byte[] key, byte[] iv, CipherMode cipherMode = CipherMode.CBC, | ||
PaddingMode paddingMode = PaddingMode.PKCS7) | ||
{ | ||
using (var aesAlg = Aes.Create()) | ||
{ | ||
aesAlg.Key = key; | ||
aesAlg.IV = iv; | ||
aesAlg.Padding = paddingMode; | ||
aesAlg.Mode = cipherMode; | ||
|
||
ICryptoTransform encryptor = aesAlg.CreateEncryptor(aesAlg.Key, aesAlg.IV); | ||
using (MemoryStream msEncrypt = new MemoryStream()) | ||
{ | ||
using (CryptoStream csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write)) | ||
{ | ||
using (StreamWriter swEncrypt = new StreamWriter(csEncrypt)) | ||
{ | ||
swEncrypt.Write(plainText); | ||
} | ||
} | ||
return msEncrypt.ToArray(); | ||
} | ||
} | ||
} | ||
|
||
public static string Decrypt(string cipherText, string key, string iv, CipherMode cipherMode = CipherMode.CBC, | ||
PaddingMode paddingMode = PaddingMode.PKCS7) | ||
{ | ||
return Decrypt(Base64.Decode(cipherText), key.HexStringToBytes(), iv.HexStringToBytes(), cipherMode, paddingMode); | ||
} | ||
|
||
public static string Decrypt(byte[] cipherText, byte[] key, byte[] iv, CipherMode cipherMode = CipherMode.CBC, | ||
PaddingMode paddingMode = PaddingMode.PKCS7) | ||
{ | ||
using (Aes aesAlg = Aes.Create()) | ||
{ | ||
aesAlg.Key = key; | ||
aesAlg.IV = iv; | ||
aesAlg.Padding = paddingMode; | ||
aesAlg.Mode = cipherMode; | ||
|
||
ICryptoTransform decryptor = aesAlg.CreateDecryptor(aesAlg.Key, aesAlg.IV); | ||
using (MemoryStream msDecrypt = new MemoryStream(cipherText)) | ||
{ | ||
using (CryptoStream csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read)) | ||
{ | ||
using (StreamReader srDecrypt = new StreamReader(csDecrypt)) | ||
{ | ||
return srDecrypt.ReadToEnd(); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
using System.IO; | ||
using System.Security.Cryptography; | ||
using Jx.Toolbox.Extensions; | ||
using Jx.Toolbox.Utils; | ||
|
||
namespace Jx.Toolbox.Cryptography | ||
{ | ||
public class DesEncryption | ||
{ | ||
public static string Encrypt(string plainText, string key, string iv, CipherMode cipherMode = CipherMode.CBC, | ||
PaddingMode paddingMode = PaddingMode.PKCS7) | ||
{ | ||
return Base64.Encode(Encrypt(plainText, key.HexStringToBytes(), iv.HexStringToBytes(), cipherMode, paddingMode)); | ||
} | ||
|
||
public static byte[] Encrypt(string plainText, byte[] key, byte[] iv, CipherMode cipherMode = CipherMode.CBC, | ||
PaddingMode paddingMode = PaddingMode.PKCS7) | ||
{ | ||
using (DESCryptoServiceProvider des = new DESCryptoServiceProvider()) | ||
{ | ||
des.Key = key; | ||
des.IV = iv; | ||
des.Mode = cipherMode; | ||
des.Padding = paddingMode; | ||
|
||
ICryptoTransform encryptor = des.CreateEncryptor(des.Key, des.IV); | ||
using (MemoryStream msEncrypt = new MemoryStream()) | ||
{ | ||
using (CryptoStream csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write)) | ||
{ | ||
using (StreamWriter swEncrypt = new StreamWriter(csEncrypt)) | ||
{ | ||
swEncrypt.Write(plainText); | ||
} | ||
} | ||
return msEncrypt.ToArray(); | ||
} | ||
} | ||
} | ||
|
||
public static string Decrypt(string cipherText, string key, string iv, CipherMode cipherMode = CipherMode.CBC, | ||
PaddingMode paddingMode = PaddingMode.PKCS7) | ||
{ | ||
return Decrypt(Base64.Decode(cipherText), key.HexStringToBytes(), iv.HexStringToBytes(), cipherMode, paddingMode); | ||
} | ||
|
||
public static string Decrypt(byte[] cipherText, byte[] key, byte[] iv, CipherMode cipherMode = CipherMode.CBC, | ||
PaddingMode paddingMode = PaddingMode.PKCS7) | ||
{ | ||
using (DESCryptoServiceProvider des = new DESCryptoServiceProvider()) | ||
{ | ||
des.Key = key; | ||
des.IV = iv; | ||
des.Mode = cipherMode; | ||
des.Padding = paddingMode; | ||
|
||
ICryptoTransform decryptor = des.CreateDecryptor(des.Key, des.IV); | ||
using (MemoryStream msDecrypt = new MemoryStream(cipherText)) | ||
{ | ||
using (CryptoStream csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read)) | ||
{ | ||
using (StreamReader srDecrypt = new StreamReader(csDecrypt)) | ||
{ | ||
return srDecrypt.ReadToEnd(); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
using System; | ||
|
||
namespace Jx.Toolbox.Extensions | ||
{ | ||
/// <summary> | ||
/// byte数组扩展 | ||
/// </summary> | ||
public static class ByteArrayExtension | ||
{ | ||
/// <summary> | ||
/// byte数组转16进制字符串 | ||
/// </summary> | ||
/// <param name="bytes"></param> | ||
/// <returns></returns> | ||
public static string ToHexString(this byte[] bytes) | ||
{ | ||
return BitConverter.ToString(bytes).Replace("-", ""); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
using System.Text; | ||
|
||
namespace Jx.Toolbox.Cryptography | ||
namespace Jx.Toolbox.Hash | ||
{ | ||
public static class MD5 | ||
{ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
using System.Security.Cryptography; | ||
using System.Text; | ||
|
||
namespace Jx.Toolbox.Hash | ||
{ | ||
/// <summary> | ||
/// SHA类 | ||
/// </summary> | ||
public class ShaHash | ||
{ | ||
/// <summary> | ||
/// SHA1 | ||
/// </summary> | ||
/// <param name="rawData"></param> | ||
/// <returns></returns> | ||
public static string ComputeSha1Hash(string rawData) | ||
{ | ||
using (SHA1 sha1 = SHA1.Create()) | ||
{ | ||
byte[] bytes = sha1.ComputeHash(Encoding.UTF8.GetBytes(rawData)); | ||
StringBuilder builder = new StringBuilder(); | ||
for (int i = 0; i < bytes.Length; i++) | ||
{ | ||
builder.Append(bytes[i].ToString("x2")); | ||
} | ||
return builder.ToString(); | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// SHA256 | ||
/// </summary> | ||
/// <param name="rawData"></param> | ||
/// <returns></returns> | ||
public static string ComputeSha256Hash(string rawData) | ||
{ | ||
using (SHA256 sha256 = SHA256.Create()) | ||
{ | ||
byte[] bytes = sha256.ComputeHash(Encoding.UTF8.GetBytes(rawData)); | ||
StringBuilder builder = new StringBuilder(); | ||
for (int i = 0; i < bytes.Length; i++) | ||
{ | ||
builder.Append(bytes[i].ToString("x2")); | ||
} | ||
return builder.ToString(); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters