本文整理汇总了C#中System.Security.Cryptography.TripleDESCryptoServiceProvider类的典型用法代码示例。如果您正苦于以下问题:C# TripleDESCryptoServiceProvider类的具体用法?C# TripleDESCryptoServiceProvider怎么用?C# TripleDESCryptoServiceProvider使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
TripleDESCryptoServiceProvider类属于System.Security.Cryptography命名空间,在下文中一共展示了TripleDESCryptoServiceProvider类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Decodificar
public static string Decodificar(string entrada)
{
TripleDESCryptoServiceProvider tripledescryptoserviceprovider = new TripleDESCryptoServiceProvider();
MD5CryptoServiceProvider md5cryptoserviceprovider = new MD5CryptoServiceProvider();
try
{
if (entrada.Trim() != "")
{
string chave = "asdfg";
tripledescryptoserviceprovider.Key = md5cryptoserviceprovider.ComputeHash(Encoding.Default.GetBytes(chave));
tripledescryptoserviceprovider.Mode = CipherMode.ECB;
ICryptoTransform desdencrypt = tripledescryptoserviceprovider.CreateDecryptor();
byte[] buff = Convert.FromBase64String(entrada);
return Encoding.Default.GetString(desdencrypt.TransformFinalBlock(buff, 0, buff.Length));
}
else
{
return "";
}
}
catch (Exception exception)
{
MessageBox.Show("Erro" + exception.Message);
throw exception;
}
finally
{
tripledescryptoserviceprovider = null;
md5cryptoserviceprovider = null;
}
}
示例2: Encrypt
/// <summary>
///
/// </summary>
/// <param name="strToEncrypt"></param>
/// <param name="key"></param>
/// <returns></returns>
/// <exception cref="Exception"></exception>
public static string Encrypt(string strToEncrypt, string key)
{
try
{
TripleDESCryptoServiceProvider crypto = new TripleDESCryptoServiceProvider();
MD5CryptoServiceProvider hash = new MD5CryptoServiceProvider();
byte[] byteHash, byteBuff;
string strTempKey = key;
byteHash = hash.ComputeHash(ASCIIEncoding.ASCII.GetBytes(strTempKey));
hash = null;
crypto.Key = byteHash;
crypto.Mode = CipherMode.ECB; //CBC, CFB
byteBuff = ASCIIEncoding.ASCII.GetBytes(strToEncrypt);
return Convert.ToBase64String(crypto.CreateEncryptor().TransformFinalBlock(byteBuff, 0, byteBuff.Length));
}
catch (Exception ex)
{
throw new Exception("Error: " + ex.Message + ".\n Encryption Failed. Please try again.");
}
}
示例3: Encrypt
public static string Encrypt(String plainText )
{
string encrypted = null;
try
{
byte[] inputBytes = ASCIIEncoding.ASCII.GetBytes(plainText);
byte[] pwdhash = null;
MD5CryptoServiceProvider hashmd5;
//generate an MD5 hash from the password.
//a hash is a one way encryption meaning once you generate
//the hash, you cant derive the password back from it.
hashmd5 = new MD5CryptoServiceProvider();
pwdhash = hashmd5.ComputeHash(ASCIIEncoding.ASCII.GetBytes(key));
hashmd5 = null;
// Create a new TripleDES service provider
TripleDESCryptoServiceProvider tdesProvider = new TripleDESCryptoServiceProvider();
tdesProvider.Key = pwdhash;
tdesProvider.Mode = CipherMode.ECB;
encrypted = Convert.ToBase64String(
tdesProvider.CreateEncryptor().TransformFinalBlock(inputBytes, 0, inputBytes.Length));
}
catch(Exception e)
{
string str = e.Message;
throw ;
}
return encrypted;
}
示例4: Encrypt
/// <summary>
/// Encrypt a string using dual encryption method. Return a encrypted cipher Text
/// </summary>
/// <param name="toEncrypt">string to be encrypted</param>
/// <param name="useHashing">use hashing? send to for extra secirity</param>
/// <returns></returns>
public static string Encrypt(string toEncrypt, bool useHashing)
{
byte[] keyArray;
byte[] toEncryptArray = UTF8Encoding.UTF8.GetBytes(toEncrypt);
System.Configuration.AppSettingsReader settingsReader = new AppSettingsReader();
// Get the key from config file
string key = SecurityKey;
//System.Windows.Forms.MessageBox.Show(key);
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.CreateEncryptor();
byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);
tdes.Clear();
return Convert.ToBase64String(resultArray, 0, resultArray.Length);
}
示例5: EncryptString
public static string EncryptString(string Message)
{
byte[] results;
var hashProvider = new MD5CryptoServiceProvider();
var tripleDESAlgorithm = new TripleDESCryptoServiceProvider() { Mode = CipherMode.ECB, Padding = PaddingMode.PKCS7 };
tripleDESAlgorithm.Key = hashProvider.ComputeHash(Encoding.UTF8.GetBytes(customPassPhrase));
byte[] dataToEncrypt = Encoding.UTF8.GetBytes(Message);
try
{
ICryptoTransform encryptor = tripleDESAlgorithm.CreateEncryptor();
results = encryptor.TransformFinalBlock(dataToEncrypt, 0, dataToEncrypt.Length);
encryptor.Dispose();
}
finally
{
// Clear the TripleDes and Hashprovider services of any sensitive information
tripleDESAlgorithm.Clear();
hashProvider.Clear();
}
return Convert.ToBase64String(results);
}
示例6: GetCriptografiaSimetrica
/// <summary>
/// A chave deve possuir 16 com caracteres "1234567890123456"
/// </summary>
/// <param name="str"></param>
/// <param name="key"></param>
/// <returns></returns>
public static string GetCriptografiaSimetrica(this string str, string key)
{
using (TripleDESCryptoServiceProvider provider = new TripleDESCryptoServiceProvider())
{
provider.Mode = CipherMode.CFB;
provider.Padding = PaddingMode.PKCS7;
MemoryStream mStream = new MemoryStream();
CryptoStream cs = new CryptoStream(mStream, provider.CreateEncryptor(Encoding.UTF8.GetBytes(key), new byte[] { 138, 154, 251, 188, 64, 108, 167, 121 }), CryptoStreamMode.Write);
byte[] toEncrypt = new UTF8Encoding().GetBytes(str);
cs.Write(toEncrypt, 0, toEncrypt.Length);
cs.FlushFinalBlock();
byte[] ret = mStream.ToArray();
mStream.Close();
cs.Close();
str = Convert.ToBase64String(ret);
}
return str;
}
示例7: Decrypt
public static string Decrypt(string cypherString, bool useHasing)
{
byte[] keyArray;
byte[] toDecryptArray = Convert.FromBase64String(cypherString);
string key = "uzma";
if (useHasing)
{
MD5CryptoServiceProvider hashmd = new MD5CryptoServiceProvider();
keyArray = hashmd.ComputeHash(UTF8Encoding.UTF8.GetBytes(key));
hashmd.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();
try
{
byte[] resultArray = cTransform.TransformFinalBlock(toDecryptArray, 0, toDecryptArray.Length);
tDes.Clear();
return UTF8Encoding.UTF8.GetString(resultArray, 0, resultArray.Length);
}
catch (Exception ex)
{
throw ex;
}
}
示例8: EncryptString
public static string EncryptString(string needToEncrypt)
{
string encryptedstring;
try
{
// Create a new instance of the TripleDESCryptoServiceProvider
// class. This generates a new key and initialization
// vector (IV).
using (TripleDESCryptoServiceProvider myTripleDES = new TripleDESCryptoServiceProvider())
{
myTripleDES.Key = StringToByteArray(HttpContext.GetGlobalResourceObject("Global", "Key").ToString());
myTripleDES.IV = StringToByteArray(HttpContext.GetGlobalResourceObject("Global", "IV").ToString());
// Encrypt the string to an array of bytes.
byte[] encrypted = EncryptStringToBytes(needToEncrypt, myTripleDES.Key, myTripleDES.IV);
encryptedstring = ByteArrayToString(encrypted);
// Decrypt the bytes to a string.
string roundtrip2 = DecryptStringFromBytes(StringToByteArray(encryptedstring), myTripleDES.Key, myTripleDES.IV);
}
return encryptedstring;
}
catch (Exception ex)
{
return null;
}
}
示例9: DecryptKey
public static AptimaLicenseInfo DecryptKey(string licenseKey, string securityKey, int inputStringLength)
{
////Decrypt
byte[] keyArray;
byte[] toEncryptArray = Convert.FromBase64String(licenseKey);
keyArray = UTF8Encoding.UTF8.GetBytes(securityKey);
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();
string outputString = UTF8Encoding.UTF8.GetString(resultArray);
if (outputString.Length != inputStringLength)
{//invalid output string!
return new AptimaLicenseInfo("Invalid license key! (Return string has incorrect length)");
}
////populate
return PopulateLicenseInfo(outputString);
}
示例10: TDesTest_EncryptUpdate
public MFTestResults TDesTest_EncryptUpdate()
{
MFTestResults res;
try
{
using (TripleDESCryptoServiceProvider csp = new TripleDESCryptoServiceProvider())
{
res = SymmetricTestHelper.Test_EncryptUpdate(csp);
}
if (res == MFTestResults.Pass && m_isEmulator)
{
using (TripleDESCryptoServiceProvider csp = new TripleDESCryptoServiceProvider("Emulator_Crypto"))
{
res = SymmetricTestHelper.Test_EncryptUpdate(csp);
}
}
}
catch
{
return MFTestResults.Fail;
}
return res;
}
示例11: 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;
}
}
示例12: ReadFile
/// <summary>
/// Ghi license ra file
/// </summary>
/// <param name="FilePath"></param>
/// <returns></returns>
public static string ReadFile(string FilePath)
{
try
{
FileInfo fi = new FileInfo(FilePath);
if (fi.Exists == false)
return string.Empty;
FileStream fin = new FileStream(FilePath, FileMode.Open, FileAccess.Read);
TripleDES tdes = new TripleDESCryptoServiceProvider();
CryptoStream cs = new CryptoStream(fin, tdes.CreateDecryptor(key, iv), CryptoStreamMode.Read);
StringBuilder SB = new StringBuilder();
int ch;
for (int i = 0; i < fin.Length; i++)
{
ch = cs.ReadByte();
if (ch == 0)
break;
SB.Append(Convert.ToChar(ch));
}
cs.Close();
fin.Close();
return SB.ToString();
}
catch(Exception ex)
{
return "";
}
}
示例13: Decrypt
public static string Decrypt(string cipherText, string passPhrase)
{
try
{
TripleDESCryptoServiceProvider objDESCrypto =
new TripleDESCryptoServiceProvider();
MD5CryptoServiceProvider objHashMD5 = new MD5CryptoServiceProvider();
byte[] byteHash, byteBuff;
string strTempKey = passPhrase;
byteHash = objHashMD5.ComputeHash(ASCIIEncoding.ASCII.GetBytes(strTempKey));
objHashMD5 = null;
objDESCrypto.Key = byteHash;
objDESCrypto.Mode = CipherMode.ECB; //CBC, CFB
byteBuff = Convert.FromBase64String(cipherText);
string strDecrypted = ASCIIEncoding.ASCII.GetString
(objDESCrypto.CreateDecryptor().TransformFinalBlock
(byteBuff, 0, byteBuff.Length));
objDESCrypto = null;
return strDecrypted;
}
catch (Exception ex)
{
return null;
}
}
示例14: 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;
}
}
示例15: Encrypt
/// <summary>
/// Encrypts to provided string parameter.
/// </summary>
public static string Encrypt(string s)
{
if (s == null || s.Length == 0) return string.Empty;
string result = string.Empty;
try
{
byte[] buffer = Encoding.ASCII.GetBytes(s);
TripleDESCryptoServiceProvider des =
new TripleDESCryptoServiceProvider();
MD5CryptoServiceProvider MD5 =
new MD5CryptoServiceProvider();
des.Key =
MD5.ComputeHash(ASCIIEncoding.ASCII.GetBytes(cryptoKey));
des.IV = IV;
result = Convert.ToBase64String(
des.CreateEncryptor().TransformFinalBlock(
buffer, 0, buffer.Length));
}
catch
{
throw;
}
return result;
}