Пример #1
0
        /// <summary>
        /// Verifies a signature to be authentic
        /// </summary>
        /// <param name="originalSignature">The signature which is be verified</param>
        /// <param name="publicKey">the public key used for the verification</param>
        /// <param name="data">the data which is signed</param>
        /// <returns>true if signature is authentic, false if not</returns>
        public bool Verify(byte[] originalSignature, byte[] pubKey, byte[] data)
        {
            int bytesRead;

            cipher.ImportSubjectPublicKeyInfo(pubKey, out bytesRead);
            var hash = MsdnHash.Compute(MsdnHashAlgorithim.SHA1, data);

            return(cipher.VerifyHash(hash, HashAlgorithmName.SHA1.ToString(), originalSignature));
        }
Пример #2
0
        /// <summary>
        /// Signs the passed in data with a private key
        /// </summary>
        /// <param name="privateKey">the private key used to create the signature</param>
        /// <param name="data">The data to sign</param>
        /// <returns>the signature as a byte array</returns>
        public byte[] Sign(byte[] privKey, byte[] data)
        {
            int bytesRead;

            cipher.ImportPkcs8PrivateKey(privKey, out bytesRead);
            var hash = MsdnHash.Compute(MsdnHashAlgorithim.SHA1, data);

            return(cipher.SignHash(hash, HashAlgorithmName.SHA1.ToString()));
        }
Пример #3
0
        /// <summary>
        /// Verifies a signature to be authentic
        /// </summary>
        /// <param name="originalSignature">The signature which is be verified</param>
        /// <param name="publicKey">the public key used for the verification</param>
        /// <param name="data">the data which is signed</param>
        /// <returns>true if signature is authentic, false if not</returns>
        public bool Verify(byte[] originalSignature, byte[] pubKey, byte[] data)
        {
            int bytesRead;

            cipher.ImportRSAPublicKey(pubKey, out bytesRead);
            var hash = MsdnHash.Compute(MsdnHashAlgorithim.SHA1, data);

            return(cipher.VerifyHash(hash, originalSignature, HashAlgorithmName.SHA1, RSASignaturePadding.Pkcs1));
        }
Пример #4
0
        /// <summary>
        /// Signs the passed in data with a private key
        /// </summary>
        /// <param name="privateKey">the private key used to create the signature</param>
        /// <param name="data">The data to sign</param>
        /// <returns>the signature as a byte array</returns>
        public byte[] Sign(byte[] privKey, byte[] data)
        {
            int bytesRead;

            cipher.ImportRSAPrivateKey(privKey, out bytesRead);
            var hash = MsdnHash.Compute(MsdnHashAlgorithim.SHA1, data);

            return(cipher.SignHash(hash, HashAlgorithmName.SHA1, RSASignaturePadding.Pkcs1));
        }
Пример #5
0
        /// <summary>
        /// Verifies a signature to be authentic
        /// </summary>
        /// <param name="originalSignature">The signature which is be verified</param>
        /// <param name="publicKey">the public key used for the verification</param>
        /// <param name="data">the data which is signed</param>
        /// <returns>true if signature is authentic, false if not</returns>
        public bool Verify(byte[] originalSignature, byte[] pubKey, byte[] data)
        {
            int bytesRead;

            try
            {
                //Import public key bytes
                cipher.ImportSubjectPublicKeyInfo(pubKey, out bytesRead);
            }
            catch (CryptographicException exception)
            {
                string message = "Signature Verification Failed!\n" +
                                 $"{exception.Message}\n" +
                                 "The contents of source do not represent an ASN.1-DER-encoded X.509 public key structure.\n" +
                                 "-or- The contents of source indicate the key is for an algorithm other than the algorithm represented by this instance.\n" +
                                 "-or- The contents of source represent the key in a format that is not supported.\n" +
                                 "-or- The algorithm-specific key import failed.\n";
                throw new CryptographicException(message, exception);
            }
            catch (PlatformNotSupportedException exception)
            {
                string message = "Signature Verification Failed!\n" +
                                 $"{exception.Message}\n" +
                                 "The public key is corrupted.\n" +
                                 "Verify the public key.";
                throw new CryptographicException(message, exception);
            }
            catch (Exception exception)
            {
                string message = "Signature Verification Failed!\n" +
                                 $"{exception.Message}\n" +
                                 "Contact developer.";
                throw new CryptographicException(message, exception);
            }

            //hash data
            var hash = MsdnHash.Compute(MsdnHashAlgorithim.SHA512, data);

            //verify signature
            return(cipher.VerifyHash(hash, originalSignature));
        }
Пример #6
0
        /// <summary>
        /// Signs the passed in data with a private key
        /// </summary>
        /// <param name="privateKey">the private key used to create the signature</param>
        /// <param name="data">The data to sign</param>
        /// <returns>the signature as a byte array</returns>
        public byte[] Sign(byte[] privKey, byte[] data)
        {
            int bytesRead;

            try
            {
                //import private key bytes
                cipher.ImportPkcs8PrivateKey(privKey, out bytesRead);
            }
            catch (CryptographicException exception)
            {
                string message = "Signature Failed!\n" +
                                 $"{exception.Message}\n" +
                                 "The contents of source do not represent an ASN.1-BER-encoded PKCS#8 private key structure.\n" +
                                 "-or- The contents of source indicate the key is for an algorithm other than the algorithm represented by this instance.\n" +
                                 "-or- The contents of source represent the key in a format that is not supported.\n" +
                                 "-or- The algorithm-specific key import failed.\n";
                throw new CryptographicException(message, exception);
            }
            catch (PlatformNotSupportedException exception)
            {
                string message = "Signature Failed!\n" +
                                 $"{exception.Message}\n" +
                                 "The public key is corrupted.\n" +
                                 "Verify the public key.";
                throw new CryptographicException(message, exception);
            }
            catch (Exception exception)
            {
                string message = "Signature Failed!\n" +
                                 $"{exception.Message}\n" +
                                 "Contact developer.";
                throw new CryptographicException(message, exception);
            }

            //hash data
            var hash = MsdnHash.Compute(MsdnHashAlgorithim.SHA512, data);

            //sign hash
            return(cipher.SignHash(hash));
        }