本文整理汇总了C#中System.Security.Cryptography.RijndaelManaged.CreateEncryptor方法的典型用法代码示例。如果您正苦于以下问题:C# RijndaelManaged.CreateEncryptor方法的具体用法?C# RijndaelManaged.CreateEncryptor怎么用?C# RijndaelManaged.CreateEncryptor使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Security.Cryptography.RijndaelManaged
的用法示例。
在下文中一共展示了RijndaelManaged.CreateEncryptor方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: AES_Encrypt
public byte[] AES_Encrypt(byte[] bytesToBeEncrypted, byte[] passwordBytes)
{
byte[] encryptedBytes = null;
// Set your salt here, change it to meet your flavor:
// The salt bytes must be at least 8 bytes.
byte[] saltBytes = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8 };
using (MemoryStream ms = new MemoryStream())
{
using (RijndaelManaged AES = new RijndaelManaged())
{
AES.KeySize = 256;
AES.BlockSize = 128;
var key = new Rfc2898DeriveBytes(passwordBytes, saltBytes, 1000);
AES.Key = key.GetBytes(AES.KeySize / 8);
AES.IV = key.GetBytes(AES.BlockSize / 8);
AES.Mode = CipherMode.CBC;
using (var cs = new CryptoStream(ms, AES.CreateEncryptor(), CryptoStreamMode.Write))
{
cs.Write(bytesToBeEncrypted, 0, bytesToBeEncrypted.Length);
cs.Close();
}
encryptedBytes = ms.ToArray();
}
}
return encryptedBytes;
}
示例2: EncryptionHelper
// Methods
public EncryptionHelper()
{
RijndaelManaged managed = new RijndaelManaged();
this.EncryptorTransform = managed.CreateEncryptor(this.Key, this.Vector);
this.DecryptorTransform = managed.CreateDecryptor(this.Key, this.Vector);
this.UTFEncoder = new UTF8Encoding();
}
示例3: EncryptString
public static string EncryptString(
string plainText,
string passPhrase,
string saltValue,
int passwordIterations,
string initVector,
int keySize)
{
byte[] initVectorBytes = initVector == null ? new byte[16] : Encoding.ASCII.GetBytes(initVector);
byte[] plainTextBytes = Encoding.UTF8.GetBytes(plainText);
byte[] keyBytes = GetKeyBytes(passPhrase, saltValue, passwordIterations, keySize);
RijndaelManaged symmetricKey = new RijndaelManaged();
symmetricKey.Mode = CipherMode.CBC;
ICryptoTransform encryptor = symmetricKey.CreateEncryptor(keyBytes, initVectorBytes);
byte[] cipherTextBytes;
using (MemoryStream memoryStream = new MemoryStream())
{
using (CryptoStream cryptoStream = new CryptoStream(memoryStream, encryptor, CryptoStreamMode.Write))
{
cryptoStream.Write(plainTextBytes, 0, plainTextBytes.Length);
cryptoStream.FlushFinalBlock();
cipherTextBytes = memoryStream.ToArray();
}
}
string cipherText = Convert.ToBase64String(cipherTextBytes);
return cipherText;
}
示例4: Encrypt
public static string Encrypt(string plainText, string passPhrase)
{
byte[] plainTextBytes = Encoding.UTF8.GetBytes(plainText);
using (PasswordDeriveBytes password = new PasswordDeriveBytes(passPhrase, null))
{
byte[] keyBytes = password.GetBytes(keysize / 8);
using (RijndaelManaged symmetricKey = new RijndaelManaged())
{
symmetricKey.Mode = CipherMode.CBC;
using (ICryptoTransform encryptor = symmetricKey.CreateEncryptor(keyBytes, initVectorBytes))
{
using (MemoryStream memoryStream = new MemoryStream())
{
using (CryptoStream cryptoStream = new CryptoStream(memoryStream, encryptor, CryptoStreamMode.Write))
{
cryptoStream.Write(plainTextBytes, 0, plainTextBytes.Length);
cryptoStream.FlushFinalBlock();
byte[] cipherTextBytes = memoryStream.ToArray();
return Convert.ToBase64String(cipherTextBytes);
}
}
}
}
}
}
示例5: Encrypt
public static string Encrypt(string plainText, string passPhrase)
{
// Salt and IV is randomly generated each time, but is preprended to encrypted cipher text
// so that the same Salt and IV values can be used when decrypting.
var saltStringBytes = Generate256BitsOfRandomEntropy();
var ivStringBytes = Generate256BitsOfRandomEntropy();
var plainTextBytes = Encoding.UTF8.GetBytes(plainText);
var password = new Rfc2898DeriveBytes(passPhrase, saltStringBytes, DerivationIterations);
var keyBytes = password.GetBytes(Keysize / 8);
using (var symmetricKey = new RijndaelManaged())
{
symmetricKey.BlockSize = 256;
symmetricKey.Mode = CipherMode.CBC;
symmetricKey.Padding = PaddingMode.PKCS7;
using (var encryptor = symmetricKey.CreateEncryptor(keyBytes, ivStringBytes))
{
using (var memoryStream = new MemoryStream())
{
using (var cryptoStream = new CryptoStream(memoryStream, encryptor, CryptoStreamMode.Write))
{
cryptoStream.Write(plainTextBytes, 0, plainTextBytes.Length);
cryptoStream.FlushFinalBlock();
// Create the final bytes as a concatenation of the random salt bytes, the random iv bytes and the cipher bytes.
var cipherTextBytes = saltStringBytes;
cipherTextBytes = cipherTextBytes.Concat(ivStringBytes).ToArray();
cipherTextBytes = cipherTextBytes.Concat(memoryStream.ToArray()).ToArray();
memoryStream.Close();
cryptoStream.Close();
return Convert.ToBase64String(cipherTextBytes);
}
}
}
}
}
示例6: AesEncryption
public AesEncryption(byte[] Key, byte[] Vector)
{
RijndaelManaged rijndaelManaged = new RijndaelManaged();
this.EncryptorTransform = rijndaelManaged.CreateEncryptor(Key, Vector);
this.DecryptorTransform = rijndaelManaged.CreateDecryptor(Key, Vector);
this.UTFEncoder = new UTF8Encoding();
}
示例7: EncryptKeyForArchival
byte[] EncryptKeyForArchival(byte[] keyToExport, string passphrase, byte[] salt)
{
RijndaelManaged archivalEncryptionAlgorithm = new RijndaelManaged();
byte[] rgbKey = this.GenerateArchivalKey(archivalEncryptionAlgorithm, passphrase, salt);
byte[] rgbIV = new byte[archivalEncryptionAlgorithm.BlockSize / 8];
return CryptographyUtility.Transform(archivalEncryptionAlgorithm.CreateEncryptor(rgbKey, rgbIV), keyToExport);
}
示例8: Criptografar
public static string Criptografar(string texto)
{
if(string.IsNullOrEmpty(texto))
return String.Empty;
string outStr;
RijndaelManaged aesAlg = null;
try
{
var key = new Rfc2898DeriveBytes(Segredo, Complemento);
aesAlg = new RijndaelManaged();
aesAlg.Key = key.GetBytes(aesAlg.KeySize / 8);
aesAlg.IV = key.GetBytes(aesAlg.BlockSize / 8);
var encryptor = aesAlg.CreateEncryptor(aesAlg.Key, aesAlg.IV);
using (var msEncrypt = new MemoryStream())
{
using (var csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write))
{
using (var swEncrypt = new StreamWriter(csEncrypt))
{
swEncrypt.Write(texto);
}
}
outStr = Convert.ToBase64String(msEncrypt.ToArray());
}
}
finally
{
if (aesAlg != null)
aesAlg.Clear();
}
return outStr;
}
示例9: encrypt
private static String encrypt (Dictionary<String, String> data, String api_key)
{
JavaScriptSerializer serializer = new JavaScriptSerializer();
String json_data = serializer.Serialize(data);
String iv = api_key.Substring(16, 16);
api_key = api_key.Substring(0, 16);
byte[] data_bytes = Encoding.UTF8.GetBytes(json_data);
byte[] api_bytes = Encoding.ASCII.GetBytes(api_key);
byte[] iv_bytes = Encoding.ASCII.GetBytes(iv);
RijndaelManaged AES = new RijndaelManaged();
AES.Padding = PaddingMode.PKCS7;
AES.Mode = CipherMode.CBC;
AES.BlockSize = 128;
AES.KeySize = 128;
MemoryStream memStream = new MemoryStream();
CryptoStream cryptoStream = new CryptoStream(memStream, AES.CreateEncryptor(api_bytes, iv_bytes), CryptoStreamMode.Write);
cryptoStream.Write(data_bytes, 0, data_bytes.Length);
cryptoStream.FlushFinalBlock();
byte[] encryptedMessageBytes = new byte[memStream.Length];
memStream.Position = 0;
memStream.Read(encryptedMessageBytes, 0, encryptedMessageBytes.Length);
string encryptedMessage = System.Convert.ToBase64String(encryptedMessageBytes);
return HttpUtility.UrlEncode(encryptedMessage);
}
示例10: AuthenticationHelper
public AuthenticationHelper()
{
var rm = new RijndaelManaged();
encryptor = rm.CreateEncryptor(key, vector);
decryptor = rm.CreateDecryptor(key, vector);
encoder = new UTF8Encoding();
}
示例11: EncryptStringAES
/// <summary>
/// AES加密
/// </summary>
/// <param name="plainText">要加密的字串</param>
/// <returns>加密后的字串</returns>
public static string EncryptStringAES(string plainText)
{
if (string.IsNullOrEmpty(plainText))
throw new ArgumentNullException("plainText");
string outStr = null;
RijndaelManaged aesAlg = null;
try
{
Rfc2898DeriveBytes key = new Rfc2898DeriveBytes(_secret, _salt);
aesAlg = new RijndaelManaged();
aesAlg.Key = key.GetBytes(aesAlg.KeySize / 8);
ICryptoTransform encryptor = aesAlg.CreateEncryptor(aesAlg.Key, aesAlg.IV);
using (MemoryStream msEncrypt = new MemoryStream())
{
msEncrypt.Write(BitConverter.GetBytes(aesAlg.IV.Length), 0, sizeof(int));
msEncrypt.Write(aesAlg.IV, 0, aesAlg.IV.Length);
using (CryptoStream csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write))
{
using (StreamWriter swEncrypt = new StreamWriter(csEncrypt))
{
swEncrypt.Write(plainText);
}
}
outStr = Convert.ToBase64String(msEncrypt.ToArray());
}
}
finally
{
if (aesAlg != null)
aesAlg.Clear();
}
return outStr;
}
示例12: AESEncrypt
/// <summary>
/// 有密码的AES加密
/// </summary>
/// <param name="text">加密字符</param>
/// <param name="password">加密的密码</param>
/// <param name="iv">密钥</param>
/// <returns></returns>
public static string AESEncrypt(string text, string password, string iv)
{
var rijndaelCipher = new RijndaelManaged
{
Mode = CipherMode.CBC,
Padding = PaddingMode.PKCS7,
KeySize = 128,
BlockSize = 128
};
var pwdBytes = System.Text.Encoding.UTF8.GetBytes(password);
var keyBytes = new byte[24];
var len = pwdBytes.Length;
if (len > keyBytes.Length) len = keyBytes.Length;
System.Array.Copy(pwdBytes, keyBytes, len);
rijndaelCipher.Key = keyBytes;
var ivBytes = System.Text.Encoding.UTF8.GetBytes(iv);
rijndaelCipher.IV = ivBytes;
var transform = rijndaelCipher.CreateEncryptor();
var plainText = Encoding.UTF8.GetBytes(text);
var cipherBytes = transform.TransformFinalBlock(plainText, 0, plainText.Length);
return Convert.ToBase64String(cipherBytes);
}
示例13: Encrypt
/// <summary>
/// Encrypts a string
/// </summary>
/// <param name="plainText">Text to be encrypted</param>
/// <param name="password">Password to encrypt with</param>
/// <param name="salt">Salt to encrypt with</param>
/// <param name="hashAlgorithm">Can be either SHA1 or MD5</param>
/// <param name="passwordIterations">Number of iterations to do. The number of times the algorithm is run on the text. </param>
/// <param name="initialVector">Needs to be 16 ASCII characters long</param>
/// <param name="keySize">Can be 128, 192, or 256</param>
/// <returns>An encrypted string</returns>
public static string Encrypt(string plainText, string password, string salt = "69ad1bfbd6605f3f6a3011460cdfb9db7757e0f9", string hashAlgorithm = "SHA1", int passwordIterations = 2, string initialVector = "OFRna73m*aze01xY", int keySize = 256)
{
if (string.IsNullOrEmpty(plainText))
return "";
byte[] initialVectorBytes = Encoding.ASCII.GetBytes(initialVector);
byte[] saltValueBytes = Encoding.ASCII.GetBytes(salt);
byte[] plainTextBytes = Encoding.UTF8.GetBytes(plainText);
var derivedPassword = new PasswordDeriveBytes(password, saltValueBytes, hashAlgorithm, passwordIterations);
byte[] keyBytes = derivedPassword.GetBytes(keySize / 8);
var symmetricKey = new RijndaelManaged {Mode = CipherMode.CBC};
byte[] cipherTextBytes;
using (ICryptoTransform encryptor = symmetricKey.CreateEncryptor(keyBytes, initialVectorBytes))
{
using (MemoryStream memStream = new MemoryStream())
{
using (CryptoStream cryptoStream = new CryptoStream(memStream, encryptor, CryptoStreamMode.Write))
{
cryptoStream.Write(plainTextBytes, 0, plainTextBytes.Length);
cryptoStream.FlushFinalBlock();
cipherTextBytes = memStream.ToArray();
memStream.Close();
cryptoStream.Close();
}
}
}
symmetricKey.Clear();
return Convert.ToBase64String(cipherTextBytes);
}
示例14: EncryptPassword
private const int IterateDecoding = 1000; //Try to think of a less terrible name for this.
public static string EncryptPassword(string plainText, string passPhrase)
{
var rand1StringBytes = Generate256BitsOfStuff(); //256 bits of random ****.
var rand2StringBytes = Generate256BitsOfStuff(); //Two * 256 bits of random ****. You'll see why!
var unencryptedTextBytes = Encoding.UTF8.GetBytes(plainText); //unencrypted plain text. I hope we're not getting marked on the conciseness of our variable declerations...
using (var password = new Rfc2898DeriveBytes(passPhrase, rand1StringBytes, IterateDecoding))
{
var keyBytes = password.GetBytes(KeySize / 8);
using (var symmetricKey = new RijndaelManaged())
{
symmetricKey.BlockSize = KeySize; //There's no need to use more RAM than we need now is there?
symmetricKey.Mode = CipherMode.CBC;
symmetricKey.Padding = PaddingMode.PKCS7;
using (var encryptor = symmetricKey.CreateEncryptor(keyBytes, rand2StringBytes))
{
using (var memoryStream = new MemoryStream()) //Using memory stream is a great way of keeping information volitile/difficult to intercept.
{
using (var cryptoStream = new CryptoStream(memoryStream, encryptor, CryptoStreamMode.Write))
{
cryptoStream.Write(unencryptedTextBytes, 0, unencryptedTextBytes.Length); //There's no point in encrypting characters that aren't there.
cryptoStream.FlushFinalBlock();
// Create the final bytes as a concatenation of the random salt bytes, the random iv bytes and the cipher bytes.
var cipherTextBytes = rand1StringBytes;
cipherTextBytes = cipherTextBytes.Concat(rand2StringBytes).ToArray(); //Stores how we've encrypted everything in an array so we can decrypt it when needed.
cipherTextBytes = cipherTextBytes.Concat(memoryStream.ToArray()).ToArray();
memoryStream.Close();
cryptoStream.Close();
return Convert.ToBase64String(cipherTextBytes);
}
}
}
}
}
} //End of encrypt method.
示例15: encryptFile
private void encryptFile(string inputFile, string cryptFile)
{
FileStream fsCrypt = null;
CryptoStream cryptStream = null;
try
{
UnicodeEncoding UE = new UnicodeEncoding();
byte[] key = UE.GetBytes(TPW);
fsCrypt = new FileStream(cryptFile, FileMode.Create);
RijndaelManaged RMCrypto = new RijndaelManaged();
cryptStream = new CryptoStream(fsCrypt,
RMCrypto.CreateEncryptor(key, key),
CryptoStreamMode.Write);
byte[] inBytes = File.ReadAllBytes(inputFile);
cryptStream.Write(inBytes, 0, inBytes.Length);
}
finally
{
if (cryptStream != null)
cryptStream.Close();
if ( fsCrypt != null )
fsCrypt.Close();
}
}