私はこのコードをAES暗号化に使用していますが、このコードが良くて間違っていないことを確認できる人もいますか?それは正常に動作しますが、私はアルゴリズムの実装についてより心配しています。C#で自分のAES暗号化コードを改善する方法はありますか?
// Plaintext value to be encrypted.
//Passphrase from which a pseudo-random password will be derived.
//The derived password will be used to generate the encryption key.
//Password can be any string. In this example we assume that this passphrase is an ASCII string.
//Salt value used along with passphrase to generate password.
//Salt can be any string. In this example we assume that salt is an ASCII string.
//HashAlgorithm used to generate password. Allowed values are: "MD5" and "SHA1".
//SHA1 hashes are a bit slower, but more secure than MD5 hashes.
//PasswordIterations used to generate password. One or two iterations should be enough.
//InitialVector (or IV). This value is required to encrypt the first block of plaintext data.
//For RijndaelManaged class IV must be exactly 16 ASCII characters long.
//KeySize. Allowed values are: 128, 192, and 256.
//Longer keys are more secure than shorter keys.
//Encrypted value formatted as a base64-encoded string.
public static string Encrypt(string PlainText, string Password, string Salt, string HashAlgorithm, int PasswordIterations, string InitialVector, int KeySize)
{
byte[] InitialVectorBytes = Encoding.ASCII.GetBytes(InitialVector);
byte[] SaltValueBytes = Encoding.ASCII.GetBytes(Salt);
byte[] PlainTextBytes = Encoding.UTF8.GetBytes(PlainText);
PasswordDeriveBytes DerivedPassword = new PasswordDeriveBytes(Password, SaltValueBytes, HashAlgorithm, PasswordIterations);
byte[] KeyBytes = DerivedPassword.GetBytes(KeySize/8);
RijndaelManaged SymmetricKey = new RijndaelManaged();
SymmetricKey.Mode = CipherMode.CBC;
ICryptoTransform Encryptor = SymmetricKey.CreateEncryptor(KeyBytes, InitialVectorBytes);
MemoryStream MemStream = new MemoryStream();
CryptoStream CryptoStream = new CryptoStream(MemStream, Encryptor, CryptoStreamMode.Write);
CryptoStream.Write(PlainTextBytes, 0, PlainTextBytes.Length);
CryptoStream.FlushFinalBlock();
byte[] CipherTextBytes = MemStream.ToArray();
MemStream.Close();
CryptoStream.Close();
return Convert.ToBase64String(CipherTextBytes);
}
public static string Decrypt(string CipherText, string Password, string Salt, string HashAlgorithm, int PasswordIterations, string InitialVector, int KeySize)
{
byte[] InitialVectorBytes = Encoding.ASCII.GetBytes(InitialVector);
byte[] SaltValueBytes = Encoding.ASCII.GetBytes(Salt);
byte[] CipherTextBytes = Convert.FromBase64String(CipherText);
PasswordDeriveBytes DerivedPassword = new PasswordDeriveBytes(Password, SaltValueBytes, HashAlgorithm, PasswordIterations);
byte[] KeyBytes = DerivedPassword.GetBytes(KeySize/8);
RijndaelManaged SymmetricKey = new RijndaelManaged();
SymmetricKey.Mode = CipherMode.CBC;
ICryptoTransform Decryptor = SymmetricKey.CreateDecryptor(KeyBytes, InitialVectorBytes);
MemoryStream MemStream = new MemoryStream(CipherTextBytes);
CryptoStream cryptoStream = new CryptoStream(MemStream, Decryptor, CryptoStreamMode.Read);
byte[] PlainTextBytes = new byte[CipherTextBytes.Length];
int ByteCount = cryptoStream.Read(PlainTextBytes, 0, PlainTextBytes.Length);
MemStream.Close();
cryptoStream.Close();
return Encoding.UTF8.GetString(PlainTextBytes, 0, ByteCount);
}
私はプロのセキュリティの男やプロのプログラマーではないよ、私は学習を開始し、私はそれを
私の計画を理解するのが大好きですが、まだ次の規格を、最小限の入力を必要とAES暗号化機能を構築することですPHPやJavaScriptのような他の言語で同等の機能を実現するのは簡単です!
ありがとうございました
また、IVに関するコメントも塩のために保持されています。 –
ありがとうございます。私はあなたの提案をコードにどのように翻訳していますか? –