【发布时间】:2018-06-21 10:41:58
【问题描述】:
我想在 .net core 1.1 中进行 AES256 加密。 RijndaelManaged 不支持 .net core 1.1。所以我在这里使用AES aes = new AES.create()
这部分代码创建用于加密的随机私钥
public string GenaratePassPharse()
{
RandomNumberGenerator rngCryptoServiceProvider = RandomNumberGenerator.Create();
byte[] randomBytes = new byte[KEY_SIZE];
rngCryptoServiceProvider.GetBytes(randomBytes);
string plainPassPharse = Convert.ToBase64String(randomBytes);
return plainPassPharse;
}
这里是 AES() 加密方法。我想要做的是传递我生成的密钥(从上述方法返回)而不是 aesAlg.Key 作为加密密钥。
static byte[] EncryptStringToBytes_Aes(string plainText, byte[] Key, byte[] IV)
{
// Check arguments.
if (plainText == null || plainText.Length <= 0)
throw new ArgumentNullException("plainText");
if (Key == null || Key.Length <= 0)
throw new ArgumentNullException("Key");
if (IV == null || IV.Length <= 0)
throw new ArgumentNullException("IV");
byte[] encrypted;
// Create an Aes object
// with the specified key and IV.
using (Aes aesAlg = Aes.Create())
{
aesAlg.Key = Key;
aesAlg.IV = IV;
aesAlg.BlockSize = 128;
aesAlg.KeySize = 128;
// Create an encryptor to perform the stream transform.
ICryptoTransform encryptor = aesAlg.CreateEncryptor(aesAlg.Key, aesAlg.IV);
// Create the streams used for encryption.
using (MemoryStream msEncrypt = new MemoryStream())
{
using (CryptoStream csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write))
{
using (StreamWriter swEncrypt = new StreamWriter(csEncrypt))
{
//Write all data to the stream.
swEncrypt.Write(plainText);
}
encrypted = msEncrypt.ToArray();
}
}
}
// Return the encrypted bytes from the memory stream.
return encrypted;
}
还有其他使用 AES256 加密算法的方法吗?
【问题讨论】:
-
在
EncryptStringToBytes_Aes中使用byte[]时不清楚为什么要使用Convert.ToBase64String... 你有什么问题?有用吗? -
好的,场景是我需要为用户随机创建密码,并且所有密码都有唯一的私钥来解密该加密密码。并且存储在数据库中的加密密码和存储在 Azure 中的生成的私钥
-
Azure 密钥保管库。这就是为什么我要传递创建的私钥。密钥长度应为 128。AES() 不提供设置密钥长度为 128
-
AES类确实支持 128 位和 256 位密钥长度... -
所以请你提供示例。我搜索了微软网站,但我没有得到任何设置密钥长度的示例
标签: c# cryptography aes .net-core-1.1