本文整理汇总了C#中System.Security.Cryptography.DESCryptoServiceProvider.CreateEncryptor方法的典型用法代码示例。如果您正苦于以下问题:C# DESCryptoServiceProvider.CreateEncryptor方法的具体用法?C# DESCryptoServiceProvider.CreateEncryptor怎么用?C# DESCryptoServiceProvider.CreateEncryptor使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Security.Cryptography.DESCryptoServiceProvider
的用法示例。
在下文中一共展示了DESCryptoServiceProvider.CreateEncryptor方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: 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;
}
}
示例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
/// <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 "";
}
示例4: 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();
}
}
示例5: EncryptString
public static string EncryptString(string _inputString,
string sKey)
{
string sInputFilename = string.Empty;
string sOutputFilename = string.Empty;
FileStream fsInput = new FileStream(_inputString,
FileMode.Open,
FileAccess.Read);
FileStream fsEncrypted = new FileStream(sOutputFilename,
FileMode.Create,
FileAccess.Write);
DESCryptoServiceProvider DES = new DESCryptoServiceProvider();
DES.Key = ASCIIEncoding.ASCII.GetBytes(sKey);
DES.IV = ASCIIEncoding.ASCII.GetBytes(sKey);
ICryptoTransform desencrypt = DES.CreateEncryptor();
CryptoStream cryptostream = new CryptoStream(fsEncrypted,
desencrypt,
CryptoStreamMode.Write);
byte[] bytearrayinput = new byte[_inputString.Length];
fsInput.Read(bytearrayinput, 0, bytearrayinput.Length);
cryptostream.Write(bytearrayinput, 0, bytearrayinput.Length);
cryptostream.Close();
fsInput.Close();
fsEncrypted.Close();
return bytearrayinput.ToString();
}
示例6: EncryptString
/// <summary>
/// 使用DES以及固定的密码加密字符串
/// </summary>
/// <param name="pToEncrypt">待加密字符串</param>
/// <returns></returns>
public static string EncryptString(string pToEncrypt)
{
DESCryptoServiceProvider des = new DESCryptoServiceProvider();
try
{
byte[] inputByteArray = Encoding.Default.GetBytes(pToEncrypt);
des.Key = Encoding.Default.GetBytes(DESKEY);
des.IV = Encoding.Default.GetBytes(DESKEY);
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
{
return pToEncrypt;
}
finally
{
des = null;
}
}
示例7: Encrypt
/// <summary>
/// encrypt a file
/// </summary>
/// <param name="unencryptedFileName">name of the plain text file</param>
/// <param name="encryptedFileName">name of the encrypted file to create and write to</param>
/// <param name="key">the key to use to encrypt the file</param>
public static void Encrypt(string unencryptedFileName, string encryptedFileName, string key)
{
// create file IO streams
FileStream openStream = new FileStream(unencryptedFileName, FileMode.Open, FileAccess.Read);
FileStream writeStream = new FileStream(encryptedFileName, FileMode.Create, FileAccess.Write);
// create the crypto service provider
DESCryptoServiceProvider crypto = new DESCryptoServiceProvider();
// set the key and initialization vector
byte[] keyBytes = ASCIIEncoding.ASCII.GetBytes(key);
crypto.Key = keyBytes;
crypto.IV = keyBytes;
// create the CtryptoStream object to transform plain text to encrypted and write it to disk
ICryptoTransform transform = crypto.CreateEncryptor();
CryptoStream cryptoStream = new CryptoStream(writeStream, transform, CryptoStreamMode.Write);
byte[] unencryptedBytes = new byte[openStream.Length];
openStream.Read(unencryptedBytes, 0, unencryptedBytes.Length);
// write to disk
cryptoStream.Write(unencryptedBytes, 0, unencryptedBytes.Length);
// close connections
openStream.Close();
cryptoStream.Close();
writeStream.Close();
}
示例8: EncodeStr
/// <summary>
/// DES 加密算法
/// </summary>
/// <param name="str">待加密字符串</param>
/// <returns>加密后字符串</returns>
public static string EncodeStr(string str)
{
try
{
byte[] temp = Encoding.UTF8.GetBytes(str);
using (DESCryptoServiceProvider desc = new DESCryptoServiceProvider())
{
desc.Key = ASCIIEncoding.ASCII.GetBytes(key); desc.IV = ASCIIEncoding.ASCII.GetBytes(iv);
using (MemoryStream ms = new MemoryStream())
{
using (CryptoStream cs = new CryptoStream(ms, desc.CreateEncryptor(), CryptoStreamMode.Write))
{
cs.Write(temp, 0, temp.Length);
cs.FlushFinalBlock();
}
StringBuilder sb = new StringBuilder();
foreach (byte b in ms.ToArray())
{
sb.AppendFormat("{0:X2}", b);
}
return sb.ToString();
}
}
}
catch (Exception ex)
{
throw ex;
}
}
示例9: Encrypt
public static string Encrypt(string text)
{
byte[] encodedkey;
byte[] iv = { 0x1F, 0x2E, 0x3D, 0x4C, 0x5B, 0x6A, 0x78, 0xA7 };
byte[] bytes;
encodedkey = Encoding.UTF8.GetBytes(EncryptKey);
DESCryptoServiceProvider csp = new DESCryptoServiceProvider();
bytes = Encoding.UTF8.GetBytes(text);
MemoryStream ms = new MemoryStream();
try
{
CryptoStream cs = new CryptoStream(ms, csp.CreateEncryptor(encodedkey, iv), CryptoStreamMode.Write);
cs.Write(bytes, 0, bytes.Length);
cs.FlushFinalBlock();
}
catch (Exception ex)
{
return "";
}
return Convert.ToBase64String(ms.ToArray());
}
示例10: Encrypt
public static string Encrypt(string text)
{
try
{
key = Encoding.UTF8.GetBytes(stringKey.Substring(0, 8));
DESCryptoServiceProvider des = new DESCryptoServiceProvider();
Byte[] byteArray = Encoding.UTF8.GetBytes(text);
MemoryStream memoryStream = new MemoryStream();
CryptoStream cryptoStream = new CryptoStream(memoryStream,
des.CreateEncryptor(key, IV), CryptoStreamMode.Write);
cryptoStream.Write(byteArray, 0, byteArray.Length);
cryptoStream.FlushFinalBlock();
return Convert.ToBase64String(memoryStream.ToArray());
}
catch (Exception ex)
{
// Handle Exception Here
}
return string.Empty;
}
示例11: Criptografar
//Criptografa o Cookie
public static string Criptografar(string valor)
{
DESCryptoServiceProvider des;
MemoryStream ms;
CryptoStream cs; byte[] input;
try
{
using (des = new DESCryptoServiceProvider())
{
using (ms = new MemoryStream())
{
input = Encoding.UTF8.GetBytes(valor);
chave = Encoding.UTF8.GetBytes(chaveCriptografia.Substring(0, 8));
using (cs = new CryptoStream(ms, des.CreateEncryptor(chave, iv), CryptoStreamMode.Write))
{
cs.Write(input, 0, input.Length);
cs.FlushFinalBlock();
}
return Convert.ToBase64String(ms.ToArray());
}
}
}
catch (Exception ex)
{
throw ex;
}
}
示例12: Encrypt
/// <summary>
/// Do Encry for plainString.
/// </summary>
/// <param name="plainString">Plain string.</param>
/// <returns>Encrypted string as base64.</returns>
public string Encrypt(string plainString)
{
// Create the file streams to handle the input and output files.
MemoryStream fout = new MemoryStream(200);
fout.SetLength(0);
// Create variables to help with read and write.
byte[] bin = System.Text.Encoding.Unicode.GetBytes(plainString);
DES des = new DESCryptoServiceProvider();
// des.KeySize=64;
CryptoStream encStream = new CryptoStream(fout, des.CreateEncryptor(desKey, desIV), CryptoStreamMode.Write);
encStream.Write(bin, 0, bin.Length);
encStream.FlushFinalBlock();
fout.Flush();
fout.Seek(0, SeekOrigin.Begin);
// read all string
byte[] bout = new byte[fout.Length];
fout.Read(bout, 0, bout.Length);
encStream.Close();
fout.Close();
return Convert.ToBase64String(bout, 0, bout.Length);
}
示例13: DESEncrypt
// Encrypt the content
public static string DESEncrypt(string stringToEncrypt)
{
byte[] key;
byte[] IV;
byte[] inputByteArray;
try
{
key = Convert2ByteArray(DESKey);
IV = Convert2ByteArray(DESIV);
inputByteArray = Encoding.UTF8.GetBytes(stringToEncrypt);
DESCryptoServiceProvider des = new DESCryptoServiceProvider();
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 (System.Exception ex)
{
throw ex;
}
}
示例14: EncryptField
public string EncryptField(string fieldValue)
{
string result = string.Empty;
if (!string.IsNullOrEmpty(fieldValue)) {
try {
DESCryptoServiceProvider cryptoProvider = new DESCryptoServiceProvider();
MemoryStream memoryStream = new MemoryStream();
CryptoStream cryptoStream = new CryptoStream(memoryStream, cryptoProvider.CreateEncryptor(this.key, this.key), CryptoStreamMode.Write);
StreamWriter writer = new StreamWriter(cryptoStream);
writer.Write(fieldValue);
writer.Flush();
cryptoStream.FlushFinalBlock();
writer.Flush();
result = Convert.ToBase64String(memoryStream.GetBuffer(), 0, (int)memoryStream.Length);
} catch (Exception ex) {
/*LogManager.GetCurrentClassLogger().Error(
errorMessage => errorMessage("Could not encrypt value: {0}", fieldValue), ex);*/
throw new ApplicationException("Could not encrypt value: " + fieldValue, ex);
}
}
return result;
}
示例15: CreateEncryptor
public static ICryptoTransform CreateEncryptor(string key, CryptionType type)
{
ICryptoTransform transform;
SHA512 sha512 = new SHA512CryptoServiceProvider();
var bytes = sha512.ComputeHash(Sha1(key).ToAsciiBytes());
switch (type)
{
case CryptionType.Aes:
var aes = Rijndael.Create();
aes.Mode = CipherMode.CBC;
transform = aes.CreateEncryptor(bytes.Skip(17).Take(32).ToArray(), bytes.Skip(17).Take(16).ToArray());
aes.Clear();
break;
case CryptionType.Des:
var des = new DESCryptoServiceProvider { Mode = CipherMode.CBC };
transform = des.CreateEncryptor(bytes.Skip(17).Take(8).ToArray(), bytes.Skip(17).Take(16).ToArray());
des.Clear();
break;
default:
var tripleDes = new TripleDESCryptoServiceProvider { Mode = CipherMode.CBC };
transform = tripleDes.CreateEncryptor(bytes.Skip(17).Take(24).ToArray(), bytes.Skip(17).Take(16).ToArray());
tripleDes.Clear();
break;
}
return transform;
}