public PassEncryptedData(
     byte[] originData,
     EPrivateKeysKeyDerivationFunction keyDerivationFunction,
     byte[] pass
     ) : this()
 {
     KeyDerivationFunctionType
         = (int)keyDerivationFunction;
     if (
         keyDerivationFunction
         == EPrivateKeysKeyDerivationFunction.ScryptDefault
         )
     {
         AesEncryptedData = CryptConfigFileHelper.Encrypt(
             originData,
             pass,
             Salt
             );
     }
     else
     {
         throw new ArgumentOutOfRangeException(
                   MyNameof.GetLocalVarName(() => keyDerivationFunction)
                   );
     }
 }
Пример #2
0
        public void TestEncryptDecryptImageFile()
        {
            const string dataFileName = "PNG_transparency_demonstration_1.png";
            var          fileExists   = File.Exists(dataFileName);

            _output.WriteLine(fileExists + string.Empty);
            Assert.Equal(true, fileExists);
            var dataToEncrypt = File.ReadAllBytes(dataFileName);
            var pass          = Encoding.UTF8.GetBytes("TestPassword");
            var salt          = new byte[32];
            var mySha256      = SHA256.Create();
            int seed          =
                BitConverter.ToInt32(
                    mySha256.ComputeHash(
                        BitConverter.GetBytes(
                            (int)DateTime.UtcNow.Ticks
                            & DateTime.UtcNow.Millisecond
                            )
                        ),
                    0
                    );
            var randomSource = new Random(seed);

            randomSource.NextBytes(salt);
            _output.WriteLine("{0} {1} {2}", dataToEncrypt.Length, pass.Length, salt.Length);
            var encryptedData = CryptConfigFileHelper.Encrypt(dataToEncrypt, pass, salt);

            _output.WriteLine("{0}", encryptedData.Length);
            var decryptedData = CryptConfigFileHelper.Decrypt(encryptedData, pass, salt);

            Assert.Equal(dataToEncrypt, decryptedData);
            var encryptedFileName = dataFileName + ".aes256";

            File.WriteAllText(
                encryptedFileName,
                new ScryptPassEncryptedData
            {
                EncryptedData = encryptedData,
                Salt          = salt
            }.WriteObjectToJson()
                );
        }