本文整理汇总了C#中DESCryptoServiceProvider类的典型用法代码示例。如果您正苦于以下问题:C# DESCryptoServiceProvider类的具体用法?C# DESCryptoServiceProvider怎么用?C# DESCryptoServiceProvider使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
DESCryptoServiceProvider类属于命名空间,在下文中一共展示了DESCryptoServiceProvider类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Crypt
public static string Crypt(this string text)
{
try
{
//SymmetricAlgorithm algorithm = DES.Create();
DES des = new DESCryptoServiceProvider();
des.Mode = CipherMode.ECB;
des.Padding = PaddingMode.PKCS7;
des.Key = key;
des.IV = iv;
//ICryptoTransform transform = algorithm.CreateEncryptor(key, iv);
ICryptoTransform transform = des.CreateEncryptor(key,iv);
byte[] inputbuffer = Encoding.Unicode.GetBytes(text);
byte[] outputBuffer;
try
{
outputBuffer = transform.TransformFinalBlock(inputbuffer, 0, inputbuffer.Length);
}
catch(Exception e)
{
inputbuffer = Encoding.UTF8.GetBytes(text);
outputBuffer = transform.TransformFinalBlock(inputbuffer, 0, inputbuffer.Length);
}
string asdfs = Convert.ToBase64String(outputBuffer);
return asdfs;
}
catch (Exception e)
{
return "";
}
}
示例2: Main
static int Main ()
{
string filename = Path.Combine (AppDomain.CurrentDomain.BaseDirectory,
"encrypt.tmp");
string data = "this is sensitive data";
DESCryptoServiceProvider des = new DESCryptoServiceProvider ();
des.GenerateIV ();
des.GenerateKey ();
// ----------- WRITING ENCRYPTED SERIALIZED DATA ------------------
Stream stream = new FileStream (filename, FileMode.Create, FileAccess.Write);
stream = new CryptoStream (stream, des.CreateEncryptor (), CryptoStreamMode.Write);
BinaryFormatter bformatter = new BinaryFormatter ();
bformatter.Serialize (stream, data);
stream.Close ();
stream = null;
bformatter = null;
data = string.Empty;
// ----------- READING ENCRYPTED SERIALIZED DATA ------------------
stream = new FileStream (filename, FileMode.Open, FileAccess.Read);
stream = new CryptoStream (stream, des.CreateDecryptor (), CryptoStreamMode.Read);
bformatter = new BinaryFormatter ();
data = (string) bformatter.Deserialize (stream);
stream.Close ();
//----------- CHECK RESULTS ----------------
if (data != "this is sensitive data")
return 1;
return 0;
}
示例3: DecryptFile
/// <summary>
/// 对文件内容进行DES解密
/// </summary>
/// <param name="sourceFile">待解密的文件绝对路径</param>
/// <param name="destFile">解密后的文件保存的绝对路径</param>
public static void DecryptFile(string sourceFile, string destFile)
{
if (!File.Exists(sourceFile)) throw new FileNotFoundException("指定的文件路径不存在!", sourceFile);
byte[] btKey = Encoding.Default.GetBytes(key);
byte[] btIV = Encoding.Default.GetBytes(iv);
DESCryptoServiceProvider des = new DESCryptoServiceProvider();
byte[] btFile = File.ReadAllBytes(sourceFile);
using (FileStream fs = new FileStream(destFile, FileMode.Create, FileAccess.Write))
{
try
{
using (CryptoStream cs = new CryptoStream(fs, des.CreateDecryptor(btKey, btIV), CryptoStreamMode.Write))
{
cs.Write(btFile, 0, btFile.Length);
cs.FlushFinalBlock();
}
}
catch
{
throw;
}
finally
{
fs.Close();
}
}
}
示例4: TryDecrypt
public bool TryDecrypt(string encryptedItem, string password, out string decryptedItem)
{
if (string.IsNullOrEmpty (encryptedItem) ||
string.IsNullOrEmpty (password)) {
decryptedItem = "";
return false;
}
try {
byte[] cipherBytes = Convert.FromBase64String (encryptedItem);
using (var memoryStream = new MemoryStream(cipherBytes)) {
var des = new DESCryptoServiceProvider ();
byte[] iv = new byte[8];
memoryStream.Read (iv, 0, iv.Length);
var rfc2898DeriveBytes = new Rfc2898DeriveBytes (password, iv, ITERATIONS);
byte[] key = rfc2898DeriveBytes.GetBytes (8);
using (var cryptoStream = new CryptoStream(memoryStream, des.CreateDecryptor(key, iv), CryptoStreamMode.Read))
using (var streamReader = new StreamReader(cryptoStream)) {
decryptedItem = streamReader.ReadToEnd ();
return true;
}
}
} catch (Exception ex) {
Logger.instance.Log (new Logger.Message (this, ex));
decryptedItem = "";
return false;
}
}
示例5: DeCryption
private string DeCryption(string content, string sKey, string sIV)
{
try
{
byte[] inputByteArray = Convert.FromBase64String(content);
using (DESCryptoServiceProvider desProvider = new DESCryptoServiceProvider())
{
//byte[] inputByteArray = Convert.FromBase64String(content);
desProvider.Key = System.Text.Encoding.ASCII.GetBytes(sKey);
desProvider.IV = System.Text.Encoding.ASCII.GetBytes(sIV);
System.IO.MemoryStream ms = new System.IO.MemoryStream();
using (CryptoStream cs = new CryptoStream(ms,desProvider.CreateDecryptor(), CryptoStreamMode.Write))
{
//cs.Clear();
cs.Write(inputByteArray,0,inputByteArray.Length);
cs.FlushFinalBlock();
cs.Close();
}
string str = System.Text.Encoding.UTF8.GetString(ms.ToArray());
ms.Close();
return str;
}
}
catch
{
return "String Not Base64 Encode!!!";
}
}
示例6: Load
public static SaveData Load()
{
if (!File.Exists("save01.sav"))
return null;
SaveData data = null;
using(Stream stream = File.Open("save01.sav", FileMode.Open)) {
BinaryFormatter binaryFormatter = new BinaryFormatter();
binaryFormatter.Binder = new VersionDeserializationBinder();
byte[] byKey = Encoding.UTF8.GetBytes(strEncrypt.Substring(0, 8));
using (DESCryptoServiceProvider des = new DESCryptoServiceProvider()) {
using (Stream cryptoStream = new CryptoStream(stream, des.CreateDecryptor(byKey, dv), CryptoStreamMode.Read))
{
Log.debug("Save", "Loading...");
try {
data = (SaveData) binaryFormatter.Deserialize(cryptoStream);
} catch (Exception e) {
Log.debug("Save", ""+e);
return new SaveData();
}
Log.debug("Save", "Loaded (level={0})", data.level);
cryptoStream.Close();
}
}
stream.Close();
}
return data;
}
示例7: Encrypt
public string Encrypt(string item, string password)
{
if (item == null) {
throw new ArgumentNullException ("item");
}
if (string.IsNullOrEmpty (password)) {
throw new ArgumentNullException ("password");
}
var des = new DESCryptoServiceProvider ();
des.GenerateIV ();
var rfc2898DeriveBytes = new Rfc2898DeriveBytes (password, des.IV, ITERATIONS);
byte[] key = rfc2898DeriveBytes.GetBytes (8);
using (var memoryStream = new MemoryStream()) {
using (var cryptoStream = new CryptoStream(memoryStream, des.CreateEncryptor(key, des.IV), CryptoStreamMode.Write)) {
memoryStream.Write (des.IV, 0, des.IV.Length);
byte[] bytes = Encoding.UTF8.GetBytes (item);
cryptoStream.Write (bytes, 0, bytes.Length);
cryptoStream.FlushFinalBlock ();
return Convert.ToBase64String (memoryStream.ToArray ());
}
}
}
示例8: Encrypt
public static string Encrypt(string originalString, string sKey)
{
DESCryptoServiceProvider des = new DESCryptoServiceProvider();
// 把字符串放到byte数组中
byte[] inputByteArray = Encoding.Default.GetBytes(originalString);
des.Key = ASCIIEncoding.ASCII.GetBytes(sKey); //建立加密对象的密钥和偏移量
des.IV = ASCIIEncoding.ASCII.GetBytes(sKey); //原文使用ASCIIEncoding.ASCII方法的
//GetBytes方法
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();
}
示例9: Decrypt
//cmThe function used to decrypt the text
private static string Decrypt(string strText, string SaltKey)
{
byte[] byKey = { };
byte[] IV = { 0x12, 0x34, 0x56, 0x78, 0x90, 0xab, 0xcd, 0xef };
byte[] inputByteArray = new byte[strText.Length + 1];
try
{
byKey = System.Text.Encoding.UTF8.GetBytes(SaltKey.Substring(0, 8));
DESCryptoServiceProvider des = new DESCryptoServiceProvider();
inputByteArray = Convert.FromBase64String(strText);
MemoryStream ms = new MemoryStream();
CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(byKey, IV), CryptoStreamMode.Write);
cs.Write(inputByteArray, 0, inputByteArray.Length);
cs.FlushFinalBlock();
System.Text.Encoding encoding = System.Text.Encoding.UTF8;
return encoding.GetString(ms.ToArray());
}
catch (Exception ex)
{
return ex.Message;
}
}
示例10: Decrypt
public static string Decrypt(string originalString, string sKey)
{
DESCryptoServiceProvider des = new DESCryptoServiceProvider();
byte[] inputByteArray = new byte[originalString.Length / 2];
for (int x = 0; x < originalString.Length / 2; x++)
{
int i = (Convert.ToInt32(originalString.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();
//建立StringBuild对象,CreateDecrypt使用的是流对象,必须把解密后的文本变成流对象
StringBuilder ret = new StringBuilder();
return System.Text.Encoding.Default.GetString(ms.ToArray());
}
示例11: Decode
/// <summary>
/// 对数据进行解密
/// </summary>
/// <param name="decryptstring">需要解密的数据</param>
/// <returns></returns>
public static string Decode(string data)
{
byte[] byKey = System.Text.ASCIIEncoding.ASCII.GetBytes(KEY_64);
byte[] byIV = System.Text.ASCIIEncoding.ASCII.GetBytes(IV_64);
byte[] byEnc;
try
{
byEnc = Convert.FromBase64String(data);
}
catch
{
return null;
}
try
{
DESCryptoServiceProvider cryptoProvider = new DESCryptoServiceProvider();
MemoryStream ms = new MemoryStream(byEnc);
CryptoStream cst = new CryptoStream(ms, cryptoProvider.CreateDecryptor(byKey, byIV), CryptoStreamMode.Read);
StreamReader sr = new StreamReader(cst);
return sr.ReadToEnd();
}
catch
{
return null;
}
}
示例12: EncryptData
private static void EncryptData(String inName, String outName, byte[] desKey, byte[] desIV)
{
//Create the file streams to handle the input and output files.
FileStream fin = new FileStream(inName, FileMode.Open, FileAccess.Read);
FileStream fout = new FileStream(outName, FileMode.OpenOrCreate, FileAccess.Write);
fout.SetLength(0);
//Create variables to help with read and write.
byte[] bin = new byte[100]; //This is intermediate storage for the encryption.
long rdlen = 0; //This is the total number of bytes written.
long totlen = fin.Length; //This is the total length of the input file.
int len; //This is the number of bytes to be written at a time.
SymmetricAlgorithm des = new DESCryptoServiceProvider();
des.Padding = PaddingMode.PKCS7;
CryptoStream encStream = new CryptoStream(fout, des.CreateEncryptor(desKey, desIV), CryptoStreamMode.Write);
Console.WriteLine("Encrypting...");
//Read from the input file, then encrypt and write to the output file.
while(rdlen < totlen)
{
len = fin.Read(bin, 0, 100);
encStream.Write(bin, 0, len);
rdlen = rdlen + len;
Console.WriteLine("{0} bytes processed", rdlen);
}
encStream.Close();
}
示例13: Decrypt
public static string Decrypt(string stringToDecrypt)
{
string sEncryptionKey = "thuynnxkey";
byte[] key = { };
byte[] IV = { 10, 20, 30, 40, 50, 60, 70, 80 };
byte[] inputByteArray = new byte[stringToDecrypt.Length];
try
{
key = Encoding.UTF8.GetBytes(sEncryptionKey.Substring(0, 8));
DESCryptoServiceProvider des = new DESCryptoServiceProvider();
inputByteArray = Convert.FromBase64String(stringToDecrypt);
MemoryStream ms = new MemoryStream();
CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(key, IV), CryptoStreamMode.Write);
cs.Write(inputByteArray, 0, inputByteArray.Length);
cs.FlushFinalBlock();
cs.Close();
Encoding encoding = Encoding.UTF8;
return encoding.GetString(ms.ToArray());
}
catch (System.Exception ex)
{
SiAuto.Main.LogString("Lỗi :", ex.ToString());
return (string.Empty);
}
}
示例14: EncryptString
// 加密字符串
public string EncryptString(string sInputString, string sKey)
{
byte[] data = Encoding.UTF8.GetBytes(sInputString);
var DES = new DESCryptoServiceProvider {Key = Encoding.ASCII.GetBytes(sKey), IV = Encoding.ASCII.GetBytes(sKey)};
ICryptoTransform desencrypt = DES.CreateEncryptor();
byte[] result = desencrypt.TransformFinalBlock(data,0,data.Length);
return BitConverter.ToString(result);
}
示例15: Decrypt
public string Decrypt(string SourceData)
{
byte[] EncryptedDataBytes = Convert.FromBase64String(SourceData);
MemoryStream TempStream = new MemoryStream(EncryptedDataBytes, 0, EncryptedDataBytes.Length);
DESCryptoServiceProvider Decryptor = new DESCryptoServiceProvider();
CryptoStream DecryptionStream = new CryptoStream(TempStream, Decryptor.CreateDecryptor(Key, Iv), CryptoStreamMode.Read);
StreamReader AllDataReader = new StreamReader(DecryptionStream);
return AllDataReader.ReadToEnd();
}