Esempio n. 1
0
        public bool VerifyData(string originalMessage, string signedMessage)
        {
            if (string.IsNullOrEmpty(originalMessage))
            {
                return(false);
            }

            if (string.IsNullOrEmpty(signedMessage))
            {
                return(false);
            }

            if (_publicKey.IsNull())
            {
                throw new ArgumentException("The public key value for verification has not been provided, you must either provide them or call SignData before attempting to verify data", nameof(PublicKey));
            }

            bool success;

            using (var rsa = CreateCrypto())
            {
                var    encoder       = new UTF8Encoding();
                byte[] bytesToVerify = encoder.GetBytes(originalMessage);
                byte[] signedBytes   = Convert.FromBase64String(signedMessage);

                rsa.ImportParameters(_publicKey);

                success = rsa.VerifyData(bytesToVerify, signedBytes, _hashAlgorithmName, _rsaSignaturePadding);
            }

            return(success);
        }
Esempio n. 2
0
        public string SignData(string message)
        {
            if (string.IsNullOrEmpty(message))
            {
                return(null);
            }

            byte[] signedBytes;
            using (var rsa = CreateCrypto())
            {
                var    encoder      = new UTF8Encoding();
                byte[] originalData = encoder.GetBytes(message);

                if (!_privateKey.IsNull())
                {
                    rsa.ImportParameters(_privateKey);
                }
                else
                {
                    // make new keys accessible to consumers for storage
                    SetKeys(rsa);
                }

                signedBytes = rsa.SignData(originalData, 0, originalData.Length, _hashAlgorithmName, _rsaSignaturePadding);
            }

            return(Convert.ToBase64String(signedBytes));
        }
Esempio n. 3
0
        public RsaCrypto(RSAParameters privateKey, RSAParameters publicKey)
        {
            if (privateKey.IsNull() && publicKey.IsNull())
            {
                throw new ArgumentException($"Ctor requires at least one key, {nameof(privateKey)} and {nameof(publicKey)} are null or empty");
            }

            _privateKey = privateKey;
            PrivateKey  = RsaParamToString(_privateKey);
            _publicKey  = publicKey;
            PublicKey   = RsaParamToString(_publicKey);
        }