本文整理汇总了C#中System.Security.Cryptography.TripleDESCryptoServiceProvider.Clear方法的典型用法代码示例。如果您正苦于以下问题:C# TripleDESCryptoServiceProvider.Clear方法的具体用法?C# TripleDESCryptoServiceProvider.Clear怎么用?C# TripleDESCryptoServiceProvider.Clear使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Security.Cryptography.TripleDESCryptoServiceProvider
的用法示例。
在下文中一共展示了TripleDESCryptoServiceProvider.Clear方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Decrypt
/// <summary>
/// Desencripta un texto pasandole la KEY a usar.
/// Se puede pasar la variable "useHashing" como true para usar un codigo hash en vez de la KEY.
/// </summary>
/// <param name="cipherString"></param>
/// <param name="Key"></param>
/// <param name="useHashing"></param>
/// <returns></returns>
public static string Decrypt(string cipherString, string Key, bool useHashing = false)
{
byte[] keyArray;
byte[] toEncryptArray;
TripleDESCryptoServiceProvider tdes = new TripleDESCryptoServiceProvider();
try
{
// Get the byte code of the string
toEncryptArray = Convert.FromBase64String(cipherString);
if (useHashing)
{
// If hashing was used get the hash code with regards to your key
MD5CryptoServiceProvider hashmd5 = new MD5CryptoServiceProvider();
keyArray = hashmd5.ComputeHash(UTF8Encoding.UTF8.GetBytes(Key));
// Release any resource held by the MD5CryptoServiceProvider
hashmd5.Clear();
}
else
{
// If hashing was not implemented get the byte code of the key
keyArray = UTF8Encoding.UTF8.GetBytes(Key);
}
// Set the secret key for the tripleDES algorithm
tdes.Key = keyArray;
// Mode of operation. there are other 4 modes (we choose ECB(Electronic code Book))
tdes.Mode = CipherMode.ECB;
// Padding mode(if any extra byte added)
tdes.Padding = PaddingMode.PKCS7;
ICryptoTransform cTransform = tdes.CreateDecryptor();
byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);
// Release resources held by TripleDes Encryptor
tdes.Clear();
// Return the Clear decrypted TEXT
return UTF8Encoding.UTF8.GetString(resultArray);
}
catch
{
// Release resources held by TripleDes Encryptor
tdes.Clear();
return "-1";
}
}
示例2: Encrypt
/// <summary>
/// Encripta un texto pasandole la KEY a usar.
/// Se puede pasar la variable "useHashing" como true para usar un codigo hash en vez de la KEY.
/// </summary>
/// <param name="toEncrypt"></param>
/// <param name="Key"></param>
/// <param name="useHashing"></param>
/// <returns></returns>
public static string Encrypt(string toEncrypt, string Key, bool useHashing = false)
{
byte[] keyArray;
byte[] toEncryptArray = UTF8Encoding.UTF8.GetBytes(toEncrypt);
TripleDESCryptoServiceProvider tdes = new TripleDESCryptoServiceProvider();
try
{
// If hashing use get hashcode regards to your key
if (useHashing)
{
MD5CryptoServiceProvider hashmd5 = new MD5CryptoServiceProvider();
keyArray = hashmd5.ComputeHash(UTF8Encoding.UTF8.GetBytes(Key));
// Always release the resources and flush data of the Cryptographic service provide. Best Practice
hashmd5.Clear();
}
else
{
keyArray = UTF8Encoding.UTF8.GetBytes(Key);
}
// Set the secret key for the tripleDES algorithm
tdes.Key = keyArray;
// Mode of operation. there are other 4 modes (we choose ECB(Electronic code Book))
tdes.Mode = CipherMode.ECB;
// Padding mode(if any extra byte added)
tdes.Padding = PaddingMode.PKCS7;
ICryptoTransform cTransform = tdes.CreateEncryptor();
// Transform the specified region of bytes array to resultArray
byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);
// Release resources held by TripleDes Encryptor
tdes.Clear();
// Return the encrypted data into unreadable string format
return Convert.ToBase64String(resultArray, 0, resultArray.Length);
}
catch
{
// Release resources held by TripleDes Encryptor
tdes.Clear();
return "-1";
}
}
示例3: Encrypt
public static string Encrypt(this string plainText, string encryptionKey)
{
var toEncryptArray = Encoding.ASCII.GetBytes(plainText);
byte[] encryptionKeyArray;
// get MD5 has for the key
using (var hashmd5 = new MD5CryptoServiceProvider())
{
encryptionKeyArray = hashmd5.ComputeHash(Encoding.ASCII.GetBytes(encryptionKey));
hashmd5.Clear();
}
byte[] resultArray;
// Set the secret key for the tripleDES algorithm
using (
var tdes = new TripleDESCryptoServiceProvider
{
Key = encryptionKeyArray,
Mode = CipherMode.ECB,
Padding = PaddingMode.PKCS7
})
{
// Transform the specified region of bytes array to resultArray
var cTransform = tdes.CreateEncryptor();
resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);
tdes.Clear();
}
// Return the encrypted data into unreadable string format
return Convert.ToBase64String(resultArray);
}
示例4: DecryptString
public static string DecryptString(string message)
{
byte[] results;
// Step 1. We hash the customPassPhrase using MD5
// We use the MD5 hash generator as the result is a 128 bit byte array
// which is a valid length for the TripleDES encoder we use below
var hashProvider = new MD5CryptoServiceProvider();
byte[] TDESKey = hashProvider.ComputeHash(Encoding.UTF8.GetBytes(customPassPhrase));
// Step 3. Setup the decoder
var tripleDESAlgorithm = new TripleDESCryptoServiceProvider() { Mode = CipherMode.ECB, Padding = PaddingMode.PKCS7 };
tripleDESAlgorithm.Key = TDESKey;
// Step 4. Convert the input string to a byte[]
byte[] dataToDecrypt = Convert.FromBase64String(message);
// Step 5. Attempt to decrypt the string
try
{
ICryptoTransform decryptor = tripleDESAlgorithm.CreateDecryptor();
results = decryptor.TransformFinalBlock(dataToDecrypt, 0, dataToDecrypt.Length);
decryptor.Dispose();
}
finally
{
// Clear the TripleDes and Hashprovider services of any sensitive information
tripleDESAlgorithm.Clear();
hashProvider.Clear();
}
// Step 6. Return the decrypted string in UTF8 format
return Encoding.UTF8.GetString(results);
}
示例5: Decrypt
/// <summary>
/// DeCrypt a string using dual encryption method. Return a DeCrypted clear string
/// </summary>
/// <param name="cipherString">encrypted string</param>
/// <param name="useHashing">Did you use hashing to encrypt this data? pass true is yes</param>
/// <returns></returns>
public static string Decrypt(string cipherString, bool useHashing)
{
byte[] keyArray;
byte[] toEncryptArray = Convert.FromBase64String(cipherString);
System.Configuration.AppSettingsReader settingsReader = new AppSettingsReader();
//Get your key from config file to open the lock!
string key = SecurityKey;
if (useHashing)
{
MD5CryptoServiceProvider hashmd5 = new MD5CryptoServiceProvider();
keyArray = hashmd5.ComputeHash(UTF8Encoding.UTF8.GetBytes(key));
hashmd5.Clear();
}
else
keyArray = UTF8Encoding.UTF8.GetBytes(key);
TripleDESCryptoServiceProvider tdes = new TripleDESCryptoServiceProvider();
tdes.Key = keyArray;
tdes.Mode = CipherMode.ECB;
tdes.Padding = PaddingMode.PKCS7;
ICryptoTransform cTransform = tdes.CreateDecryptor();
byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);
tdes.Clear();
return UTF8Encoding.UTF8.GetString(resultArray);
}
示例6: maHoa
public static string maHoa(string strMaHoa)
{
byte[] arrKey;
byte[] arrMahoa = UTF8Encoding.UTF8.GetBytes(strMaHoa);
System.Configuration.AppSettingsReader settingsReader = new AppSettingsReader();
//Lấy giá trị key từ file Web.config
string keyConfig = (string)settingsReader.GetValue("keyMD5", typeof(String));
MD5CryptoServiceProvider serviceMD5 = new MD5CryptoServiceProvider();
arrKey = serviceMD5.ComputeHash(UTF8Encoding.UTF8.GetBytes(keyConfig));
serviceMD5.Clear();//Giải phóng tài nguyên
TripleDESCryptoServiceProvider tdsp = new TripleDESCryptoServiceProvider();
//Thiết lập 1 key cho thuật toán mã hóa
tdsp.Key = arrKey;
//Chọn phương thức mã hóa. Ở đây chúng ta chọn ECB(Electronic code Book ) [1]
tdsp.Mode = CipherMode.ECB;
//Thêm phương thức mã hóa IS010126 (random chuỗi kết quả) [2]
tdsp.Padding = PaddingMode.ISO10126;
ICryptoTransform cTransform = tdsp.CreateEncryptor();
byte[] arrKQ = cTransform.TransformFinalBlock(arrMahoa, 0, arrMahoa.Length);
tdsp.Clear(); //Giải phóng tài nguyên của TripleDES Encryptor
//Kết quả mã hóa được trả về 1 giá trị kiểu String
return Convert.ToBase64String(arrKQ, 0, arrKQ.Length);
}
示例7: Decrypt
//Giai ma HashMD5 và TripleDES
public string Decrypt(string cipherString, bool i_blUseHashing)
{
byte[] v_arrKey;
byte[] v_arrEncrypt = new byte[(int)cipherString.Length];
try
{
v_arrEncrypt = Convert.FromBase64String(cipherString);
}
catch (Exception v_ex)
{
throw v_ex;
}
string v_strKey = "fatherofbill";
if (i_blUseHashing)
{
MD5CryptoServiceProvider hashmd5 = new MD5CryptoServiceProvider();
v_arrKey = hashmd5.ComputeHash(UTF8Encoding.UTF8.GetBytes(v_strKey));
hashmd5.Clear();
}
else
{
v_arrKey = UTF8Encoding.UTF8.GetBytes(v_strKey);
}
TripleDESCryptoServiceProvider tdes = new TripleDESCryptoServiceProvider();
tdes.Key = v_arrKey;
tdes.Mode = CipherMode.ECB;
tdes.Padding = PaddingMode.PKCS7;
ICryptoTransform cTransform = tdes.CreateDecryptor();
byte[] resultArray = cTransform.TransformFinalBlock(v_arrEncrypt, 0, v_arrEncrypt.Length);
tdes.Clear();
return UTF8Encoding.UTF8.GetString(resultArray);
}
示例8: Decrypt
/// <summary>
/// Returns a decrypted string by a key
/// </summary>
/// <remarks>
/// If something is broken this method returns an empty string
/// </remarks>
/// <param name="text">Text to decrypt</param>
/// <param name="key">Key to decrypt</param>
/// <param name="encoding" >Encoding to get bytes. UTF8 by default.</ param >
/// <returns></returns>
public static String Decrypt(String textoEncriptado, String clave, Encoding encoding = null)
{
try
{
if (String.IsNullOrEmpty(textoEncriptado) || String.IsNullOrEmpty(clave))
return String.Empty;
byte[] keyBytes;
byte[] encryptedBytes = Convert.FromBase64String(textoEncriptado);
//Create a MD5 object to obtain a hash
MD5CryptoServiceProvider hashmd5 = new MD5CryptoServiceProvider();
keyBytes = hashmd5.ComputeHash(encoding.GetBytes(clave));
hashmd5.Clear();
//Create a Triple DES object to decrypt
TripleDESCryptoServiceProvider tdes = new TripleDESCryptoServiceProvider();
tdes.Key = keyBytes;
tdes.Mode = CipherMode.ECB;
tdes.Padding = PaddingMode.PKCS7;
ICryptoTransform cTransform = tdes.CreateDecryptor();
byte[] resultArray = cTransform.TransformFinalBlock(encryptedBytes, 0, encryptedBytes.Length);
tdes.Clear();
return encoding.GetString(resultArray);
}
catch (Exception)
{
return String.Empty;
}
}
示例9: Decrypt
public static string Decrypt(string cypherString)
{
byte[] keyArray;
byte[] toDecryptArray = StringToByteArray(cypherString);
MD5CryptoServiceProvider hashmd = new MD5CryptoServiceProvider();
keyArray = hashmd.ComputeHash(UTF8Encoding.UTF8.GetBytes(ENCRYPTION_KEY));
hashmd.Clear();
TripleDESCryptoServiceProvider tDes = new TripleDESCryptoServiceProvider();
tDes.Key = keyArray;
tDes.Mode = CipherMode.ECB;
tDes.Padding = PaddingMode.PKCS7;
ICryptoTransform cTransform = tDes.CreateDecryptor();
try
{
byte[] resultArray = cTransform.TransformFinalBlock(toDecryptArray, 0, toDecryptArray.Length);
tDes.Clear();
return UTF8Encoding.UTF8.GetString(resultArray, 0, resultArray.Length);
}
catch (Exception ex)
{
throw ex;
}
}
示例10: Decrypt
public static string Decrypt(string cipherString)
{
try
{
byte[] keyArray;
//get the byte code of the string
byte[] toEncryptArray = Convert.FromBase64String(cipherString);
MD5CryptoServiceProvider hashmd5 = new MD5CryptoServiceProvider();
keyArray = hashmd5.ComputeHash(UTF8Encoding.UTF8.GetBytes(Key));
hashmd5.Clear();
TripleDESCryptoServiceProvider tdes = new TripleDESCryptoServiceProvider();
tdes.Key = keyArray;
tdes.Mode = CipherMode.ECB;
tdes.Padding = PaddingMode.PKCS7;
ICryptoTransform cTransform = tdes.CreateDecryptor();
byte[] resultArray = cTransform.TransformFinalBlock(
toEncryptArray, 0, toEncryptArray.Length);
tdes.Clear();
return UTF8Encoding.UTF8.GetString(resultArray);
}
catch (Exception ex)
{
return string.Empty;
}
}
示例11: Encrypt
public string Encrypt(string message)
{
byte[] results;
UTF8Encoding utf8 = new UTF8Encoding();
//to create the object for UTF8Encoding class
//TO create the object for MD5CryptoServiceProvider
MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();
byte[] deskey = md5.ComputeHash(utf8.GetBytes(Passphrase));
//to convert to binary passkey
//TO create the object for TripleDESCryptoServiceProvider
TripleDESCryptoServiceProvider desalg = new TripleDESCryptoServiceProvider();
desalg.Key = deskey;//to pass encode key
desalg.Mode = CipherMode.ECB;
desalg.Padding = PaddingMode.PKCS7;
byte[] encryptData = utf8.GetBytes(message);
//to convert the string to utf encoding binary
try
{
//To transform the utf binary code to md5 encrypt
ICryptoTransform encryptor = desalg.CreateEncryptor();
results = encryptor.TransformFinalBlock(encryptData, 0, encryptData.Length);
}
finally
{
//to clear the allocated memory
desalg.Clear();
md5.Clear();
}
//to convert to 64 bit string from converted md5 algorithm binary code
return Convert.ToBase64String(results);
}
示例12: Decrypt
public string Decrypt(string message)
{
byte[] results;
UTF8Encoding utf8 = new UTF8Encoding();
MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();
byte[] deskey = md5.ComputeHash(utf8.GetBytes(Passphrase));
TripleDESCryptoServiceProvider desalg = new TripleDESCryptoServiceProvider();
desalg.Key = deskey;
desalg.Mode = CipherMode.ECB;
desalg.Padding = PaddingMode.PKCS7;
byte[] decryptData = Convert.FromBase64String(message);
try
{
//To transform the utf binary code to md5 decrypt
ICryptoTransform decryptor = desalg.CreateDecryptor();
results = decryptor.TransformFinalBlock(decryptData, 0, decryptData.Length);
}
finally
{
desalg.Clear();
md5.Clear();
}
//TO convert decrypted binery code to string
return utf8.GetString(results);
}
示例13: Encrypt
public string Encrypt(string text)
{
try
{
byte[] getBytes = UTF8Encoding.UTF8.GetBytes(text); //getting the string to encrypt
System.Security.Cryptography.MD5CryptoServiceProvider cryptObj = new System.Security.Cryptography.MD5CryptoServiceProvider();
byte[] computeHashArray = cryptObj.ComputeHash(UTF8Encoding.UTF8.GetBytes(secureKey));
cryptObj.Clear(); //Releasing the object
TripleDESCryptoServiceProvider tripleDESC = new TripleDESCryptoServiceProvider(); //creating an object of TRIPLE DES service provider
tripleDESC.Key = computeHashArray; //securekey is being set
tripleDESC.Mode = CipherMode.ECB; //Electronic Code Book mode is being set
tripleDESC.Padding = PaddingMode.PKCS7; //Padding
ICryptoTransform ICT = tripleDESC.CreateEncryptor();
byte[] transform = ICT.TransformFinalBlock(getBytes, 0,getBytes.Length); //Byte Array to Transform array
tripleDESC.Clear(); //release object
return Convert.ToBase64String(transform, 0, transform.Length); //return encrypted string
}
catch (Exception ex)
{
return ex.ToString();
}
}
示例14: Decrypt
private const string secureKey = "QaZ1wSx2Edc3RfV4"; //Important Security Key
#endregion Fields
#region Methods
public string Decrypt(string encryptedString)
{
try
{
byte[] extractBytes = Convert.FromBase64String(encryptedString); //getting the string to decrypt
MD5CryptoServiceProvider descObj = new MD5CryptoServiceProvider(); //creating an instance of MD5CryptoServiceProvider
byte[] hashCompute = descObj.ComputeHash(UTF8Encoding.UTF8.GetBytes(secureKey));
descObj.Clear(); //releasing resourse
TripleDESCryptoServiceProvider DESCD = new TripleDESCryptoServiceProvider();
DESCD.Key = hashCompute; //secure Key is being set
DESCD.Mode = CipherMode.ECB; //Electronic Code Book Mode
DESCD.Padding = PaddingMode.PKCS7; //Padding
ICryptoTransform ICTransform = DESCD.CreateDecryptor();
byte[] finalResult = ICTransform.TransformFinalBlock( //Transforming byte array to finalResult
extractBytes, 0, extractBytes.Length);
DESCD.Clear();
return UTF8Encoding.UTF8.GetString(finalResult); //Return Decrypted Text
}
catch (Exception ex)
{
return ex.ToString();
}
}
示例15: Encrypt
public static string Encrypt(string text, bool useHash)
{
byte[] keyArray;
byte[] toEcryptArray = UTF8Encoding.UTF8.GetBytes(text);
System.Configuration.AppSettingsReader setingReader = new System.Configuration.AppSettingsReader();
string key = (string)setingReader.GetValue("SecurityKey", typeof(string));
if (useHash)
{
MD5CryptoServiceProvider hashMd5 = new MD5CryptoServiceProvider();
keyArray = hashMd5.ComputeHash(UTF8Encoding.UTF8.GetBytes(key));
hashMd5.Clear();
}
else
{
keyArray = UTF8Encoding.UTF8.GetBytes(key);
}
TripleDESCryptoServiceProvider crypto = new TripleDESCryptoServiceProvider();
crypto.Key = keyArray;
crypto.Mode = CipherMode.ECB;
crypto.Padding = PaddingMode.PKCS7;
ICryptoTransform transforme = crypto.CreateEncryptor();
byte[] result = transforme.TransformFinalBlock(toEcryptArray, 0, toEcryptArray.Length);
crypto.Clear();
return Convert.ToBase64String(result, 0, result.Length);
}