本文整理汇总了C#中System.Security.Cryptography.AesManaged.GenerateIV方法的典型用法代码示例。如果您正苦于以下问题:C# AesManaged.GenerateIV方法的具体用法?C# AesManaged.GenerateIV怎么用?C# AesManaged.GenerateIV使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Security.Cryptography.AesManaged
的用法示例。
在下文中一共展示了AesManaged.GenerateIV方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: 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 AesManaged { 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;
}
示例2: Encrypt
/// <summary>
/// <see cref="IAesManagedWrapper.Encrypt"/>
/// </summary>
public byte[] Encrypt(byte[] bytesToBeEncrypted, byte[] aesKey, out byte[] aesIv)
{
byte[] encryptedBytes;
using (var ms = new MemoryStream())
{
using (var aes = new AesManaged())
{
SetStandardConfiguration(aes);
aes.Key = aesKey;
aes.GenerateIV();
aesIv = aes.IV;
using (var cs = new CryptoStream(ms, aes.CreateEncryptor(), CryptoStreamMode.Write))
{
cs.Write(bytesToBeEncrypted, 0, bytesToBeEncrypted.Length);
cs.Close();
}
encryptedBytes = ms.ToArray();
}
}
return encryptedBytes;
}
示例3: CreateNewAESSymmetricKeyset
/// <summary>
/// Creates a symmetric key. See this link for more information behind the numbers
/// http://blogs.msdn.com/b/shawnfa/archive/2006/10/09/the-differences-between-rijndael-and-aes.aspx
/// </summary>
/// <returns></returns>
private SymmetricKey CreateNewAESSymmetricKeyset()
{
if (cert == null)
{
throw new InvalidOperationException("Unable to create new AES keyset; Certificate not loaded.");
}
byte[] symmKey, iv;
using (AesManaged aes = new AesManaged())
{
aes.GenerateIV();
aes.GenerateKey();
symmKey = aes.Key;
iv = aes.IV;
aes.Clear();
}
// Encrypt the Symmetric Key for storage
byte[] encryptedKey = EncryptRSA(symmKey, cert);
SymmetricKey symmKeySet = new SymmetricKey() {
iv = iv,
Key = encryptedKey,
CertificateThumbprint = cert.Thumbprint
};
return symmKeySet;
}
示例4: Encrypt
public EncryptedMessage Encrypt(byte[] input)
{
var em = new EncryptedMessage();
using (var aes = new AesManaged())
{
aes.KeySize = 256;
aes.GenerateIV();
aes.GenerateKey();
em.IV = aes.IV;
em.Key = aes.Key;
em.EncryptionType = MessageEncryptionType.Aes;
using (var encryptor = aes.CreateEncryptor())
{
using (var msOutput = new MemoryStream())
{
using (var cryptoStream = new CryptoStream(msOutput, encryptor, CryptoStreamMode.Write)) {
using (var msInput = new MemoryStream(input)) {
msInput.CopyTo(cryptoStream);
}
}
em.CipherBytes = msOutput.ToArray();
}
}
return em;
}
}
示例5: GenerateNewAesIv
/// <summary>
/// Generates a new random Initialization vector.
/// This can be used for the encryption of keys in the HOTP/TOTP classes
/// </summary>
/// <returns>returns a new Initialization vector</returns>
public static byte[] GenerateNewAesIv()
{
using (AesManaged aesAlg = new AesManaged())
{
aesAlg.GenerateIV();
return aesAlg.IV;
}
}
示例6: AESEncryption
public AESEncryption()
{
Algorithm = new AesManaged();
Algorithm.KeySize = 256;
Algorithm.Mode = CipherMode.CBC;
Algorithm.GenerateIV();
Algorithm.GenerateKey();
Algorithm.Padding = PaddingMode.PKCS7;
}
示例7: GenerateEncryptionParameters
private void GenerateEncryptionParameters(X509Certificate2 certificate)
{
var aesManaged = new AesManaged();
aesManaged.GenerateIV();
aesManaged.GenerateKey();
string ivToStoreInConfig = Convert.ToBase64String(aesManaged.IV);
string encryptionKeyAsString = Convert.ToBase64String(aesManaged.Key);
string keyToStoreInConfig = RsaEncrypt(certificate, encryptionKeyAsString);
ConfigurationManager.AppSettings["EncryptionIV"] = ivToStoreInConfig;
ConfigurationManager.AppSettings["EncryptionKey"] = keyToStoreInConfig;
}
示例8: CreateNewAesSymmetricKeyset
public SymmetricKey CreateNewAesSymmetricKeyset()
{
var aes = new AesManaged();
aes.GenerateIV();
aes.GenerateKey();
var symmKeySet = new SymmetricKey()
{
Iv = RsaEncryptBytes(aes.IV),
Key = RsaEncryptBytes(aes.Key)
};
return symmKeySet;
}
示例9: TokenCrypt
static TokenCrypt()
{
try
{
CryptKey = JsonHelper.ParseJson<AesKey>(File.ReadAllText(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, KeyFile)).Trim());
}
catch(FileNotFoundException)
{
using(var aes = new AesManaged())
{
aes.GenerateKey();
aes.GenerateIV();
CryptKey = new AesKey { Key = aes.Key, IV = aes.IV };
File.WriteAllText(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, KeyFile), CryptKey.ToJsonString());
}
}
}
示例10: EncryptString
public static byte[] EncryptString(string text, string secret, string salt)
{
byte[] buffer;
using (Aes aes = new AesManaged())
{
aes.Key = new Rfc2898DeriveBytes(secret, Encoding.UTF8.GetBytes(salt)).GetBytes(0x10);
aes.GenerateIV();
using (MemoryStream stream = new MemoryStream())
{
stream.Write(BitConverter.GetBytes(aes.IV.Length), 0, 4);
stream.Write(aes.IV, 0, aes.IV.Length);
using (CryptoStream stream2 = new CryptoStream(stream, aes.CreateEncryptor(), CryptoStreamMode.Write))
{
stream2.Write(Encoding.UTF8.GetBytes(text), 0, text.Length);
stream2.FlushFinalBlock();
buffer = stream.ToArray();
}
}
}
return buffer;
}
示例11: Encrypt
/**
* Encrypts a specified string using AES encryption and returns
* the encrypted string and key used.
*/
public static string[] Encrypt(string str)
{
byte[] bytes = Encoding.UTF8.GetBytes(str);
var manager = new AesManaged
{
KeySize = 256,
Padding = PaddingMode.PKCS7
};
manager.GenerateKey();
manager.GenerateIV();
ICryptoTransform c = manager.CreateEncryptor();
bytes = c.TransformFinalBlock(bytes, 0, bytes.Length);
string hash = BitConverter.ToString(manager.IV).Replace("-", "");
hash += BitConverter.ToString(bytes).Replace("-", "");
string key = BitConverter.ToString(manager.Key).Replace("-", "");
return new[] {hash, key};
}
示例12: AESCipher
public AESCipher()
{
this.utf8Encoding = new System.Text.UTF8Encoding();
this.aes = new AesManaged();
this.aes.Mode = CipherMode.CBC;
this.aes.Padding = PaddingMode.PKCS7;
this.aes.KeySize = 128;
this.aes.BlockSize = 128;
aes.GenerateKey();
aes.GenerateIV();
if (File.Exists(@"C:\Users\Chris\Documents\Cambridge\Part II Project\Code\Encryption Example\EncryptionExample\AESkey.xml") == true)
File.Delete(@"C:\Users\Chris\Documents\Cambridge\Part II Project\Code\Encryption Example\EncryptionExample\AESkey.xml");
//provide AES encryption key params
FileStream fs = new FileStream(@"C:\Users\Chris\Documents\Cambridge\Part II Project\Code\Encryption Example\EncryptionExample\AESkey.xml", FileMode.CreateNew, FileAccess.ReadWrite);
StreamWriter sw = new StreamWriter(fs);
string aesKeyXML = byte2Hex(aes.Key);
sw.Write("<AESkey>"+ aesKeyXML + "</AESkey>");
sw.Close();
fs.Close();
}
示例13: EncryptStringToByte
public static byte[] EncryptStringToByte(string sourceText,ref byte[] key,ref byte[] IV)
{
if (sourceText == null || sourceText == "")
{
return null;
}
byte[] result;
AesManaged aesAlg = new AesManaged();
if (key == null || key.Length <= 0 || IV == null || IV.Length <= 0)
{
aesAlg.GenerateKey();
aesAlg.GenerateIV();
key = aesAlg.Key;
IV = aesAlg.IV;
}
else
{
aesAlg.Key = key;
aesAlg.IV = IV;
}
ICryptoTransform encryptor = aesAlg.CreateEncryptor();
MemoryStream msEncrypt = new MemoryStream();
CryptoStream csEncrypt = new CryptoStream(msEncrypt,encryptor, CryptoStreamMode.Write);
StreamWriter swEncrypt = new StreamWriter(csEncrypt);
swEncrypt.Write(sourceText);
swEncrypt.Close();
csEncrypt.Close();
result = msEncrypt.ToArray();
return result;
}
示例14: RsaHelperBytesShouldSucceed
public void RsaHelperBytesShouldSucceed()
{
var directory = TestContext.DeploymentDirectory;
var helper = new RsaHelper(Path.Combine(directory, "TestCertificate.pfx"), CertificatePassword);
var aes = new AesManaged();
aes.GenerateIV();
aes.GenerateKey();
var originalKey = aes.Key;
var originalIv = aes.IV;
var encryptedKeyBytes = helper.RsaEncryptBytes(aes.Key);
var encryptedIvBytes = helper.RsaEncryptBytes(aes.IV);
encryptedIvBytes.Should().NotBeNull("IV failed to encrypt");
encryptedKeyBytes.Should().NotBeNull("Key failed to encrypt");
var decryptedKeyBytes = helper.RsaDecryptToBytes(encryptedKeyBytes);
var decryptedIvBytes = helper.RsaDecryptToBytes(encryptedIvBytes);
originalKey.ShouldBeEquivalentTo(decryptedKeyBytes);
originalIv.ShouldBeEquivalentTo(decryptedIvBytes);
}
示例15: SecurityHelper
public SecurityHelper()
{
rijAlg = new AesManaged();
rijAlg.GenerateIV();
IV = rijAlg.IV;
KEY = Convert.FromBase64String(EncodeTo64(key));
rijAlg.Key = KEY;
//try
//{
// string original = "Here is some data to encrypt!";
// // Create a new instance of the AesManaged
// // class. This generates a new key and initialization
// // vector (IV).
// using (AesManaged myAes = new AesManaged())
// {
// myAes.Key = Convert.FromBase64String(EncodeTo64(key));
// // Encrypt the string to an array of bytes.
// byte[] encrypted = EncryptStringToBytes_Aes(original, myAes.Key, myAes.IV);
// // Decrypt the bytes to a string.
// string roundtrip = DecryptStringFromBytes_Aes(encrypted, myAes.Key, myAes.IV);
// //Display the original data and the decrypted data.
// Debug.WriteLine("Original: {0}", original);
// Debug.WriteLine("Round Trip: {0}", roundtrip);
// }
//}
//catch (Exception e)
//{
// Debug.WriteLine("Error: {0}", e.Message);
//}
}