本文整理汇总了C#中System.Security.Cryptography.DESCryptoServiceProvider类的典型用法代码示例。如果您正苦于以下问题:C# DESCryptoServiceProvider类的具体用法?C# DESCryptoServiceProvider怎么用?C# DESCryptoServiceProvider使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
DESCryptoServiceProvider类属于System.Security.Cryptography命名空间,在下文中一共展示了DESCryptoServiceProvider类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Decrypt
public static string Decrypt(string sourceData)
{
// set key and initialization vector values
byte[] key = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8 };
byte[] iv = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8 };
try
{
// convert data to byte array
byte[] encryptedDataBytes =
Convert.FromBase64String(sourceData);
// get source memory stream and fill it
MemoryStream tempStream =
new MemoryStream(encryptedDataBytes, 0,
encryptedDataBytes.Length);
// get decryptor and decryption stream
DESCryptoServiceProvider decryptor =
new DESCryptoServiceProvider();
CryptoStream decryptionStream =
new CryptoStream(tempStream,
decryptor.CreateDecryptor(key, iv),
CryptoStreamMode.Read);
// decrypt data
StreamReader allDataReader =
new StreamReader(decryptionStream);
return allDataReader.ReadToEnd();
}
catch
{
throw new StringEncryptorException(
"Unable to decrypt data.");
}
}
示例2: Encode
public static string Encode(string str, string key)
{
DESCryptoServiceProvider provider = new DESCryptoServiceProvider();
provider.Key = Encoding.ASCII.GetBytes(key.Substring(0, 8));
provider.IV = Encoding.ASCII.GetBytes(key.Substring(0, 8));
byte[] bytes = Encoding.UTF8.GetBytes(str);
MemoryStream stream = new MemoryStream();
CryptoStream stream2 = new CryptoStream(stream, provider.CreateEncryptor(), CryptoStreamMode.Write);
stream2.Write(bytes, 0, bytes.Length);
stream2.FlushFinalBlock();
StringBuilder builder = new StringBuilder();
foreach (byte num in stream.ToArray())
{
builder.AppendFormat("{0:X2}", num);
}
stream.Close();
return builder.ToString();
}
示例3: Encrypt
public static string Encrypt( string strEncryptString, string strEncryptionKey)
{
byte[] inputByteArray;
try
{
key = Encoding.UTF8.GetBytes(strEncryptionKey.Substring(0, 8));
DESCryptoServiceProvider des = new DESCryptoServiceProvider();
inputByteArray = Encoding.UTF8.GetBytes(strEncryptString);
MemoryStream ms = new MemoryStream();
CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(key, IV), CryptoStreamMode.Write);
cs.Write(inputByteArray, 0, inputByteArray.Length);
cs.FlushFinalBlock();
return Convert.ToBase64String(ms.ToArray());
}
catch (Exception eX)
{
throw eX;
}
}
示例4: Encrypt
/// <summary>
/// 加密方法
/// </summary>
/// <param name="pToEncrypt">需要加密字符串</param>
/// <param name="sKey">密钥</param>
/// <returns>加密后的字符串</returns>
public static string Encrypt(string pToEncrypt, string sKey)
{
try
{
DESCryptoServiceProvider des = new DESCryptoServiceProvider();
//把字符串放到byte数组中
//原来使用的UTF8编码,我改成Unicode编码了,不行
byte[] inputByteArray = Encoding.Default.GetBytes(pToEncrypt);
//建立加密对象的密钥和偏移量
//使得输入密码必须输入英文文本
des.Key = ASCIIEncoding.ASCII.GetBytes(sKey);
des.IV = ASCIIEncoding.ASCII.GetBytes(sKey);
MemoryStream ms = new MemoryStream();
CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(), CryptoStreamMode.Write);
cs.Write(inputByteArray, 0, inputByteArray.Length);
cs.FlushFinalBlock();
StringBuilder ret = new StringBuilder();
foreach (byte b in ms.ToArray())
{
ret.AppendFormat("{0:X2}", b);
}
ret.ToString();
return ret.ToString();
}
catch (Exception ex)
{
}
return "";
}
示例5: Decrypt
/// <summary>
/// 解密
/// </summary>
/// <param name="text">要被解密字符</param>
/// <param name="sKey">密钥</param>
/// <returns></returns>
public static string Decrypt(this string text, string sKey)
{
var provider = new DESCryptoServiceProvider();
int num = text.Length / 2;
byte[] buffer = new byte[num];
try
{
for (int i = 0; i < num; i++)
{
int num3 = Convert.ToInt32(text.Substring(i * 2, 2), 0x10);
buffer[i] = (byte)num3;
}
}
catch
{
return string.Empty;
}
provider.Key = Encoding.ASCII.GetBytes(FormsAuthentication.HashPasswordForStoringInConfigFile(sKey, "md5").Substring(0, 8));
provider.IV = Encoding.ASCII.GetBytes(FormsAuthentication.HashPasswordForStoringInConfigFile(sKey, "md5").Substring(0, 8));
MemoryStream stream = new MemoryStream();
CryptoStream stream2 = new CryptoStream(stream, provider.CreateDecryptor(), CryptoStreamMode.Write);
try
{
stream2.Write(buffer, 0, buffer.Length);
stream2.FlushFinalBlock();
}
catch
{
return string.Empty;
}
return Encoding.Default.GetString(stream.ToArray());
}
示例6: Decrypt
/// <summary>
/// 解密数据
/// </summary>
/// <param name="Text"></param>
/// <param name="sKey"></param>
/// <returns></returns>
public static string Decrypt(string Text,string sKey)
{
try
{
DESCryptoServiceProvider des = new DESCryptoServiceProvider();
int len;
len = Text.Length / 2;
byte[] inputByteArray = new byte[len];
int x, i;
for (x = 0; x < len; x++)
{
i = Convert.ToInt32(Text.Substring(x * 2, 2), 16);
inputByteArray[x] = (byte)i;
}
//.net framework 4.5方法过期
//des.Key = ASCIIEncoding.ASCII.GetBytes(System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(sKey, "md5").Substring(0, 8));
//des.IV = ASCIIEncoding.ASCII.GetBytes(System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(sKey, "md5").Substring(0, 8));
MD5 md5Hash = MD5.Create();
des.Key = ASCIIEncoding.ASCII.GetBytes(GetMd5Hash(sKey, md5Hash).Substring(0, 8));
des.IV = ASCIIEncoding.ASCII.GetBytes(GetMd5Hash(sKey, md5Hash).Substring(0, 8));
System.IO.MemoryStream ms = new System.IO.MemoryStream();
CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(), CryptoStreamMode.Write);
cs.Write(inputByteArray, 0, inputByteArray.Length);
cs.FlushFinalBlock();
return Encoding.Default.GetString(ms.ToArray());
}
catch
{
return null;
}
}
示例7: DecodeStr
/// <summary>
/// DES 解密算法
/// </summary>
/// <param name="str">待解密字符串</param>
/// <returns>解密后字符串</returns>
public static string DecodeStr(string str)
{
try
{
byte[] temp = new byte[str.Length / 2];
using (DESCryptoServiceProvider desc = new DESCryptoServiceProvider())
{
for (int i = 0; i < str.Length / 2; i++)
{
int j = Convert.ToInt32(str.Substring(i * 2, 2), 16);
temp[i] = (byte)j;
}
desc.Key = ASCIIEncoding.ASCII.GetBytes(key); desc.IV = ASCIIEncoding.ASCII.GetBytes(iv);
using (MemoryStream ms = new MemoryStream())
{
using (CryptoStream cs = new CryptoStream(ms, desc.CreateDecryptor(), CryptoStreamMode.Write))
{
cs.Write(temp, 0, temp.Length);
cs.FlushFinalBlock();
}
return System.Text.Encoding.UTF8.GetString(ms.ToArray());
}
}
}
catch (Exception ex)
{
throw ex;
}
}
示例8: DESEncrypt
/// <summary>
/// 加密方法
/// </summary>
/// <param name="pToEncrypt">需要加密字符串</param>
/// <param name="sKey">密钥</param>
/// <returns>加密后的字符串</returns>
public static string DESEncrypt(string pToEncrypt)
{
try
{
DESCryptoServiceProvider des = new DESCryptoServiceProvider();
//把字符串放到byte数组中
//原来使用的UTF8编码,我改成Unicode编码了,不行
byte[] inputByteArray = Encoding.UTF8.GetBytes(pToEncrypt);
//建立加密对象的密钥和偏移量
//使得输入密码必须输入英文文本
des.Key = Encoding.UTF8.GetBytes(sKey);
des.IV = Encoding.UTF8.GetBytes(sKey);
MemoryStream ms = new MemoryStream();
CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(), CryptoStreamMode.Write);
cs.Write(inputByteArray, 0, inputByteArray.Length);
cs.FlushFinalBlock();
StringBuilder ret = new StringBuilder();
foreach (byte b in ms.ToArray())
{
ret.AppendFormat("{0:X2}", b);
}
ret.ToString();
return ret.ToString();
}
catch (Exception ex)
{
System.Web.HttpContext.Current.Response.Write("写入配置信息失败,详细信息:" + ex.Message.Replace("\r\n", "").Replace("'", ""));
}
return "";
}
示例9: Decrypt
public static string Decrypt(string strText)
{
string text1;
if (strText == String.Empty)
{
return strText;
}
byte[] buffer1;
byte[] buffer2;
byte[] buffer3 = GetCryptArr();
try
{
buffer1 = Encoding.UTF8.GetBytes(GetCryptKey().Substring(0,8));
DESCryptoServiceProvider provider1 = new DESCryptoServiceProvider();
buffer2 = Convert.FromBase64String(strText);
MemoryStream stream2 = new MemoryStream();
using (CryptoStream stream1 = new CryptoStream(stream2, provider1.CreateDecryptor(buffer1, buffer3), CryptoStreamMode.Write))
{
stream1.Write(buffer2, 0, buffer2.Length);
stream1.FlushFinalBlock();
text1 = Encoding.UTF8.GetString(stream2.ToArray());
}
}
catch
{
return "Error";
}
return text1;
}
示例10: DESDeCode
public static string DESDeCode(string pToDecrypt, string sKey)
{
// HttpContext.Current.Response.Write(pToDecrypt + "<br>" + sKey);
// HttpContext.Current.Response.End();
DESCryptoServiceProvider des = new DESCryptoServiceProvider();
byte[] inputByteArray = new byte[pToDecrypt.Length / 2];
for (int x = 0; x < pToDecrypt.Length / 2; x++)
{
int i = (Convert.ToInt32(pToDecrypt.Substring(x * 2, 2), 16));
inputByteArray[x] = (byte)i;
}
des.Key = ASCIIEncoding.ASCII.GetBytes(sKey);
des.IV = ASCIIEncoding.ASCII.GetBytes(sKey);
MemoryStream ms = new MemoryStream();
CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(), CryptoStreamMode.Write);
cs.Write(inputByteArray, 0, inputByteArray.Length);
cs.FlushFinalBlock();
StringBuilder ret = new StringBuilder();
// return HttpContext.Current.Server.UrlDecode(System.Text.Encoding.Default.GetString(ms.ToArray()));
return System.Text.Encoding.Default.GetString(ms.ToArray());
}
示例11: Decrypt
public static string Decrypt(string text)
{
try
{
key = Encoding.UTF8.GetBytes(stringKey.Substring(0, 8));
DESCryptoServiceProvider des = new DESCryptoServiceProvider();
Byte[] byteArray = Convert.FromBase64String(text);
MemoryStream memoryStream = new MemoryStream();
CryptoStream cryptoStream = new CryptoStream(memoryStream,
des.CreateDecryptor(key, IV), CryptoStreamMode.Write);
cryptoStream.Write(byteArray, 0, byteArray.Length);
cryptoStream.FlushFinalBlock();
return Encoding.UTF8.GetString(memoryStream.ToArray());
}
catch (Exception ex)
{
// Handle Exception Here
}
return string.Empty;
}
示例12: EncryptString
/// <summary>
/// 加密字符串
/// </summary>
/// <param name="inputStr">输入字符串</param>
/// <param name="keyStr">密码,可以为“”</param>
/// <returns>输出加密后字符串</returns>
public string EncryptString(string inputStr, string keyStr)
{
DESCryptoServiceProvider des = new DESCryptoServiceProvider();
if (keyStr == "")
keyStr = key;
byte[] inputByteArray = Encoding.Default.GetBytes(inputStr);
byte[] keyByteArray = Encoding.Default.GetBytes(keyStr);
SHA1 ha = new SHA1Managed();
byte[] hb = ha.ComputeHash(keyByteArray);
sKey = new byte[8];
sIV = new byte[8];
for (int i = 0; i < 8; i++)
sKey[i] = hb[i];
for (int i = 8; i < 16; i++)
sIV[i - 8] = hb[i];
des.Key = sKey;
des.IV = sIV;
MemoryStream ms = new MemoryStream();
CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(), CryptoStreamMode.Write);
cs.Write(inputByteArray, 0, inputByteArray.Length);
cs.FlushFinalBlock();
StringBuilder ret = new StringBuilder();
foreach (byte b in ms.ToArray())
{
ret.AppendFormat("{0:X2}", b);
}
cs.Close();
ms.Close();
return ret.ToString();
}
示例13: DESEnCode
public static string DESEnCode(string pToEncrypt, string sKey)
{
// string pToEncrypt1 = HttpContext.Current.Server.UrlEncode(pToEncrypt);
DESCryptoServiceProvider des = new DESCryptoServiceProvider();
byte[] inputByteArray = Encoding.GetEncoding("UTF-8").GetBytes(pToEncrypt);
//建立加密对象的密钥和偏移量
//原文使用ASCIIEncoding.ASCII方法的GetBytes方法
//使得输入密码必须输入英文文本
des.Key = ASCIIEncoding.ASCII.GetBytes(sKey);
des.IV = ASCIIEncoding.ASCII.GetBytes(sKey);
MemoryStream ms = new MemoryStream();
CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(), CryptoStreamMode.Write);
cs.Write(inputByteArray, 0, inputByteArray.Length);
cs.FlushFinalBlock();
StringBuilder ret = new StringBuilder();
foreach (byte b in ms.ToArray())
{
ret.AppendFormat("{0:X2}", b);
}
ret.ToString();
return ret.ToString();
}
示例14: Decrypt
/// <summary>
/// 用给定的Key进行解密
/// </summary>
/// <param name="key">密钥</param>
/// <param name="decryptString">要解密的字符串</param>
/// <returns>解密后的字符串</returns>
public static string Decrypt(string decryptString, string key)
{
if (string.IsNullOrWhiteSpace(key))
key = _Key;
else
key += _Key;
if (string.IsNullOrWhiteSpace(decryptString))
decryptString = string.Empty;
byte[] keyBytes = Encoding.UTF8.GetBytes(key.Substring(0, 8));
byte[] keyIV = keyBytes;
byte[] inputByteArray = new byte[decryptString.Length / 2];
for (int x = 0; x < decryptString.Length / 2; x++)
{
int i = (Convert.ToInt32(decryptString.Substring(x * 2, 2), 16));
inputByteArray[x] = (byte)i;
}
using (DESCryptoServiceProvider provider = new DESCryptoServiceProvider())
{
using (MemoryStream mStream = new MemoryStream())
{
using (CryptoStream cStream = new CryptoStream(mStream, provider.CreateDecryptor(keyBytes, keyIV), CryptoStreamMode.Write))
{
cStream.Write(inputByteArray, 0, inputByteArray.Length);
cStream.FlushFinalBlock();
return Encoding.UTF8.GetString(mStream.ToArray());
}
}
}
}
示例15: Encrypt
/// <summary>
/// 用给定的Key进行加密
/// </summary>
/// <param name="key">密钥</param>
/// <param name="encryptString">要加密的字符串</param>
/// <returns>加密后的字符串</returns>
public static string Encrypt(string encryptString, string key)
{
if (string.IsNullOrWhiteSpace(key))
key = _Key;
else
key += _Key;
if (string.IsNullOrWhiteSpace(encryptString))
encryptString = string.Empty;
byte[] keyBytes = Encoding.UTF8.GetBytes(key.Substring(0, 8));
byte[] keyIV = keyBytes;
byte[] inputByteArray = Encoding.UTF8.GetBytes(encryptString);
using (DESCryptoServiceProvider provider = new DESCryptoServiceProvider())
{
MemoryStream mStream = new MemoryStream();
CryptoStream cStream = new CryptoStream(mStream, provider.CreateEncryptor(keyBytes, keyIV), CryptoStreamMode.Write);
cStream.Write(inputByteArray, 0, inputByteArray.Length);
cStream.FlushFinalBlock();
//Get the data back from the memory stream, and into a string
StringBuilder ret = new StringBuilder();
foreach (byte b in mStream.ToArray())
{
//Format as hex
ret.AppendFormat("{0:X2}", b);
}
return ret.ToString();
}
}