TFS_ANTIGUO/SIGPC/wcfSigdd/Helper/Encrytp.cs

79 lines
2.7 KiB
C#
Raw Normal View History

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;
using System.Security.Cryptography;
using System.IO;
using System.ComponentModel;
using Microsoft.VisualBasic;
namespace Helper
{
public class Encrytp
{
public String GBPassword = ConfigurationManager.AppSettings["GBPassword"].ToString();
public String LOGeneraKey = ConfigurationManager.AppSettings["LOGeneraKey"].ToString();
/// <summary>
///
/// </summary>
/// <param name="InputString"></param>
/// <param name="SecretKey"></param>
/// <param name="CyphMode"></param>
/// <returns></returns>
public String DecryptString(String InputString, CipherMode CyphMode = CipherMode.ECB)
{
String SecretKey = LOGeneraKey;
if (InputString == string.Empty)
{
return "";
}
else
{
TripleDESCryptoServiceProvider Des = new TripleDESCryptoServiceProvider();
byte[] InputbyteArray = new byte[Convert.ToInt32(InputString.Length / 2 - 1) + 1];
MD5CryptoServiceProvider hashMD5 = new MD5CryptoServiceProvider();
Des.Key = hashMD5.ComputeHash(ASCIIEncoding.ASCII.GetBytes(SecretKey));
Des.Mode = CyphMode;
//Put the input string into the byte array
int X = 0;
for (X = 0; X <= InputbyteArray.Length - 1; X++)
{
Int32 IJ = (Convert.ToInt32(InputString.Substring(X * 2, 2), 16));
ByteConverter BT = new ByteConverter();
InputbyteArray[X] = new byte();
InputbyteArray[X] = Convert.ToByte(BT.ConvertTo(IJ, typeof(byte)));
}
MemoryStream ms = new MemoryStream();
CryptoStream cs = new CryptoStream(ms, Des.CreateDecryptor(), CryptoStreamMode.Write);
//Flush the data through the crypto stream into the memory stream
cs.Write(InputbyteArray, 0, InputbyteArray.Length);
cs.FlushFinalBlock();
////Get the decrypted data back from the memory stream
StringBuilder ret = new StringBuilder();
byte[] B = ms.ToArray();
ms.Close();
int I = 0;
for (I = 0; I <= Information.UBound(B); I++)
{
ret.Append(Strings.Chr(B[I]));
}
return ret.ToString();
}
}
}
}