mirror of
https://github.com/Crypto-Notepad/Crypto-Notepad.git
synced 2026-03-11 08:55:25 +00:00
Merge pull request #15 from h5p9sl/master
Fixed RNG generating null-bytes in metadata; causing read errors.
This commit is contained in:
commit
268499286e
1 changed files with 38 additions and 5 deletions
|
|
@ -83,12 +83,43 @@ public bool GetMetadata(byte[] rawData)
|
||||||
|
|
||||||
class AES
|
class AES
|
||||||
{
|
{
|
||||||
|
private static bool ByteArrayIsNonZero(byte[] byte_array)
|
||||||
|
{
|
||||||
|
foreach (byte i in byte_array)
|
||||||
|
{
|
||||||
|
if (i == 0)
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
// The purpose of this function is to ensure that the bytes generated by it are nonzero 100% of the time
|
||||||
|
// as there was a strange bug in which RNGCryptoServiceProvider.GetNonZeroBytes() would return a null byte sometimes
|
||||||
|
private static byte[] GenerateSecureNonZeroByteArray(int length)
|
||||||
|
{
|
||||||
|
byte[] result = new byte[length];
|
||||||
|
|
||||||
|
using (RNGCryptoServiceProvider rng = new RNGCryptoServiceProvider())
|
||||||
|
{
|
||||||
|
do
|
||||||
|
{
|
||||||
|
rng.GetNonZeroBytes(result);
|
||||||
|
} while (!ByteArrayIsNonZero(result));
|
||||||
|
}
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
public static string Encrypt(string plainText, string password,
|
public static string Encrypt(string plainText, string password,
|
||||||
string salt = null, string hashAlgorithm = "SHA1",
|
string salt = null, string hashAlgorithm = "SHA1",
|
||||||
int passwordIterations = 2, int keySize = 256)
|
int passwordIterations = 2, int keySize = 256)
|
||||||
{
|
{
|
||||||
if (string.IsNullOrEmpty(plainText))
|
if (string.IsNullOrEmpty(plainText))
|
||||||
return "";
|
return null;
|
||||||
|
if (string.IsNullOrEmpty(password))
|
||||||
|
return null;
|
||||||
|
|
||||||
byte[] plainTextBytes;
|
byte[] plainTextBytes;
|
||||||
byte[] saltValueBytes;
|
byte[] saltValueBytes;
|
||||||
|
|
@ -97,9 +128,7 @@ public static string Encrypt(string plainText, string password,
|
||||||
if (string.IsNullOrEmpty(salt))
|
if (string.IsNullOrEmpty(salt))
|
||||||
{
|
{
|
||||||
saltValueBytes = new byte[64]; // Nice and long
|
saltValueBytes = new byte[64]; // Nice and long
|
||||||
RandomNumberGenerator rng = RandomNumberGenerator.Create();
|
saltValueBytes = GenerateSecureNonZeroByteArray(saltValueBytes.Length);
|
||||||
rng.GetNonZeroBytes(saltValueBytes);
|
|
||||||
rng.Dispose();
|
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
|
|
@ -117,7 +146,9 @@ public static string Encrypt(string plainText, string password,
|
||||||
byte[] keyBytes = derivedPassword.GetBytes(keySize / 8);
|
byte[] keyBytes = derivedPassword.GetBytes(keySize / 8);
|
||||||
RijndaelManaged symmetricKey = new RijndaelManaged();
|
RijndaelManaged symmetricKey = new RijndaelManaged();
|
||||||
symmetricKey.Mode = CipherMode.CBC;
|
symmetricKey.Mode = CipherMode.CBC;
|
||||||
symmetricKey.GenerateIV();
|
|
||||||
|
// Generate IV
|
||||||
|
symmetricKey.IV = GenerateSecureNonZeroByteArray(symmetricKey.IV.Length);
|
||||||
|
|
||||||
byte[] cipherTextBytes = null;
|
byte[] cipherTextBytes = null;
|
||||||
|
|
||||||
|
|
@ -151,6 +182,8 @@ public static string Decrypt(string cipherText, string password, string salt = "
|
||||||
{
|
{
|
||||||
if (string.IsNullOrEmpty(cipherText))
|
if (string.IsNullOrEmpty(cipherText))
|
||||||
return null;
|
return null;
|
||||||
|
if (string.IsNullOrEmpty(password))
|
||||||
|
return null;
|
||||||
|
|
||||||
byte[] initialVectorBytes;
|
byte[] initialVectorBytes;
|
||||||
byte[] saltValueBytes;
|
byte[] saltValueBytes;
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue