本文整理汇总了C#中DESCryptoServiceProvider.CreateDecryptor方法的典型用法代码示例。如果您正苦于以下问题:C# DESCryptoServiceProvider.CreateDecryptor方法的具体用法?C# DESCryptoServiceProvider.CreateDecryptor怎么用?C# DESCryptoServiceProvider.CreateDecryptor使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类DESCryptoServiceProvider
的用法示例。
在下文中一共展示了DESCryptoServiceProvider.CreateDecryptor方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: 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;
}
示例2: 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!!!";
}
}
示例3: DecryptData
private static void DecryptData(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.CreateDecryptor(desKey, desIV), CryptoStreamMode.Write);
Console.WriteLine("Decrypting...");
//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();
}
示例4: 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;
}
示例5: 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;
}
}
示例6: 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;
}
}
示例7: 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());
}
示例8: 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;
}
}
示例9: 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);
}
}
示例10: 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();
}
}
}
示例11: 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();
}
示例12: DESDecrypt
public static string DESDecrypt(string input)
{
byte[] bytes = Encoding.ASCII.GetBytes((string)ConfigurationSettings.AppSettings["CipherKeyProd"]);
DESCryptoServiceProvider cryptoProvider = new DESCryptoServiceProvider();
MemoryStream memoryStream = new MemoryStream
(Convert.FromBase64String(input));
CryptoStream cryptoStream = new CryptoStream(memoryStream,
cryptoProvider.CreateDecryptor(bytes, bytes), CryptoStreamMode.Read);
StreamReader reader = new StreamReader(cryptoStream);
return reader.ReadToEnd();
}
示例13: DesDecrypt
/**/
/// <summary>
/// 对称加密解密的密钥
/// </summary>
public static string Key
{
get
{
return key;
}
set
{
key = value;
}
}
#endregion Properties
#region Methods
/**/
/// <summary>
/// DES解密
/// </summary>
/// <param name="decryptString"></param>
/// <returns></returns>
public static string DesDecrypt(string decryptString)
{
byte[] keyBytes = Encoding.UTF8.GetBytes(key.Substring(0, 8));
byte[] keyIV = keyBytes;
byte[] inputByteArray = Convert.FromBase64String(decryptString);
DESCryptoServiceProvider provider = new DESCryptoServiceProvider();
MemoryStream mStream = new MemoryStream();
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());
}
示例14: Decrypt
public string Decrypt(string input)
{
string output = "";
DESCryptoServiceProvider cryptoProvider = new DESCryptoServiceProvider();
MemoryStream memoryStream = new MemoryStream(Convert.FromBase64String(input));
CryptoStream cryptoStream = new CryptoStream(memoryStream, cryptoProvider.CreateDecryptor(cryptBytes, cryptBytes), CryptoStreamMode.Read);
StreamReader reader = new StreamReader(cryptoStream);
output = reader.ReadToEnd();
return output;
}
示例15: DecryptDES
public static string DecryptDES(string text, byte[] key)
{
if(text != null && text != "")
{
DESCryptoServiceProvider cryptoProvider = new DESCryptoServiceProvider();
MemoryStream memoryStream = new MemoryStream(Convert.FromBase64String(text));
CryptoStream cryptoStream = new CryptoStream(memoryStream,
cryptoProvider.CreateDecryptor(key, key), CryptoStreamMode.Read);
StreamReader reader = new StreamReader(cryptoStream);
text = reader.ReadToEnd();
}
return text;
}