本文整理汇总了C#中System.Security.Cryptography.SymmetricAlgorithm.CreateEncryptor方法的典型用法代码示例。如果您正苦于以下问题:C# SymmetricAlgorithm.CreateEncryptor方法的具体用法?C# SymmetricAlgorithm.CreateEncryptor怎么用?C# SymmetricAlgorithm.CreateEncryptor使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Security.Cryptography.SymmetricAlgorithm
的用法示例。
在下文中一共展示了SymmetricAlgorithm.CreateEncryptor方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: EncryptString
public static string EncryptString(string Value, string parKey)
{
mCSP = SetEnc();
string iv = "PenS8UCVF7s=";
mCSP.IV = Convert.FromBase64String(iv);
string key = SetLengthString(parKey.ToString(), 32);
mCSP.Key = Convert.FromBase64String(key);
ICryptoTransform ct;
MemoryStream ms;
CryptoStream cs;
Byte[] byt = new byte[64];
try
{
ct = mCSP.CreateEncryptor(mCSP.Key, mCSP.IV);
byt = Encoding.UTF8.GetBytes(Value);
ms = new MemoryStream();
cs = new CryptoStream(ms, ct, CryptoStreamMode.Write);
cs.Write(byt, 0, byt.Length);
cs.FlushFinalBlock();
cs.Close();
return Convert.ToBase64String(ms.ToArray());
}
catch (Exception Ex)
{
throw (new Exception("An error occurred while encrypting string"));
}
}
示例2: EncryptFile
/// <summary>
/// 加密文件
/// </summary>
/// <param name="inName">来源文件</param>
/// <param name="outName">输出文件</param>
/// <param name="Algorithm">对称算法</param>
public static void EncryptFile(string inName, string outName, SymmetricAlgorithm Algorithm)
{
FileStream fin = new FileStream(inName, FileMode.Open, FileAccess.Read);
using (FileStream fout = new FileStream(outName, FileMode.OpenOrCreate, FileAccess.Write))
{
fout.SetLength(0);
CryptoStream encStream = new CryptoStream(fout, Algorithm.CreateEncryptor(), CryptoStreamMode.Write);
//Read from the input file, then encrypt and write to the output file.
//Create variables to help with read and write.
long rdlen = 0; //This is the total number of bytes written.
int len; //This is the number of bytes to be written at a time.
byte[] bin = new byte[FileReadStep];
while (rdlen < fin.Length)
{
len = fin.Read(bin, 0, FileReadStep);
encStream.Write(bin, 0, len);
rdlen += len;
}
encStream.Close();
fin.Close();
}
}
示例3: Encrypt
public static byte[] Encrypt(string strText, SymmetricAlgorithm key)
{
// Create a memory stream.
MemoryStream ms = new MemoryStream();
// Create a CryptoStream using the memory stream and the
// CSP(cryptoserviceprovider) DES key.
CryptoStream crypstream = new CryptoStream(ms, key.CreateEncryptor(), CryptoStreamMode.Write);
// Create a StreamWriter to write a string to the stream.
StreamWriter sw = new StreamWriter(crypstream);
// Write the strText to the stream.
sw.WriteLine(strText);
// Close the StreamWriter and CryptoStream.
sw.Close();
crypstream.Close();
// Get an array of bytes that represents the memory stream.
byte[] buffer = ms.ToArray();
// Close the memory stream.
ms.Close();
// Return the encrypted byte array.
return buffer;
}
示例4: EncryptBytes
public static byte[] EncryptBytes(SymmetricAlgorithm symAlg, byte[] inBlock)
{
ICryptoTransform xfrm = symAlg.CreateEncryptor();
byte[] outBlock = xfrm.TransformFinalBlock(inBlock, 0, inBlock.Length);
return outBlock;
}
示例5: CipherTextStealingMode
/// <summary>
/// Initialize CipherTextStealingMode with a specific symmetric algorithm
/// </summary>
/// <param name="symmetricAlgorithm">The symmetric algorithm</param>
public CipherTextStealingMode(SymmetricAlgorithm symmetricAlgorithm)
{
// in CTS Mode there is no padding
symmetricAlgorithm.Padding = PaddingMode.None;
// set the symmetric algorithm's mode to ECB
// (for single block encryption and decryption)
symmetricAlgorithm.Mode = CipherMode.ECB;
// get the symmetric algorithm's block size in bytes
blockSize = symmetricAlgorithm.BlockSize / 8;
if (blockSize != symmetricAlgorithm.IV.Length)
{
throw new ArgumentException(
"The IV size should equal to the block size.");
}
// initialize local IV
iv = symmetricAlgorithm.IV;
// initialize cipher state using the symmetric algorithms's IV
cipherState = new byte[blockSize];
symmetricAlgorithm.IV.CopyTo(cipherState, 0);
// create encryptor and decryptor
encryptor = symmetricAlgorithm.CreateEncryptor();
decryptor = symmetricAlgorithm.CreateDecryptor();
}
示例6: EncryptEnvelopeKeyUsingSymmetricKey
private static byte[] EncryptEnvelopeKeyUsingSymmetricKey(SymmetricAlgorithm symmetricAlgorithm, byte[] envelopeKey)
{
symmetricAlgorithm.Mode = CipherMode.ECB;
using (ICryptoTransform encryptor = symmetricAlgorithm.CreateEncryptor())
{
return (encryptor.TransformFinalBlock(envelopeKey, 0, envelopeKey.Length));
}
}
示例7: Encrypt
private byte[] Encrypt (SymmetricAlgorithm algo, PaddingMode padding, byte[] data)
{
algo.IV = new byte [algo.BlockSize >> 3];
algo.Mode = CipherMode.CBC;
algo.Padding = padding;
ICryptoTransform ct = algo.CreateEncryptor ();
return ct.TransformFinalBlock (data, 0, data.Length);
}
示例8: TransformByCryptoStream
private byte[] TransformByCryptoStream(SymmetricAlgorithm algorithm, byte[] bytes, bool encrypt)
{
algorithm.Clear();
if (encrypt)
return TransformByCryptoStream(algorithm.CreateEncryptor(), bytes);
else
return TransformByCryptoStream(algorithm.CreateDecryptor(), bytes);
}
示例9: CreateCryptoTransform
private static ICryptoTransform CreateCryptoTransform(SymmetricAlgorithm algorithm, CryptoAction action)
{
byte[] key = Encoding.ASCII.GetBytes(stringKey);
byte[] vector = Encoding.ASCII.GetBytes(stringVector);
return action == CryptoAction.Encrypt
? algorithm.CreateEncryptor(key, vector)
: algorithm.CreateDecryptor(key, vector);
}
示例10: WinzipAesCryptoStream
internal WinzipAesCryptoStream(Stream stream, WinzipAesEncryptionData winzipAesEncryptionData, long length)
{
this.stream = stream;
totalBytesLeftToRead = length;
cipher = CreateCipher(winzipAesEncryptionData);
var iv = new byte[BLOCK_SIZE_IN_BYTES];
transform = cipher.CreateEncryptor(winzipAesEncryptionData.KeyBytes, iv);
}
示例11: Encrypt
public static byte[] Encrypt(SymmetricAlgorithm algorithm,byte[] data)
{
using (MemoryStream memory = new MemoryStream())
{
using (CryptoStream encrypt = new CryptoStream(memory, algorithm.CreateEncryptor(algorithm.Key, algorithm.IV), CryptoStreamMode.Write))
{
encrypt.Write(data, 0, data.Length);
}
return memory.ToArray();
}
}
示例12: EncryptPassword
/// <summary>
/// Encrypts the offline transaction file using the TripleDES cryptography.
/// </summary>
/// <param name="Password"></param>
/// <returns></returns>
public static string EncryptPassword(string Password)
{
string testPwd = string.Empty;
DES = new TripleDESCryptoServiceProvider();
byte[] plaintext = Encoding.ASCII.GetBytes(Password);
DES.Key = ParseKey(desKey);
DES.IV = GetIV();
//string decPwd = DecryptPassword("Lw5AEvoSG+7VlrMK+XgmGw==");
byte[] encrypted = DES.CreateEncryptor().TransformFinalBlock(plaintext, 0, plaintext.Length);
return Convert.ToBase64String(encrypted);
}
示例13: Encrypt
private static byte[] Encrypt(string PlainText, SymmetricAlgorithm key)
{
MemoryStream ms = new MemoryStream();
CryptoStream encStream = new CryptoStream(ms, key.CreateEncryptor(), CryptoStreamMode.Write);
StreamWriter sw = new StreamWriter(encStream);
sw.WriteLine(PlainText);
sw.Close();
encStream.Close();
byte[] buffer = ms.ToArray();
ms.Close();
return buffer;
}
示例14: Encrypt
public static byte[] Encrypt(SymmetricAlgorithm algorithm, byte[] src)
{
using (MemoryStream ms = new MemoryStream())
{
using (CryptoStream cs = new CryptoStream(ms, algorithm.CreateEncryptor(), CryptoStreamMode.Write))
{
cs.Write(src, 0, src.Length);
cs.FlushFinalBlock();
return ms.ToArray();
}
}
}
示例15: CtrModeCryptoTransform
public CtrModeCryptoTransform(SymmetricAlgorithm algorithm)
{
Contract.Requires(algorithm != null);
algorithm.Mode = CipherMode.ECB;
algorithm.Padding = PaddingMode.None;
_algorithm = algorithm;
_transform = algorithm.CreateEncryptor();
_iv = algorithm.IV;
_block = new byte[algorithm.BlockSize >> 3];
}