本文整理汇总了C#中System.Security.Cryptography.AesManaged.GenerateKey方法的典型用法代码示例。如果您正苦于以下问题:C# AesManaged.GenerateKey方法的具体用法?C# AesManaged.GenerateKey怎么用?C# AesManaged.GenerateKey使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Security.Cryptography.AesManaged
的用法示例。
在下文中一共展示了AesManaged.GenerateKey方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: 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;
}
示例2: DecryptsToOriginalPlainText
public void DecryptsToOriginalPlainText()
{
byte[] plaintextBytes = Encoding.UTF8.GetBytes("This is a test! It needs to be 128 characters long at least. This is a test! It needs to be 128 characters long at least. This is a test! It needs to be 128 characters long at least. This is a test! It needs to be 128 characters long at least. This is a test! It needs to be 128 characters long at least. This is a test! It needs to be 128 characters long at least. This is a test! It needs to be 128 characters long at least. This is a test! It needs to be 128 characters long at least.");
byte[] decryptedBytes;
using (SymmetricAlgorithm algorithm = new AesManaged())
{
byte[] wrongDecryptionKey = algorithm.Key;
algorithm.GenerateKey();
byte[] encryptionKey = algorithm.Key;
Assert.AreNotEqual(encryptionKey, wrongDecryptionKey);
byte[] ciphertextBytes, iv;
using (Encryptor encryptor = algorithm.CreateEncryptor(encryptionKey, out iv))
{
Assert.AreEqual(encryptionKey, encryptor.Algorithm.Key);
Assert.AreEqual(iv, encryptor.Algorithm.IV);
ciphertextBytes = encryptor.Encrypt(plaintextBytes);
}
using (Decryptor decryptor = new Decryptor(algorithm, encryptionKey, iv, Encryption.DefaultOptions))
{
Assert.AreEqual(encryptionKey, decryptor.Algorithm.Key);
Assert.AreEqual(iv, decryptor.Algorithm.IV);
decryptedBytes = decryptor.Decrypt(ciphertextBytes);
}
}
Assert.AreEqual(plaintextBytes, decryptedBytes);
}
示例3: 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;
}
}
示例4: AESEncryption
public AESEncryption()
{
Algorithm = new AesManaged();
Algorithm.KeySize = 256;
Algorithm.Mode = CipherMode.CBC;
Algorithm.GenerateIV();
Algorithm.GenerateKey();
Algorithm.Padding = PaddingMode.PKCS7;
}
示例5: GenerateAESKey
public static byte[] GenerateAESKey()
{
using (var provider = new AesManaged())
{
provider.KeySize = 256;
provider.GenerateKey();
return provider.Key;
}
}
示例6: GenerateNewAesKey
/// <summary>
/// Generates a new random 256bit AES key.
/// This can be used for the encryption of keys in the HOTP/TOTP classes
/// </summary>
/// <returns>256bit AES key</returns>
public static byte[] GenerateNewAesKey()
{
using (AesManaged aesAlg = new AesManaged())
{
aesAlg.KeySize = 256;
aesAlg.GenerateKey();
return aesAlg.Key;
}
}
示例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: GenerateAESKeys
private static void GenerateAESKeys()
{
var sw = Stopwatch.StartNew();
var aesM = new AesManaged();
for (int i = 0; i < NUM_AES_KEYS; i++)
{
aesM.GenerateKey();
byte[] result = aesM.Key;
string hexString = ConvertToHexString(result);
//Console.WriteLine("AES KEY{0}",hexString);
}
// Console.WriteLine("AES KEY:" + sw.Elapsed.ToString());
}
示例9: 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;
}
示例10: 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());
}
}
}
示例11: UnityCryptoManager
public UnityCryptoManager()
{
m_RSA_Exch_Formatter = new RiaWebSoftRu.Cryptography.Crypto.Formater.RSAESPKCSFormater();
m_RSA = new RiaWebSoftRu.Cryptography.Crypto.RsaManaged(m_KeySize);
m_SHA = new SHA256Managed();
m_RIJ = new AesManaged();
//m_RSA.GenerateKey(); Not necessary in unity. Unity clients gets a public key from server and uses that to encrypt the Rijndael key
m_SHA = new SHA256Managed();
m_RIJ.Padding = PaddingMode.PKCS7;
m_RIJ.Mode = CipherMode.CBC;
m_RIJ.IV = new byte[16];
m_RIJ.BlockSize = 128;
m_RIJ.KeySize = 256;
m_RIJ.GenerateKey();
m_PublicKey = Encoding.UTF8.GetBytes(m_RSA.ToXmlString(false).ToString());
m_PrivateKey = Encoding.UTF8.GetBytes(m_RSA.ToXmlString(true).ToString());
}
示例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: 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};
}
示例14: 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;
}
示例15: 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);
}