Esempio n. 1
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);
        }
Esempio n. 2
0
 private void btnDecrypt_Click(object sender, EventArgs e)
 {
     try
     {
         EncryptedAsymmetric encrypted = Newtonsoft.Json.JsonConvert.DeserializeObject <EncryptedAsymmetric>(txtCryptText.Text);
         txtClearText.Text = CryptHandler.Decrypt(txtPrivateKey.Text, txtPublicKeyPartner.Text, encrypted);
     }
     catch (Exception ex)
     {
         MessageBox.Show(ex.ToString());
     }
 }
Esempio n. 3
0
        public static EncryptedAsymmetric Encrypt(string xmlPrivateKey, string xmlPublicKey, string message)
        {
            EncryptedAsymmetric output = new EncryptedAsymmetric();

            var symmetric = EncryptSymmetric(message);

            output.SymmetricData      = symmetric.Encrypted;
            output.SymmetricName      = symmetric.Name;
            output.SymmetricIVLength  = symmetric.IV.Length;
            output.SymmetricKeyLength = symmetric.Key.Length;

            var hashed = Hash(CryptHandler.Encoding.GetBytes(message));

            //output.HashData = hashed.Data;
            output.HashName   = hashed.Name;
            output.HashLength = hashed.Data.Length;

            using (var cryptAlgorithm = CryptHandler.CreateAsymmetric(xmlPrivateKey))
            {
                // Sign with PrivateKey, so the receiver can make sure that the sender
                // is correct, because only sender PublicKey will be able to verify signature.

                List <byte> dataToSign = new List <byte>();
                dataToSign.AddRange(symmetric.IV);
                dataToSign.AddRange(symmetric.Key);
                dataToSign.AddRange(hashed.Data);

                //output.AsymmetricSignature = cryptAlgorithm.SignHash(output.HashData, new HashAlgorithmName(hashed.Name), padding);
                output.AsymmetricSignature = cryptAlgorithm.SignData(dataToSign.ToArray(), new HashAlgorithmName(hashed.Name), padding);
                //output.AsymmetricSignatureLength = output.AsymmetricSignature.Length;
            }

            using (var cryptAlgorithm = CryptHandler.CreateAsymmetric(xmlPublicKey))
            {
                // Encrypt with receiver PublicKey, so only receiver PrivateKey will
                // be able to decrypt it.

                List <byte> dataToEncrypt = new List <byte>();
                dataToEncrypt.AddRange(symmetric.IV);
                dataToEncrypt.AddRange(symmetric.Key);
                dataToEncrypt.AddRange(hashed.Data);
                //dataToEncrypt.AddRange(output.AsymmetricSignature);

                output.AsymmetricData = cryptAlgorithm.Encrypt(dataToEncrypt.ToArray(), RSAEncryptionPadding.OaepSHA256);
                output.AsymmetricName = cryptAlgorithm.GetType().BaseType.Name;
            }

            return(output);
        }