public class TrippleDES
{
public string Encode(string str, string key, string iv)
{
byte[] keyByte = getKeyByteArray(key);
byte[] ivByte = getKeyByteArray(iv);
MemoryStream fin = new MemoryStream(Encoding.UTF8.GetBytes(str), false);
MemoryStream fout = new MemoryStream();
fout.SetLength(0L);
byte[] bin = new byte[100];
long rdlen = 0;
long totlen = fin.Length;
int len = 0;
TripleDESCryptoServiceProvider tdes = new TripleDESCryptoServiceProvider();
CryptoStream encStream = new CryptoStream(fout, tdes.CreateEncryptor(keyByte, ivByte), CryptoStreamMode.Write);
while (rdlen < totlen)
{
len = fin.Read(bin, 0, 100);
encStream.Write(bin, 0, len);
rdlen = rdlen + len;
}
encStream.FlushFinalBlock();
fout.Position = 0L;
byte[] buffer = new byte[fout.Length];
fout.Read(buffer, 0, (int)fout.Length);
string hex_result = GetHexFromByte(buffer);//byte to hex string
return hex_result;
}
public string Decode(string str, string key, string iv)
{
byte[] strByte = GetHexArray(str);
byte[] keyByte = getKeyByteArray(key);
byte[] ivByte = getKeyByteArray(iv);
MemoryStream fin = new MemoryStream(strByte, false);
MemoryStream fout = new MemoryStream();
fout.SetLength(0L);
byte[] bin = new byte[100];
long rdlen = 0;
long totlen = fin.Length;
int len = 0;
TripleDESCryptoServiceProvider tdes = new TripleDESCryptoServiceProvider();
CryptoStream encStream = new CryptoStream(fout, tdes.CreateDecryptor(keyByte, ivByte), CryptoStreamMode.Write);
while (rdlen < totlen)
{
len = fin.Read(bin, 0, 100);
encStream.Write(bin, 0, len);
rdlen = rdlen + len;
}
encStream.FlushFinalBlock();
fout.Position = 0L;
byte[] buffer = new byte[fout.Length];
fout.Read(buffer, 0, (int)fout.Length);
string result = Encoding.UTF8.GetString(buffer);
return result;
}
public byte[] getKeyByteArray(string key)
{
if (key.Length > 0x18)
{
key = key.Substring(0, 0x18);
}
key = key.PadRight(0x18);
return Encoding.ASCII.GetBytes(key);
}
public string GetHexFromByte(byte[] bBytes)
{
StringBuilder builder = new StringBuilder(bBytes.Length);
for (int i = 0; i < bBytes.Length; i++)
{
if (bBytes[i].ToString("x").Length < 2)
{
builder.Append("0" + bBytes[i].ToString("x"));
}
else
{
builder.Append(bBytes[i].ToString("x"));
}
}
return builder.ToString();
}
public byte[] GetHexArray(string sOrg)
{
byte[] buffer = null;
int num;
if (sOrg.ToUpper().Replace(" ", "+").Replace("%2B", "+").IndexOf("+") > 0)
{
string[] strArray = sOrg.ToUpper().Split(new char[] { '+' });
buffer = new byte[strArray.Length];
for (num = 0; num < strArray.Length; num++)
{
buffer[num] = GetByteFromHex(strArray[num]);
}
return buffer;
}
int num2 = sOrg.Length / 2;
buffer = new byte[num2];
for (num = 0; num < num2; num++)
{
buffer[num] = GetByteFromHex(sOrg.Substring(num * 2, 2));
}
return buffer;
}
public byte GetByteFromHex(string sHex)
{
char[] chArray = sHex.ToUpper().ToCharArray();
byte num = 0;
for (int i = 0; i < chArray.Length; i++)
{
switch (i)
{
case 0:
{
if (chArray[i] < 'A')
{
break;
}
num = (byte)(num + ((byte)(((chArray[i] - 'A') + 10) * 0x10)));
continue;
}
case 1:
{
if (chArray[i] < 'A')
{
goto Label_0089;
}
num = (byte)(num + ((byte)((chArray[i] - 'A') + 10)));
continue;
}
default:
goto Label_00A3;
}
num = (byte)(num + ((byte)(int.Parse(chArray[i].ToString()) * 0x10)));
continue;
Label_0089:
num = (byte)(num + ((byte)int.Parse(chArray[i].ToString())));
continue;
Label_00A3:
num = 0;
}
return num;
}
}
출처 : 아마도 stackoverflow
'Web_Application > C#' 카테고리의 다른 글
텍스트 파일에서 정규표현식 문자열 추출 (0) | 2020.11.17 |
---|---|
ERROR: should be excluded because its source file '' is under Windows System File Protection. (When Building MSI) (0) | 2019.09.03 |
[c#][linq][winform] combobox query foreach get index, value (0) | 2019.04.23 |
[c#][winform][Linq] 여러개의 콤보박스 선택 유무 체크하기 (0) | 2019.04.18 |
[c#][winform] textbox 숫자만 입력 받기 (0) | 2019.04.18 |