예제 #1
0
        protected override void ProcessRecord()
        {
            var privateKey = PrivateKey.ToByteArrayFromBase64String();
            var publicKey = PublicKey.ToByteArrayFromBase64String();
            if (ParameterSetName == "File")
            {
                if (ReplaceFile.IsTrue())
                    OutFile = File;

                byte[] fileEndData = new byte[40];
                long dataEnd = 0;

                using (FileStream source = new FileStream(File, FileMode.Open, FileAccess.Read, FileShare.Read))
                {
                    source.Seek(-24, SeekOrigin.End);
                    dataEnd = source.Position;
                    source.Read(fileEndData, 0, 24);
                }

                byte[] nonce = new byte[24];
                Array.Copy(fileEndData, 0, nonce, 0, 24);

                using (ICryptoTransform transform = new SodiumCryptoTransform(nonce, privateKey, publicKey, SodiumCryptoTransform.Direction.Decrypt))
                using (FileStream destination = new FileStream(OutFile, FileMode.CreateNew, FileAccess.Write, FileShare.None))
                using (CryptoStream cryptoStream = new CryptoStream(destination, transform, CryptoStreamMode.Write))
                using (FileStream source = new FileStream(File, FileMode.Open, FileAccess.Read, FileShare.Read))
                    source.CopyTo(cryptoStream);

            }
            else
            {
                byte[] message;
                message = PublicKeyBox.Open(rawMessage, Nonce.ToByteArrayFromBase64String(), privateKey, publicKey);

                if (Raw.IsTrue())
                {
                    WriteObject(message);
                }
                else
                {
                    var plainMessage = message.ToString(Encoding);
                    WriteObject(plainMessage);
                }
            }
        }
예제 #2
0
        protected override void ProcessRecord()
        {
            var key = Key.ToByteArrayFromBase64String();
            if (ParameterSetName == "File")
            {
                if (ReplaceFile.IsTrue())
                    OutFile = File;

                byte[] fileEndData = null;
                if (algo == SodiumCryptoTransform.SymmetricAlgorithm.ChaCha20)
                    fileEndData = new byte[8];
                else
                    fileEndData = new byte[24];
                long dataEnd = 0;

                using (FileStream source = new FileStream(File, FileMode.Open, FileAccess.Read, FileShare.Read))
                {
                    source.Seek(-fileEndData.Length, SeekOrigin.End);
                    dataEnd = source.Position;
                    source.Read(fileEndData, 0, fileEndData.Length);
                }

                byte[] nonce = new byte[fileEndData.Length];
                Array.Copy(fileEndData, 0, nonce, 0, fileEndData.Length);

                using (ICryptoTransform transform = new SodiumCryptoTransform(nonce, key, SodiumCryptoTransform.Direction.Decrypt, algo))
                using (FileStream destination = new FileStream(OutFile, FileMode.CreateNew, FileAccess.Write, FileShare.None))
                using (CryptoStream cryptoStream = new CryptoStream(destination, transform, CryptoStreamMode.Write))
                using (FileStream source = new FileStream(File, FileMode.Open, FileAccess.Read, FileShare.Read))
                    source.CopyTo(cryptoStream);

            }
            else
            {
                byte[] message;
                byte[] nonce = Nonce.ToByteArrayFromBase64String();
                switch (algo)
                {
                    case SodiumCryptoTransform.SymmetricAlgorithm.ChaCha20:
                        message = StreamEncryption.DecryptChaCha20(rawMessage, nonce, key);
                        break;
                    case SodiumCryptoTransform.SymmetricAlgorithm.XSalsa:
                        message = StreamEncryption.Decrypt(rawMessage, nonce, key);
                        break;
                    case SodiumCryptoTransform.SymmetricAlgorithm.Default:
                    default:
                        message = SecretBox.Open(rawMessage, nonce, key);
                        break;
                }
                if (Raw.IsTrue())
                {
                    WriteObject(message);
                }
                else
                {
                    var plainMessage = message.ToString(Encoding);
                    WriteObject(plainMessage);
                }
            }
        }
예제 #3
0
        protected override void ProcessRecord()
        {
            var nonce = SecretBox.GenerateNonce();
            var privateKey = PrivateKey.ToByteArrayFromBase64String();
            var publicKey = PublicKey.ToByteArrayFromBase64String();
            if (ParameterSetName == "File")
            {
                if (ReplaceFile.IsTrue())
                    OutFile = Path.GetTempFileName();

                using (ICryptoTransform transform = new SodiumCryptoTransform(nonce, privateKey, publicKey, SodiumCryptoTransform.Direction.Encrypt))
                using (FileStream destination = new FileStream(OutFile, FileMode.CreateNew, FileAccess.Write, FileShare.None))
                using (CryptoStream cryptoStream = new CryptoStream(destination, transform, CryptoStreamMode.Write))
                using (FileStream source = new FileStream(File, FileMode.Open, FileAccess.Read, FileShare.Read))
                {
                    source.CopyTo(cryptoStream);
                    cryptoStream.FlushFinalBlock();
                    destination.Write(nonce, 0, nonce.Length);
                    destination.Flush();
                }

                if (ReplaceFile.IsTrue())
                {
                    System.IO.File.Delete(File);
                    System.IO.File.Move(OutFile, File);
                }
            }
            else
            {
                var encryptedMessage = PublicKeyBox.Create(rawMessage, nonce, privateKey, publicKey);
                var results = new EncryptedMessage()
                {
                    EncryptedType = "Asymetric",
                    Message = NoCompression.IsTrue() ? encryptedMessage.ToBase64String() : encryptedMessage.Compress(),
                    Nonce = nonce.ToBase64String(),
                    Compressed = !NoCompression
                };

                WriteObject(results);
            }
        }
예제 #4
0
        protected override void ProcessRecord()
        {
            var key = Key.ToByteArrayFromBase64String();
            if (ParameterSetName == "File")
            {
                if (ReplaceFile.IsTrue())
                    OutFile = Path.GetTempFileName();

                using (ICryptoTransform transform = new SodiumCryptoTransform(nonce, key, SodiumCryptoTransform.Direction.Encrypt, algo))
                using (FileStream destination = new FileStream(OutFile, FileMode.CreateNew, FileAccess.Write, FileShare.None))
                using (CryptoStream cryptoStream = new CryptoStream(destination, transform, CryptoStreamMode.Write))
                using (FileStream source = new FileStream(File, FileMode.Open, FileAccess.Read, FileShare.Read))
                {
                    source.CopyTo(cryptoStream);
                    cryptoStream.FlushFinalBlock();
                    destination.Write(nonce, 0, nonce.Length);
                    destination.Flush();
                }

                if (ReplaceFile.IsTrue())
                {
                    System.IO.File.Delete(File);
                    System.IO.File.Move(OutFile, File);
                }
            }
            else
            {
                byte[] encryptedMessage = null;
                switch (algo)
                {
                    case SodiumCryptoTransform.SymmetricAlgorithm.ChaCha20:
                        encryptedMessage = StreamEncryption.EncryptChaCha20(rawMessage, nonce, key);
                        break;
                    case SodiumCryptoTransform.SymmetricAlgorithm.XSalsa:
                        encryptedMessage = StreamEncryption.Encrypt(rawMessage, nonce, key);
                        break;
                    case SodiumCryptoTransform.SymmetricAlgorithm.Default:
                    default:
                        encryptedMessage = SecretBox.Create(rawMessage, nonce, key);
                        break;
                }

                var results = new EncryptedMessage()
                {
                    EncryptedType = algo.GetDescription(),
                    Message = NoCompression.IsTrue() ? encryptedMessage.ToBase64String() : encryptedMessage.Compress(),
                    Nonce = nonce.ToBase64String(),
                    Compressed = !NoCompression
                };
                WriteObject(results);

            }
        }