本文整理汇总了C#中System.Security.Cryptography.RijndaelManaged.GenerateIV方法的典型用法代码示例。如果您正苦于以下问题:C# RijndaelManaged.GenerateIV方法的具体用法?C# RijndaelManaged.GenerateIV怎么用?C# RijndaelManaged.GenerateIV使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Security.Cryptography.RijndaelManaged
的用法示例。
在下文中一共展示了RijndaelManaged.GenerateIV方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: PolyAESEncrypt
//Copyright (C) 2011, Dextrey (0xDEADDEAD)
public static byte[] PolyAESEncrypt(byte[] plainText, string Key)
{
byte[] salt;
SymmetricAlgorithm algo = new RijndaelManaged();
RNGCryptoServiceProvider rngAlgo = new RNGCryptoServiceProvider();
algo.Mode = CipherMode.CBC;
byte[] key = System.Text.Encoding.ASCII.GetBytes(Key);
algo.GenerateIV();
salt = new byte[32];
rngAlgo.GetBytes(salt);
Rfc2898DeriveBytes pwDeriveAlg = new Rfc2898DeriveBytes(key, salt, 2000);
algo.Key = pwDeriveAlg.GetBytes(32);
ICryptoTransform encTransform = algo.CreateEncryptor();
byte[] enced = encTransform.TransformFinalBlock(plainText, 0, plainText.Length);
int origLength = enced.Length;
Array.Resize(ref enced, enced.Length + salt.Length);
Buffer.BlockCopy(salt, 0, enced, origLength, salt.Length);
origLength = enced.Length;
Array.Resize(ref enced, enced.Length + algo.IV.Length);
Buffer.BlockCopy(algo.IV, 0, enced, origLength, algo.IV.Length);
return enced;
}
示例2: AESEncryptStream
public AESEncryptStream(byte[] aesKey, Stream data, bool readMode)
{
if (readMode && !data.CanRead)
{
throw new ArgumentException("Underlying stream is not readable", "data");
}
if (!readMode && !data.CanWrite)
{
throw new ArgumentException("Underlying stream is not writable", "data");
}
_internalStream = data;
_isRead = readMode;
var aesProvider = new RijndaelManaged();
aesProvider.Key = aesKey;
aesProvider.GenerateIV();
var encryptor = aesProvider.CreateEncryptor();
_cryptoStream = new CryptoStream(data, encryptor, _isRead ? CryptoStreamMode.Read : CryptoStreamMode.Write);
_initialBytesWritten = 0;
_initialBytes = new byte[aesProvider.IV.Length + 4];
Buffer.BlockCopy(BitConverter.GetBytes(aesProvider.IV.Length), 0, _initialBytes, 0, 4);
Buffer.BlockCopy(aesProvider.IV, 0, _initialBytes, 4, aesProvider.IV.Length);
}
示例3: Encrypt
public IConfigCommand Encrypt(String password) {
// Hard error if no password is passed through, but the data is requested to be encrypted.
if (password == null || password.Length <= 0) throw new ArgumentNullException("password");
using (RijndaelManaged managed = new RijndaelManaged()) {
// Generate a new salt.
byte[] salt = this.GenerateSalt();
this.Salt = Convert.ToBase64String(salt);
// Generate new vector.
managed.GenerateIV();
this.Vector = Convert.ToBase64String(managed.IV);
// Derive a key
byte[] key = this.DeriveKey(password, salt);
using (StringWriter writer = new StringWriter()) {
Potato.Core.Shared.Serialization.JsonSerialization.Minimal.Serialize(writer, this.Command);
byte[] text = Encoding.UTF8.GetBytes(writer.ToString());
using (ICryptoTransform transform = managed.CreateEncryptor(key, managed.IV)) {
this.Data = Convert.ToBase64String(this.CryptoTransform(text, transform));
}
}
}
// Don't store the unencrypted data.
this.Command = null;
return this;
}
示例4: Cifrar
public static String Cifrar(String contenido, String clave)
{
//tipo de codificacion
var encoding = new UTF8Encoding();
//determina cual es el algoritmo de cifrado
var cripto = new RijndaelManaged();
//texto q yo quiero cifrar
byte[] cifrado;
//array que contiene el IV mas el txt cifrado
byte[] retorno;
//convierte la clave en array de bytes
byte[] key = GeyKey(clave);
cripto.Key = key;
//genera array de inicializacion
cripto.GenerateIV();
//el texto a encriptar lo convierte en array de byte
byte[] aEncriptar = encoding.GetBytes(contenido);
//con el IV y la clave crea un encriptador al final del bloque( el q transforma, dnd empieza, dnd termina)
cifrado = cripto.CreateEncryptor().TransformFinalBlock(aEncriptar, 0, aEncriptar.Length);
//prepara lo q va a devolver. Array de bytes q esta compuesto de la longitud del IV mas la longitud de lo q he cifrado
retorno = new byte[cripto.IV.Length + cifrado.Length];
//me copias el array del IV empezando en la longitud 0
cripto.IV.CopyTo(retorno, 0);
cifrado.CopyTo(retorno, cripto.IV.Length);
return Convert.ToBase64String(retorno);
}
示例5: GenerateIV
public static byte[] GenerateIV()
{
RijndaelManaged provider = new RijndaelManaged();
provider.GenerateIV();
return provider.IV;
}
示例6: Cifrar
/// <summary>
/// Recibe un texto plano y lo devuelve cifrado
/// Cifrado Simetrico
/// </summary>
/// <param name="contenido"></param>
/// <param name="clave"></param>
/// <returns></returns>
public static String Cifrar(String contenido, String clave)
{
var encoding = new UTF8Encoding();
var cripto = new RijndaelManaged(); // es un algotimo de cifrado, para cifrar
//var iv = cripto.GenerateIV(); // Vector de inicialización: es un vector que tiene la semilla de inicialización
// Este IV
byte[] cifrado;
byte[] retorno;
byte[] key = GetKey(clave); // recibo una clave alfanumerica y devuelvo los bytes desde una cadena UTF8
cripto.Key = key;
cripto.GenerateIV(); // genera numeros aleatorios (semillas)
// voy a crear el encriptador
byte[] aEncriptar = encoding.GetBytes(contenido); // recibo contenido y lo convierto a array de bites
// ya lo tengo cifrado
cifrado = cripto.CreateEncryptor().TransformFinalBlock(aEncriptar, 0, aEncriptar.Length); // transforma el contenido desde 0 hasta que termine
// creo mi retorno
retorno = new byte[cripto.IV.Length + cifrado.Length]; // longitud del IV + el tamaño del cifrado
cripto.IV.CopyTo(retorno, 0); // donde quiero copiar, en que posición quiero copiar
cifrado.CopyTo(retorno, cripto.IV.Length);
// la mejor forma es convertirlo a base 64, datos binarios, para almacenar array de bytes
return Convert.ToBase64String(retorno); // conjunto de bytes transformados en string
// muy util para guardar imagenes
}
示例7: Cifrar
public static string Cifrar(String contenido, String clave)
{
//para codificar el texto que quiero cifrar
var encoding = new UTF8Encoding();
var cripto=new RijndaelManaged();
byte[] cifrado;
byte[] retorno;
//clave generada en bytes
byte[] key = GetKey(clave);
//asigno la clave y ek IV
cripto.Key = key;
cripto.GenerateIV();
byte[] aEncriptar = encoding.GetBytes(contenido);
//Creo un encriptador CreateEncryptor() el array aEncriptar desde posicion 0
cifrado = cripto.CreateEncryptor().TransformFinalBlock(aEncriptar, 0, aEncriptar.Length);
//el array retorno sera = la suma del vector mas longitud del cifrado
retorno = new byte[cripto.IV.Length + cifrado.Length];
//copias IV desde la posicion 0
cripto.IV.CopyTo(retorno,0);
cifrado.CopyTo(retorno,cripto.IV.Length);
//convierto la cadena en base 64, por ejemplo una imagen, word, etc
//contienetodo el flujo de datos
return Convert.ToBase64String(retorno);
}
示例8: Encrypt
public EncryptedValue Encrypt(string value)
{
using (var rijndael = new RijndaelManaged())
{
rijndael.Key = Key;
rijndael.Mode = CipherMode.CBC;
rijndael.GenerateIV();
using (var encryptor = rijndael.CreateEncryptor())
using (var memoryStream = new MemoryStream())
using (var cryptoStream = new CryptoStream(memoryStream, encryptor, CryptoStreamMode.Write))
using (var writer = new StreamWriter(cryptoStream))
{
writer.Write(value);
writer.Flush();
cryptoStream.Flush();
cryptoStream.FlushFinalBlock();
return new EncryptedValue
{
EncryptedBase64Value = Convert.ToBase64String(memoryStream.ToArray()),
Base64Iv = Convert.ToBase64String(rijndael.IV)
};
}
}
}
示例9: CryptoIV
public CryptoIV()
{
RijndaelManaged symmetricAlgorithm = new RijndaelManaged();
symmetricAlgorithm.GenerateIV();
_protectedIV = CryptoUtils.ProtectString(Convert.ToBase64String(symmetricAlgorithm.IV));
KeySize = symmetricAlgorithm.BlockSize / 8;
}
示例10: Save
public static IVCryptoStream Save(string path, byte[] key, byte[] iv)
{
FileStream file = new FileStream(path, FileMode.Create);
RijndaelManaged crypt = new RijndaelManaged();
crypt.Key = key;
if (iv == null)
crypt.GenerateIV();
else
{
Debug.Assert(iv.Length == crypt.IV.Length);
crypt.IV = iv;
}
try
{
file.Write(crypt.IV, 0, crypt.IV.Length);
}
catch
{
file.Dispose();
}
return new IVCryptoStream(file, crypt.CreateEncryptor(), CryptoStreamMode.Write);
}
示例11: GenerateEncryptionVector
/// Generates a unique encryption vector
public static byte[] GenerateEncryptionVector()
{
//Generate a Vector
RijndaelManaged rm = new RijndaelManaged();
rm.GenerateIV();
return rm.IV;
}
示例12: Cifrar
public static String Cifrar(String contenido, String clave)
{
var encoding = new UTF8Encoding();
var cripto = new RijndaelManaged();
//contenido cifrado
byte[] cifrado;
//array que contiene IV + texto cifrado
byte[] retorno;
byte[] key = GetKey(clave);
cripto.Key = key;
//IV es el patron a partir del cual el algoritmo genera el cifrado
cripto.GenerateIV();
byte[] aEncriptar = encoding.GetBytes(contenido);
cifrado = cripto.CreateEncryptor().TransformFinalBlock(aEncriptar, 0, aEncriptar.Length);
retorno = new byte[cripto.IV.Length + cifrado.Length];
cripto.IV.CopyTo(retorno, 0);
cifrado.CopyTo(retorno, cripto.IV.Length);
return Convert.ToBase64String(retorno);
}
示例13: Encrypt
/// encrypt a string message using a secret key that is known to both sender and recipient only;
/// need to give the initialization vector to the recipient as well;
static public bool Encrypt(byte[] ASecretKey, string AMessage, out string AEncryptedMessage, out string AInitializationVector)
{
Rijndael alg = new RijndaelManaged();
alg.Key = ASecretKey;
alg.GenerateIV();
MemoryStream ms = new MemoryStream();
CryptoStream encryptStream = new CryptoStream(
ms,
alg.CreateEncryptor(),
CryptoStreamMode.Write);
System.Text.UTF8Encoding enc = new System.Text.UTF8Encoding();
byte[] toEncryptBytes = enc.GetBytes(AMessage);
encryptStream.Write(toEncryptBytes, 0, toEncryptBytes.Length);
encryptStream.Close();
AEncryptedMessage = Convert.ToBase64String(ms.ToArray());
AInitializationVector = Convert.ToBase64String(alg.IV);
return true;
}
示例14: Encrypt
public static byte[] Encrypt(byte[] input)
{
if (_key == null || _key.Length == 0) throw new Exception("Key can not be empty.");
if (input == null || input.Length == 0) throw new ArgumentException("Input can not be empty.");
byte[] data = input, encdata = new byte[0];
try
{
using (var ms = new MemoryStream())
{
using (var rd = new RijndaelManaged { Key = _key })
{
rd.GenerateIV();
using (var cs = new CryptoStream(ms, rd.CreateEncryptor(), CryptoStreamMode.Write))
{
ms.Write(rd.IV, 0, rd.IV.Length); // write first 16 bytes IV, followed by encrypted message
cs.Write(data, 0, data.Length);
}
}
encdata = ms.ToArray();
}
}
catch
{
}
return encdata;
}
示例15: InvalidOperationException
EncryptedValue IEncryptionService.Encrypt(string value)
{
if (Key == null)
throw new InvalidOperationException("Cannot encrypt because a Key was not configured. Please specify 'RijndaelEncryptionServiceConfig' in your application's configuration file.");
using (var rijndael = new RijndaelManaged())
{
rijndael.Key = Key;
rijndael.Mode = CipherMode.CBC;
rijndael.GenerateIV();
using (var encryptor = rijndael.CreateEncryptor())
using (var memoryStream = new MemoryStream())
using (var cryptoStream = new CryptoStream(memoryStream, encryptor, CryptoStreamMode.Write))
using (var writer = new StreamWriter(cryptoStream))
{
writer.Write(value);
writer.Flush();
cryptoStream.Flush();
cryptoStream.FlushFinalBlock();
return new EncryptedValue
{
EncryptedBase64Value = Convert.ToBase64String(memoryStream.ToArray()),
Base64Iv = Convert.ToBase64String(rijndael.IV)
};
}
}
}