Beispiel #1
0
        private static EncryptedSymmetric EncryptSymmetric(string message)
        {
            EncryptedSymmetric encrypted;

            using (var symmetric = CryptHandler.CreateSymmetric())
            {
                symmetric.GenerateIV();
                symmetric.GenerateKey();

                using (var ms = new System.IO.MemoryStream())
                {
                    using (var cs = new CryptoStream(ms, symmetric.CreateEncryptor(), CryptoStreamMode.Write))
                    {
                        using (var writer = new StreamWriter(cs, CryptHandler.Encoding))
                        {
                            writer.Write(message);
                        }
                    }

                    encrypted = new EncryptedSymmetric()
                    {
                        Name      = symmetric.GetType().BaseType.Name,
                        IV        = symmetric.IV,
                        Key       = symmetric.Key,
                        Encrypted = ms.ToArray()
                    };
                }
                symmetric.Clear();
            }
            return(encrypted);
        }
Beispiel #2
0
        public static string Decrypt(string xmlPivateKey, string xmlPublicKey, EncryptedAsymmetric input)
        {
            EncryptedSymmetric symmetric = new EncryptedSymmetric()
            {
                Name = input.SymmetricName,
                IV   = new byte[input.SymmetricIVLength],
                Key  = new byte[input.SymmetricKeyLength]
            };
            Hashed hashed = new Hashed()
            {
                Name = input.HashName,
                Data = new byte[input.HashLength]
            };

            byte[] asymmetricDataDecrypted;
            using (var cryptAlgorithm = CryptHandler.CreateAsymmetric(xmlPivateKey))
            {
                asymmetricDataDecrypted = cryptAlgorithm.Decrypt(input.AsymmetricData, RSAEncryptionPadding.OaepSHA256);
            }

            int sourceStartPosition = 0;

            Array.Copy(asymmetricDataDecrypted, sourceStartPosition, symmetric.IV, 0, input.SymmetricIVLength);
            sourceStartPosition += input.SymmetricIVLength;
            Array.Copy(asymmetricDataDecrypted, sourceStartPosition, symmetric.Key, 0, input.SymmetricKeyLength);
            sourceStartPosition += input.SymmetricKeyLength;
            Array.Copy(asymmetricDataDecrypted, sourceStartPosition, hashed.Data, 0, input.HashLength);

            using (var cryptAlgorithm = CryptHandler.CreateAsymmetric(xmlPublicKey))
            {
                //byte[] dataToVerify = new byte[input.SymmetricIVLength + input.SymmetricKeyLength + input.HashLength];
                //Array.Copy(asymmetricDataDecrypted, 0, dataToVerify, 0, dataToVerify.Length);

                bool isDataVerified = cryptAlgorithm.VerifyData(asymmetricDataDecrypted, input.AsymmetricSignature, new HashAlgorithmName(input.HashName), padding);
                if (!isDataVerified)
                {
                    throw new Exception("Crypt data can't be verified.");
                }

                //bool isHashVerified = cryptAlgorithm.VerifyHash(hashed.Data, input.AsymmetricSignature, new HashAlgorithmName(input.HashName), padding);
                //if (!isHashVerified)
                //{
                //    throw new Exception("Crypt hash can't be verified.");
                //}
            }

            var symmetricDataDecrypted = DecryptSymmetric(input.SymmetricData, symmetric.IV, symmetric.Key);
            var hashedToCompare        = Hash(CryptHandler.Encoding.GetBytes(symmetricDataDecrypted));
            var isHashVerified         = hashedToCompare.Data.SequenceEqual(hashed.Data);

            if (!isHashVerified)
            {
                throw new Exception("Hash can't be verified.");
            }
            return(symmetricDataDecrypted);
        }