3DES encryption and decryption,
C # 3DES encryption and decryption, available for JAVA and PHP
Using System;
Using System.Security.Cryptography;
Using System.Text;
Namespace TT.Utilities.Encrypt
{
Public class DES3
{
/// <summary>
/// utf-8 encoding
/// Encryption mode ECB, padding type PKCS7
/// </summary>
/// <param name="str_content"></param>
/// <param name="str_keys">24-bit key</param>
/// <returns></returns>
Public static string DES3_Encrypt(string str_content, string str_keys)
#region
{
Encoding encoding = Encoding.UTF8;
Byte[] content = encoding.GetBytes(str_content);
Byte[] keys = encoding.GetBytes(str_keys);
TripleDESCryptoServiceProvider tdsc = new TripleDESCryptoServiceProvider();
/ / specify the key length, the default is 192
tdsc.KeySize = 128;
/ / Use the specified key and IV (encryption vector)
tdsc.Key = keys;
//tdsc.IV = IV;
//encryption mode, offset
tdsc.Mode = CipherMode.ECB;
tdsc.Padding = PaddingMode.PKCS7;
/ / Perform encryption conversion operation
ICryptoTransform ct = tdsc.CreateEncryptor();
//8 is critical, the result of the encryption is an 8-byte array
Byte[] results = ct.TransformFinalBlock(content, 0, content.Length);
String base64String = Convert.ToBase64String(results);
Return base64String;
}
#endregion
/// <summary>
/// utf-8 encoding
/// Encryption mode ECB, padding type PKCS7
/// </summary>
/// <param name="base64_content"></param>
/// <param name="str_keys">24-bit key</param>
/// <returns></returns>
Public static string DES3_Decrypt(string base64_content, string str_keys)
#region
{
Encoding encoding = Encoding.UTF8;
Byte[] content = Convert.FromBase64String(base64_content);
Byte[] keys = encoding.GetBytes(str_keys);
TripleDESCryptoServiceProvider tdsc = new TripleDESCryptoServiceProvider();
/ / specify the key length, the default is 192
tdsc.KeySize = 128;
/ / Use the specified key and IV (encryption vector)
tdsc.Key = keys;
//tdsc.IV = IV;
//encryption mode, offset
tdsc.Mode = CipherMode.ECB;
tdsc.Padding = PaddingMode.PKCS7;
/ / Perform encryption conversion operation
ICryptoTransform ct = tdsc.CreateDecryptor();
//8 is critical, the result of the encryption is an 8-byte array
Byte[] results = ct.TransformFinalBlock(content, 0, content.Length);
String oriString = encoding.GetString(results);
Return oriString;
}
#endregion
}
}