Ejemplo n.º 1
0
        public static byte[] Decrypt(byte[] data, string keysPair)
        {
            if (data == null)
            {
                throw new ArgumentNullException(nameof(data));
            }
            if (keysPair == null)
            {
                throw new ArgumentNullException(nameof(keysPair));
            }

            CryptoHelper.Separate(out var encryptedKey, out var encryptedData, data, AsymmetricEncryption.KeySize);

            var commonKey = AsymmetricEncryption.Decrypt(encryptedKey, keysPair);

            return(SymmetricEncryption.Decrypt(encryptedData, commonKey));
        }
Ejemplo n.º 2
0
        public static byte[] Encrypt(byte[] data, string publicKey)
        {
            if (data == null)
            {
                throw new ArgumentNullException(nameof(data));
            }
            if (publicKey == null)
            {
                throw new ArgumentNullException(nameof(publicKey));
            }

            var commonKey     = SymmetricEncryption.GenerateKey();
            var encryptedKey  = AsymmetricEncryption.Encrypt(commonKey, publicKey);
            var encryptedData = SymmetricEncryption.Encrypt(data, commonKey);

            CryptoHelper.Concat(encryptedKey, encryptedData, out var result);
            return(result);
        }
Ejemplo n.º 3
0
        public static void Encrypt(Stream input, Stream output, string publicKey)
        {
            if (input == null)
            {
                throw new ArgumentNullException(nameof(input));
            }
            if (output == null)
            {
                throw new ArgumentNullException(nameof(output));
            }
            if (publicKey == null)
            {
                throw new ArgumentNullException(nameof(publicKey));
            }

            var commonKey    = SymmetricEncryption.GenerateKey();
            var encryptedKey = AsymmetricEncryption.Encrypt(commonKey, publicKey);

            output.Write(encryptedKey, 0, encryptedKey.Length);
            SymmetricEncryption.Encrypt(input, output, commonKey);
        }
Ejemplo n.º 4
0
        public static void Decrypt(Stream input, Stream output, string keysPair)
        {
            if (input == null)
            {
                throw new ArgumentNullException(nameof(input));
            }
            if (output == null)
            {
                throw new ArgumentNullException(nameof(output));
            }
            if (keysPair == null)
            {
                throw new ArgumentNullException(nameof(keysPair));
            }

            var encryptedKey = new byte[AsymmetricEncryption.KeySize];

            input.Read(encryptedKey, 0, encryptedKey.Length);

            var commonKey = AsymmetricEncryption.Decrypt(encryptedKey, keysPair);

            SymmetricEncryption.Decrypt(input, output, commonKey);
        }
Ejemplo n.º 5
0
 public static AsymmetricKeys GenerateKeys() => AsymmetricEncryption.GenerateKeys();