?? mac.cs
字號:
using System;
using System.Collections.Generic;
using System.Text;
using System.Security;
using System.Security.Cryptography;
using System.IO;
using System.Runtime.InteropServices;
namespace WintecLib
{
public class Mac
{
//MD5不可逆加密
public static string Md5(string strPassword)
{
MD5CryptoServiceProvider hashmd5;
hashmd5 = new MD5CryptoServiceProvider();
return BitConverter.ToString(hashmd5.ComputeHash(Encoding.Default.GetBytes(strPassword))).Replace("-", "").ToLower();
}
/// <summary>
/// DES解密
/// </summary>
/// <param name="Key">密鑰</param>
/// <param name="Data">數據</param>
/// <returns>解密后的數據</returns>
public static byte[] DESDecrypt(byte[] Key, byte[] Data)
{
try
{
DESCryptoServiceProvider MyServiceProvider = new DESCryptoServiceProvider();
//計算des加密所采用的算法
MyServiceProvider.Mode = CipherMode.CBC;
//計算填充類型
MyServiceProvider.Padding = PaddingMode.Zeros;
ICryptoTransform MyTransform = MyServiceProvider.CreateDecryptor(Key, new byte[8] { 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0 });
//CryptoStream對象的作用是將數據流連接到加密轉換的流
MemoryStream ms = new MemoryStream();
CryptoStream MyCryptoStream = new CryptoStream(ms, MyTransform, CryptoStreamMode.Write);
//將字節數組中的數據寫入到加密流中
MyCryptoStream.Write(Data, 0, Data.Length);
MyCryptoStream.FlushFinalBlock();
MyCryptoStream.Close();
byte[] byEncRet = ms.ToArray();
ms.Close();
return byEncRet;
}
catch (Exception ex)
{
Console.WriteLine("Exception caught: {0}", ex.Message);
return new byte[8];
}
}
/// <summary>
/// DES加密
/// </summary>
/// <param name="Key">密鑰</param>
/// <param name="Data">數據</param>
/// <returns>加密后的數據</returns>
public static byte[] DESEncrypt(byte[] Key, byte[] Data)
{
try
{
DESCryptoServiceProvider MyServiceProvider = new DESCryptoServiceProvider();
//計算des加密所采用的算法
MyServiceProvider.Mode = CipherMode.CBC;
//計算填充類型
MyServiceProvider.Padding = PaddingMode.Zeros;
ICryptoTransform MyTransform = MyServiceProvider.CreateEncryptor(Key, new byte[8] { 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0 });
//CryptoStream對象的作用是將數據流連接到加密轉換的流
MemoryStream ms = new MemoryStream();
CryptoStream MyCryptoStream = new CryptoStream(ms, MyTransform, CryptoStreamMode.Write);
//將字節數組中的數據寫入到加密流中
MyCryptoStream.Write(Data, 0, Data.Length);
MyCryptoStream.FlushFinalBlock();
MyCryptoStream.Close();
byte[] byEncRet = ms.ToArray();
ms.Close();
return byEncRet;
}
catch (Exception ex)
{
Console.WriteLine("Exception caught: {0}", ex.Message);
return new byte[8];
}
}
/// <summary>
/// 對異或后的8位數據進行加密,位數不足8位補0x0
/// </summary>
/// <param name="Key">密鑰</param>
/// <param name="Data">數據</param>
/// <returns>加密后的數據</returns>
public static byte[] HCDES(byte[] Key, byte[] Data)
{
try
{
//創建一個DES算法的加密類
DESCryptoServiceProvider MyServiceProvider = new DESCryptoServiceProvider();
MyServiceProvider.Mode = CipherMode.CBC;
MyServiceProvider.Padding = PaddingMode.None;
//從DES算法的加密類對象的CreateEncryptor方法,創建一個加密轉換接口對象
//第一個參數的含義是:對稱算法的機密密鑰(長度為64位,也就是8個字節)
// 可以人工輸入,也可以隨機生成方法是:MyServiceProvider.GenerateKey();
//第二個參數的含義是:對稱算法的初始化向量(長度為64位,也就是8個字節)
// 可以人工輸入,也可以隨機生成方法是:MyServiceProvider.GenerateIV()
//
ICryptoTransform MyTransform = MyServiceProvider.CreateEncryptor(Key,Key);// new byte[8] { 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0 });
//CryptoStream對象的作用是將數據流連接到加密轉換的流
MemoryStream ms = new MemoryStream();
CryptoStream MyCryptoStream = new CryptoStream(ms, MyTransform, CryptoStreamMode.Write);
//將字節數組中的數據寫入到加密流中
MyCryptoStream.Write(Data, 0, Data.Length);
//關閉加密流對象
byte[] bEncRet = new byte[8];
// Array.Copy(ms.GetBuffer(), bEncRet, ms.Length);
bEncRet = ms.ToArray(); // MyCryptoStream關閉之前ms.Length 為8, 關閉之后為16
MyCryptoStream.FlushFinalBlock();
MyCryptoStream.Close();
byte[] bTmp = ms.ToArray();
ms.Close();
// return bEncRet;
return bTmp;//
}
catch (Exception ex)
{
Console.WriteLine("HCDES Exception Caught, Exception = {0}", ex.Message);
return new byte[8];
}
}
/// <summary>
/// Mac驗證碼的數據生成講數據分為8位1組進行異或后加密
/// </summary>
/// <param name="key">密鑰</param>
/// <param name="MacData">數據</param>
/// <returns>加密后的Mac驗證碼</returns>
public static byte[] MAC_CBC(byte[] key,byte[] MacData)
{
int count = 0;
if (MacData.Length % 8 != 0)
{
count = 8 - (MacData.Length % 8);
}
byte[] tmpData = new byte[count];
for (int index = 0; index < tmpData.Length; index++)
{
tmpData[index] = new byte[] { 0 }[0x30];
}
byte[] newMacData = new byte[MacData.Length + tmpData.Length];
Array.Copy(MacData, 0, newMacData, 0, MacData.Length);
Array.Copy(tmpData, 0, newMacData, MacData.Length, tmpData.Length);
byte[] bKey=new byte[8];
bKey=key;
try
{
int iGroup = 0;
byte[] bIV = new byte[] { 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0 };
byte[] bTmpBuf1 = new byte[8];
byte[] bTmpBuf2 = new byte[8];
Array.Copy(bIV, bTmpBuf1, 8);
if ((newMacData.Length % 8 == 0))
iGroup = newMacData.Length / 8;
else
iGroup = newMacData.Length / 8 + 1;
int i = 0;
int j = 0;
for (i = 0; i < iGroup; i++)
{
Array.Copy(newMacData, 8 * i, bTmpBuf2, 0, 8);
for (j = 0; j < 8; j++)
bTmpBuf1[j] = (byte)(bTmpBuf1[j] ^ bTmpBuf2[j]);
}
bTmpBuf2 = HCDES(bKey, bTmpBuf1);
return bTmpBuf2;
}
catch
{
return new byte[8];
}
}
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -