Exemplo n.º 1
0
        public byte[] EncryptRSA(byte[] data, RSA publicKey)
        {
            var cipher = CipherUtilities.GetCipher(RSA_CRYPTO_CIPHER);

            cipher.Init(true, BouncyCastleUtilities.GetRsaPublicKey(publicKey));
            cipher.ProcessBytes(data);
            return(cipher.DoFinal());
        }
Exemplo n.º 2
0
        public byte[] DecryptRSA(byte[] data, RSA privateKey)
        {
            var cipher = CipherUtilities.GetCipher(RSA_CRYPTO_CIPHER);

            cipher.Init(false, BouncyCastleUtilities.GetRsaKeyPair(privateKey).Private);
            cipher.ProcessBytes(data);
            return(cipher.DoFinal());
        }
Exemplo n.º 3
0
        public string GeneratePublicKeyFingerprintFromPrivateKey(RSA privateKey)
        {
            if (privateKey == null)
            {
                throw new ArgumentNullException(nameof(privateKey));
            }
            var keyInfo    = SubjectPublicKeyInfoFactory.CreateSubjectPublicKeyInfo(BouncyCastleUtilities.GetRsaPublicKey(privateKey));
            var keyBytes   = keyInfo.ToAsn1Object().GetDerEncoded();
            var hash       = DoHash(keyBytes, new MD5Digest());
            var hashString = ByteArrayUtils.ByteArrayToHexString(hash, ":");

            return(hashString);
        }
Exemplo n.º 4
0
        public RSA LoadRsaPublicKey(string keyContents)
        {
            if (keyContents == null)
            {
                throw new ArgumentNullException(nameof(keyContents));
            }
            var stringReader = new StringReader(keyContents);
            var pemReader    = new Org.BouncyCastle.OpenSsl.PemReader(stringReader);
            var pemObject    = pemReader.ReadObject();

            if (pemObject is RsaKeyParameters)
            {
                return(BouncyCastleUtilities.ToRSA((RsaKeyParameters)pemObject));
            }

            throw new CryptographyError($"Failed to load public key from PEM file.");
        }
Exemplo n.º 5
0
        public RSA LoadRsaPrivateKey(string keyContents)
        {
            if (keyContents == null)
            {
                throw new ArgumentNullException(nameof(keyContents));
            }
            try
            {
                var stringReader = new StringReader(keyContents);
                var pemReader    = new Org.BouncyCastle.OpenSsl.PemReader(stringReader);
                var pemObject    = pemReader.ReadObject();
                if (pemObject is AsymmetricCipherKeyPair)
                {
                    var cipherPair = (AsymmetricCipherKeyPair)pemObject;
                    if (cipherPair.Private == null)
                    {
                        throw new CryptographyError("No private key found in PEM object");
                    }
                    if (!(cipherPair.Private is RsaPrivateCrtKeyParameters))
                    {
                        throw new CryptographyError("Private key is not RSA");
                    }
                    return(BouncyCastleUtilities.ToRSA((RsaPrivateCrtKeyParameters)cipherPair.Private));
                }

                throw new CryptographyError($"Failed to load public key from PEM file. Object was not of type expected. ({pemObject})");
            }
            catch (CryptographyError)
            {
                throw;
            }
            catch (Exception ex)
            {
                throw new CryptographyError("Unknown error occurred while parsing PEM data, see inner exception", ex);
            }
        }