135 lines
4.5 KiB
C#
135 lines
4.5 KiB
C#
|
|
using System.Security.Cryptography;
|
|||
|
|
using System.Text;
|
|||
|
|
|
|||
|
|
namespace IotManager.Common
|
|||
|
|
{
|
|||
|
|
public class Base64
|
|||
|
|
{
|
|||
|
|
public static void MainTest()
|
|||
|
|
{
|
|||
|
|
// 要加密的原始字符串
|
|||
|
|
string originalText = "Hello, World!";
|
|||
|
|
|
|||
|
|
// 加密成Base64字符串
|
|||
|
|
string base64EncodedText = EncodeBase64(originalText);
|
|||
|
|
Console.WriteLine("Base64 编码结果: " + base64EncodedText);
|
|||
|
|
|
|||
|
|
// 解密Base64字符串
|
|||
|
|
string decodedText = DecodeBase64(base64EncodedText);
|
|||
|
|
Console.WriteLine("Base64 解码结果: " + decodedText);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// 使用Base64编码字符串
|
|||
|
|
public static string EncodeBase64(string text)
|
|||
|
|
{
|
|||
|
|
byte[] bytesToEncode = Encoding.UTF8.GetBytes(text);
|
|||
|
|
string encodedText = Convert.ToBase64String(bytesToEncode);
|
|||
|
|
return encodedText;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// 使用Base64解码字符串
|
|||
|
|
public static string DecodeBase64(string encodedText)
|
|||
|
|
{
|
|||
|
|
byte[] decodedBytes = Convert.FromBase64String(encodedText);
|
|||
|
|
string decodedText = Encoding.UTF8.GetString(decodedBytes);
|
|||
|
|
return decodedText;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
|
|||
|
|
|
|||
|
|
// 使用3DES-MAC签名消息
|
|||
|
|
public static string Sign3DESMAC(string key, string message)
|
|||
|
|
{
|
|||
|
|
byte[] keyBytes = Encoding.UTF8.GetBytes(key);
|
|||
|
|
byte[] messageBytes = Encoding.UTF8.GetBytes(message);
|
|||
|
|
|
|||
|
|
using (TripleDESCryptoServiceProvider des = new TripleDESCryptoServiceProvider())
|
|||
|
|
{
|
|||
|
|
des.Key = keyBytes;
|
|||
|
|
des.Mode = CipherMode.ECB; // 3DES-MAC通常使用ECB模式
|
|||
|
|
des.Padding = PaddingMode.PKCS7; // PKCS7填充
|
|||
|
|
|
|||
|
|
using (HMACMD5 hmac = new HMACMD5(des.Key))
|
|||
|
|
{
|
|||
|
|
byte[] hashBytes = hmac.ComputeHash(messageBytes);
|
|||
|
|
return BitConverter.ToString(hashBytes).Replace("-", "").ToLower();
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// 验证3DES-MAC签名
|
|||
|
|
public static bool Verify3DESMAC(string key, string message, string macToVerify)
|
|||
|
|
{
|
|||
|
|
string calculatedMAC = Sign3DESMAC(key, message);
|
|||
|
|
return string.Equals(calculatedMAC, macToVerify, StringComparison.OrdinalIgnoreCase);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
|
|||
|
|
|
|||
|
|
/// <summary>
|
|||
|
|
/// 取前16个字节
|
|||
|
|
/// </summary>
|
|||
|
|
/// <param name="message"></param>
|
|||
|
|
/// <param name="key"></param>
|
|||
|
|
/// <returns></returns>
|
|||
|
|
public static byte[] ComputeHMACSHA256Short(string message, string key)
|
|||
|
|
{
|
|||
|
|
using (HMACSHA256 hmac = new HMACSHA256(Encoding.UTF8.GetBytes(key)))
|
|||
|
|
{
|
|||
|
|
byte[] messageBytes = Encoding.UTF8.GetBytes(message);
|
|||
|
|
byte[] fullHash = hmac.ComputeHash(messageBytes);
|
|||
|
|
|
|||
|
|
// 对完整哈希再做一次SHA256并取前16字节
|
|||
|
|
using (SHA256 sha = SHA256.Create())
|
|||
|
|
{
|
|||
|
|
byte[] rehashed = sha.ComputeHash(fullHash);
|
|||
|
|
byte[] result = new byte[16];
|
|||
|
|
Array.Copy(rehashed, result, 16);
|
|||
|
|
return result;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
public static byte[] ComputeHMACSHA256(string message, string key)
|
|||
|
|
{
|
|||
|
|
using (HMACSHA256 hmac = new HMACSHA256(Encoding.UTF8.GetBytes(key)))
|
|||
|
|
{
|
|||
|
|
byte[] messageBytes = Encoding.UTF8.GetBytes(message);
|
|||
|
|
return hmac.ComputeHash(messageBytes);
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
|
|||
|
|
public static bool VerifyHMACSHA256(byte[] message, byte[] key, byte[] expectedMac)
|
|||
|
|
{
|
|||
|
|
using (HMACSHA256 hmac = new HMACSHA256(key))
|
|||
|
|
{
|
|||
|
|
byte[] computedMac = hmac.ComputeHash(message);
|
|||
|
|
return CryptographicOperations.FixedTimeEquals(computedMac, expectedMac); // 防止时间攻击
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
|
|||
|
|
static void Test()
|
|||
|
|
{
|
|||
|
|
string key = "ThisIsASecretKey"; // 密钥长度必须是24字节(192位)
|
|||
|
|
string message = "Hello, World!";
|
|||
|
|
|
|||
|
|
// 使用3DES-MAC签名消息
|
|||
|
|
string mac = Sign3DESMAC(key, message);
|
|||
|
|
Console.WriteLine("3DES-MAC 签名: " + mac);
|
|||
|
|
|
|||
|
|
// 验证3DES-MAC签名
|
|||
|
|
bool isVerified = Verify3DESMAC(key, message, mac);
|
|||
|
|
if (isVerified)
|
|||
|
|
{
|
|||
|
|
Console.WriteLine("消息验证成功!");
|
|||
|
|
}
|
|||
|
|
else
|
|||
|
|
{
|
|||
|
|
Console.WriteLine("消息验证失败!");
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|