public void TestPasswordDerivates() { RandomNumberGenerator rng = RandomNumberGenerator.Create(); byte[] salt = new byte[32]; rng.GetBytes(salt); string password = SHA256.GetSHA256HashAsHexString(GeneralConverters.StringToByteArray("MartinDahl")); Rfc2898DeriveBytes pDeriveBytes = new Rfc2898DeriveBytes(password, salt, 1000); Rfc2898DeriveBytes pDeriveBytes2 = new Rfc2898DeriveBytes(password, salt, 1000); string a1 = GeneralConverters.ByteArrayToHexString(pDeriveBytes.GetBytes(16)); string a2 = GeneralConverters.ByteArrayToHexString(pDeriveBytes2.GetBytes(16)); Assert.IsTrue(a1 == a2, "Passwords derived was not correct"); }
public byte[] Encode(string password) { var secureRandom = new SecureRandomGenerator(); var msBlock = new MemoryStream(); var msContent = new MemoryStream(); int leftPaddingLength = secureRandom.GetRandomInt(64, 512); int rightPaddingLength = secureRandom.GetRandomInt(64, 512); byte[] sharedSecretBytes = GeneralConverters.StringToByteArray(SharedSecret); byte[] buffer = BitConverter.GetBytes(leftPaddingLength); msBlock.Write(buffer, 0, buffer.Length); buffer = BitConverter.GetBytes(rightPaddingLength); msBlock.Write(buffer, 0, buffer.Length); buffer = BitConverter.GetBytes(leftPaddingLength + rightPaddingLength + sharedSecretBytes.Length); msBlock.Write(buffer, 0, buffer.Length); msBlock.Write(secureRandom.GetRandomData(leftPaddingLength), 0, leftPaddingLength); msBlock.Write(sharedSecretBytes, 0, sharedSecretBytes.Length); msBlock.Write(secureRandom.GetRandomData(rightPaddingLength), 0, rightPaddingLength); byte[] encodeBytes = msBlock.ToArray(); encodeBytes = EncryptionManager.EncryptData(encodeBytes, password); byte[] hashBytes = SHA512.Create().ComputeHash(encodeBytes, 0, encodeBytes.Length); buffer = BitConverter.GetBytes(encodeBytes.Length); msContent.Write(buffer, 0, buffer.Length); msBlock.WriteTo(msContent); buffer = BitConverter.GetBytes(hashBytes.Length); msContent.Write(buffer, 0, buffer.Length); msContent.Write(hashBytes, 0, hashBytes.Length); return(msContent.ToArray()); }