本文整理汇总了C#中DESCryptoServiceProvider.CreateEncryptor方法的典型用法代码示例。如果您正苦于以下问题:C# DESCryptoServiceProvider.CreateEncryptor方法的具体用法?C# DESCryptoServiceProvider.CreateEncryptor怎么用?C# DESCryptoServiceProvider.CreateEncryptor使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类DESCryptoServiceProvider
的用法示例。
在下文中一共展示了DESCryptoServiceProvider.CreateEncryptor方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: 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();
}
示例2: 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();
}
示例3: 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 "";
}
}
示例4: 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;
}
示例5: 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 ());
}
}
}
示例6: 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);
}
示例7: Encrypt
public string Encrypt(string SourceData)
{
byte[] SourceDataBytes = System.Text.ASCIIEncoding.ASCII.GetBytes(SourceData);
MemoryStream TempStream = new MemoryStream();
DESCryptoServiceProvider Encryptor = new DESCryptoServiceProvider();
CryptoStream EncryptionStream = new CryptoStream(TempStream, Encryptor.CreateEncryptor(Key, Iv), CryptoStreamMode.Write);
EncryptionStream.Write(SourceDataBytes, 0, SourceDataBytes.Length);
EncryptionStream.FlushFinalBlock();
byte[] EncryptedDataBytes = TempStream.GetBuffer();
//MsgBox(Convert.ToBase64String(EncryptedDataBytes, 0, TempStream.Length))
return Convert.ToBase64String(EncryptedDataBytes,0, Convert.ToInt16(TempStream.Length));
}
示例8: DesEncrypt
public static string DesEncrypt(string encryptString, string encrptKey)
{
if (string.IsNullOrEmpty(encrptKey)) encrptKey = Key;
byte[] keyBytes = Encoding.UTF8.GetBytes(encrptKey);
byte[] keyIV = keyBytes;
byte[] inputByteArray = Encoding.UTF8.GetBytes(encryptString);
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();
return Convert.ToBase64String(mStream.ToArray());
}
示例9: Test
static Boolean Test()
{
Byte[] PlainText = {0, 1, 2, 3, 4, 5, 6, 7}; //, 8, 9, 10, 11, 12, 13, 14, 15};
Byte[] Key = {1, 1, 1, 1, 1, 1, 1, 2};
Byte[] IV = {1, 1, 1, 1, 1, 1, 1, 1};
Console.WriteLine("Encrypting the following bytes:");
PrintByteArray(PlainText);
DESCryptoServiceProvider des = new DESCryptoServiceProvider();
des.Mode = CipherMode.ECB;
// des.FeedbackSize = 0;
des.Padding = PaddingMode.None;
Console.WriteLine("DES default key size = " + des.KeySize);
ICryptoTransform sse = des.CreateEncryptor(Key, IV);
Console.WriteLine("SSE mode = " + des.Mode);
MemoryStream ms = new MemoryStream();
CryptoStream cs = new CryptoStream(ms, sse, CryptoStreamMode.Write);
cs.Write(PlainText,0,PlainText.Length);
cs.FlushFinalBlock();
byte[] CipherText = ms.ToArray();
cs.Close();
Console.WriteLine("Cyphertext:");
PrintByteArray(CipherText);
Console.WriteLine("Decrypting...");
// DESCryptoServiceProvider des = new DESCryptoServiceProvider();
// des.Mode = CipherMode.ECB;
// des.FeedbackSize = 0;
ICryptoTransform ssd = des.CreateDecryptor(Key, IV);
Console.WriteLine("SSD mode = " + des.Mode);
cs = new CryptoStream(new MemoryStream(CipherText), ssd, CryptoStreamMode.Read);
byte[] NewPlainText = new byte[8];
cs.Read(NewPlainText,0,8);
PrintByteArray(NewPlainText);
if (!Compare(PlainText, NewPlainText)) {
Console.WriteLine("ERROR: roundtrip failed");
return false;
}
return true;
}
示例10: DESEncrypt
public static string DESEncrypt(string input)
{
byte[] bytes = Encoding.ASCII.GetBytes((string)ConfigurationSettings.AppSettings["CipherKeyProd"]);
DESCryptoServiceProvider cryptoProvider = new DESCryptoServiceProvider();
MemoryStream memoryStream = new MemoryStream();
CryptoStream cryptoStream = new CryptoStream(memoryStream,
cryptoProvider.CreateEncryptor(bytes, bytes), CryptoStreamMode.Write);
StreamWriter writer = new StreamWriter(cryptoStream);
writer.Write(input);
writer.Flush();
cryptoStream.FlushFinalBlock();
writer.Flush();
return Convert.ToBase64String(memoryStream.GetBuffer(), 0, (int)memoryStream.Length);
}
示例11: GetDesSignatureInBytes
public static byte[] GetDesSignatureInBytes(byte[] data, byte[] sharedSecret)
{
HashAlgorithm hash = new SHA1Managed();
byte[] hashedData = hash.ComputeHash(data);
DESCryptoServiceProvider des = new DESCryptoServiceProvider();
des.Key = sharedSecret;
des.Mode = CipherMode.ECB;
ICryptoTransform transform = des.CreateEncryptor();
byte[] signature = new byte[8];
transform.TransformBlock(hashedData, 0, signature.Length, signature, 0);
return signature;
}
示例12: Encrypt
public string Encrypt(string input)
{
string output = "";
DESCryptoServiceProvider cryptoProvider = new DESCryptoServiceProvider();
MemoryStream memoryStream = new MemoryStream();
CryptoStream cryptoStream = new CryptoStream(memoryStream, cryptoProvider.CreateEncryptor(cryptBytes, cryptBytes), CryptoStreamMode.Write);
StreamWriter writer = new StreamWriter(cryptoStream);
writer.Write(input);
writer.Flush();
cryptoStream.FlushFinalBlock();
writer.Flush();
output = Convert.ToBase64String(memoryStream.GetBuffer(), 0, (int)memoryStream.Length);
return output;
}
示例13: MD5Encrypt
/// <summary>
/// MD5����
/// </summary>
public static string MD5Encrypt(string Text, string sKey)
{
DESCryptoServiceProvider des = new DESCryptoServiceProvider();
byte[] inputByteArray = Encoding.Default.GetBytes(Text);
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));
System.IO.MemoryStream ms = new System.IO.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);
}
return ret.ToString();
}
示例14: EncryptDES
/*
============================================================================
DES handling functions
============================================================================
*/
public static string EncryptDES(string text, byte[] key)
{
if(text != null && text != "")
{
DESCryptoServiceProvider cryptoProvider = new DESCryptoServiceProvider();
MemoryStream memoryStream = new MemoryStream();
CryptoStream cryptoStream = new CryptoStream(memoryStream,
cryptoProvider.CreateEncryptor(key, key), CryptoStreamMode.Write);
StreamWriter writer = new StreamWriter(cryptoStream);
writer.Write(text);
writer.Flush();
cryptoStream.FlushFinalBlock();
writer.Flush();
text = Convert.ToBase64String(memoryStream.GetBuffer(), 0, (int)memoryStream.Length);
}
return text;
}
示例15: MD5Encrypt
/// DES加密
public string MD5Encrypt(string pToEncrypt, string sKey)
{
var des = new DESCryptoServiceProvider();
byte[] inputByteArray = Encoding.Default.GetBytes(pToEncrypt);
des.Key = Encoding.ASCII.GetBytes(sKey);
des.IV = Encoding.ASCII.GetBytes(sKey);
var ms = new MemoryStream();
var cs = new CryptoStream(ms, des.CreateEncryptor(), CryptoStreamMode.Write);
cs.Write(inputByteArray, 0, inputByteArray.Length);
cs.FlushFinalBlock();
var ret = new StringBuilder();
foreach (byte b in ms.ToArray())
{
ret.AppendFormat("{0:X2}", b);
}
ret.ToString();
return ret.ToString();
}