Step1:Encode
public string EnCryptMD5(string strEnCrypt, string key)
{
try
{
byte[] keyArr;
byte[] EnCryptArr = UTF8Encoding.UTF8.GetBytes(strEnCrypt);
MD5CryptoServiceProvider MD5Hash = new MD5CryptoServiceProvider();
keyArr = MD5Hash.ComputeHash(UTF8Encoding.UTF8.GetBytes(key));
TripleDESCryptoServiceProvider tripDes = new TripleDESCryptoServiceProvider();
tripDes.Key = keyArr;
tripDes.Mode = CipherMode.ECB;
tripDes.Padding = PaddingMode.PKCS7;
ICryptoTransform transform = tripDes.CreateEncryptor();
byte[] arrResult = transform.TransformFinalBlock(EnCryptArr, 0, EnCryptArr.Length);
return Convert.ToBase64String(arrResult, 0, arrResult.Length);
}
catch (Exception ex) { }
return "";
}
Step2:Decode
public string DeCryptMD5(string strDecypt, string key)
{
try
{
byte[] keyArr;
byte[] DeCryptArr = Convert.FromBase64String(strDecypt);
MD5CryptoServiceProvider MD5Hash = new MD5CryptoServiceProvider();
keyArr = MD5Hash.ComputeHash(UTF8Encoding.UTF8.GetBytes(key));
TripleDESCryptoServiceProvider tripDes = new TripleDESCryptoServiceProvider();
tripDes.Key = keyArr;
tripDes.Mode = CipherMode.ECB;
tripDes.Padding = PaddingMode.PKCS7;
ICryptoTransform transform = tripDes.CreateDecryptor();
byte[] arrResult = transform.TransformFinalBlock(DeCryptArr, 0, DeCryptArr.Length);
return UTF8Encoding.UTF8.GetString(arrResult);
}
catch (Exception ex) { }
return "";
}
Dạng 2 Không cần Authentication
fig 1:String to Base 64
public string EncodeStringToBase64(string src)
{
byte[] b = Encoding.Unicode.GetBytes(src);
return Convert.ToBase64String(b);
}
public string DecodeBase64ToString(string src)
{
byte[] b = Convert.FromBase64String(src);
return Encoding.Unicode.GetString(b);
}
{
byte[] b = Encoding.Unicode.GetBytes(src);
return Convert.ToBase64String(b);
}
public string DecodeBase64ToString(string src)
{
byte[] b = Convert.FromBase64String(src);
return Encoding.Unicode.GetString(b);
}
fig 2:Codec UTF8
public string EncodeTo64UTF8(string m_enc)
{
byte[] toEncodeAsBytes =
System.Text.Encoding.UTF8.GetBytes(m_enc);
string returnValue =
System.Convert.ToBase64String(toEncodeAsBytes);
return returnValue;
}
public string DecodeFrom64(string m_enc)
{
byte[] encodedDataAsBytes =
System.Convert.FromBase64String(m_enc);
string returnValue =
System.Text.Encoding.UTF8.GetString(encodedDataAsBytes);
return returnValue;
}
{
byte[] toEncodeAsBytes =
System.Text.Encoding.UTF8.GetBytes(m_enc);
string returnValue =
System.Convert.ToBase64String(toEncodeAsBytes);
return returnValue;
}
public string DecodeFrom64(string m_enc)
{
byte[] encodedDataAsBytes =
System.Convert.FromBase64String(m_enc);
string returnValue =
System.Text.Encoding.UTF8.GetString(encodedDataAsBytes);
return returnValue;
}
fig 3:Codec UTF32
public string EncodeToUTF32(string m_enc)
{
byte[] toEncodeAsBytes =
System.Text.Encoding.UTF32.GetBytes(m_enc);
string returnValue =
System.Convert.ToBase64String(toEncodeAsBytes);
return returnValue;
}
public string DecodeFromUTF32(string m_enc)
{
byte[] encodedDataAsBytes =
System.Convert.FromBase64String(m_enc);
string returnValue =
System.Text.Encoding.UTF32.GetString(encodedDataAsBytes);
return returnValue;
}
{
byte[] toEncodeAsBytes =
System.Text.Encoding.UTF32.GetBytes(m_enc);
string returnValue =
System.Convert.ToBase64String(toEncodeAsBytes);
return returnValue;
}
public string DecodeFromUTF32(string m_enc)
{
byte[] encodedDataAsBytes =
System.Convert.FromBase64String(m_enc);
string returnValue =
System.Text.Encoding.UTF32.GetString(encodedDataAsBytes);
return returnValue;
}
fig 4:Int to base64
public static string IntToBase64(int src)
{
byte[] b = BitConverter.GetBytes(src);
return Convert.ToBase64String(b);
}
public static int Base64ToInt(string src)
{
byte[] b = Convert.FromBase64String(src);
return BitConverter.ToInt32(b, 0);
}
{
byte[] b = BitConverter.GetBytes(src);
return Convert.ToBase64String(b);
}
public static int Base64ToInt(string src)
{
byte[] b = Convert.FromBase64String(src);
return BitConverter.ToInt32(b, 0);
}
fig 5:Using RSA key
Các bạn có thể phối hợp các dạng với nhau để có được hiệu quả tốt nhất
System
.Security.Cryptography.RSACryptoServiceProvider Provider = new System.Security.Cryptography.RSACryptoServiceProvider();Provider
.ImportParameters(your_rsa_key);byte
[] encrypted = Provider.Encrypt(System.Text.Encoding.UTF8.GetBytes("Hello World!"), true);string
decrypted =System.Text.Encoding.UTF8.GetString(Provider.Decrypt(encrypted, true));Các bạn có thể phối hợp các dạng với nhau để có được hiệu quả tốt nhất
Không có nhận xét nào:
Đăng nhận xét