Exemplo n.º 1
0
        public void EncryptAndDecryptWithFilename()
        {
            #region Arrange

            var secret = Encryption.Random.CreateData(512 / 8);

            #endregion

            #region Act

            using (var input = File.OpenRead(this.InputFile))
                using (var output = File.Open(this.OutputFile, FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite))
                {
                    SymmetricEncryption.EncryptInternal(input, output, secret, new EncryptInternalParameter()
                    {
                        Filename = Guid.NewGuid().ToString()
                    });
                }

            Console.Out.WriteLine("Encrypted content: " + Convert.ToBase64String(File.ReadAllBytes(this.OutputFile)));

            using (var input = File.OpenRead(this.OutputFile))
                using (var output = File.Create(this.ResultFile))
                {
                    SymmetricEncryption.DecryptInternal(input, output, secret, null, new DecryptInternalParameter());
                }

            #endregion

            #region Assert

            Assert.That(File.ReadLines(this.InputFile), Is.EquivalentTo(File.ReadLines(this.ResultFile)));

            #endregion
        }
Exemplo n.º 2
0
        public void ToEncyptedDataAndDecryptInternal()
        {
            #region Arrange

            var si = new SecretInformation()
            {
                Filename = Guid.NewGuid().ToString()
            };

            var secret = Random.CreateData(512 / 8);
            var result = new MemoryStream();

            #endregion

            #region Act

            var encyptedData = si.ToEncyptedData(secret);
            SymmetricEncryption.DecryptInternal(new MemoryStream(encyptedData), result, secret, null, null);
            var secretInformation = SecretInformation.FromProtoBufData(result.ToArray());

            #endregion

            #region Assert

            Assert.That(secretInformation.Filename, Is.EqualTo(si.Filename));

            #endregion
        }
Exemplo n.º 3
0
        public void EncryptAndDecrypt_MemoryStream()
        {
            #region Arrange

            var secret = Encryption.Random.CreateData(512 / 8);
            var data   = Encryption.Random.CreateData(512);

            #endregion

            #region Act

            MemoryStream output;
            using (var input = new MemoryStream(data))
                using (output = new MemoryStream())
                {
                    SymmetricEncryption.EncryptInternal(input, output, secret);
                }

            var encryptedData = output.ToArray();

            Console.Out.WriteLine("Encrypted content: " + Convert.ToBase64String(encryptedData));

            MemoryStream decrypted;
            using (var input = new MemoryStream(encryptedData))
                using (decrypted = new MemoryStream())
                {
                    SymmetricEncryption.DecryptInternal(input, decrypted, secret, null, new DecryptInternalParameter());
                }

            #endregion

            #region Assert

            Assert.That(data, Is.EquivalentTo(decrypted.ToArray()));

            #endregion
        }
Exemplo n.º 4
0
        public static DecryptInfo Decrypt(Stream input, Stream output, DecryptionParameter parameter)
        {
            byte[] DeriveSecretFromHsm(EllipticCurveEncryptionInformation information)
            {
                var keys         = Encryption.NitroKey.EllipticCurveCryptographer.GetEcKeyPairInfos();
                var ecIdentifier = keys.FirstOrDefault(info => information.DerivedSecrets.Any(secret => info.PublicKey.CheckPublicKeyHash(secret.PublicKeyHash, secret.PublicKeyHashSalt)))?.EcIdentifier;

                if (ecIdentifier == null)
                {
                    throw new Exception("Couldn't find any key on any token");
                }

                return(GetSecretKey(ecIdentifier, information, parameter.Password));
            }

            var internalParameter = new DecryptInternalParameter
            {
                EllipticCurveDeriveKeyAction = information => parameter.PrivateKey == null?DeriveSecretFromHsm(information) : GetSecretKey(parameter.PrivateKey, information),
                                                   Progress   = parameter.Progress,
                                                   IsCanceled = parameter.IsCanceled
            };

            return(SymmetricEncryption.DecryptInternal(input, output, null, null, internalParameter));
        }