Example #1
0
        private RSACryptoServiceProvider importKey(string encodedKey)
        {
            byte[] keyBytes = Convert.FromBase64String(encodedKey);

            string keyJson = ByteConverter.GetString(keyBytes);

            jwkPrivateKey key = serializer.Deserialize <jwkPrivateKey>(keyJson);

            Object        rsaKey;
            RSAParameters rsaParams;

            if (key.d != null)
            {
                rsaKey    = new rsaPrivateKey(key);
                rsaParams = (rsaKey as rsaPrivateKey).toRSAParameters();
            }
            else
            {
                rsaKey    = new rsaPublicKey(key);
                rsaParams = (rsaKey as rsaPublicKey).toRSAParameters();
            }

            RSACryptoServiceProvider rsaCsp = new RSACryptoServiceProvider();

            rsaCsp.ImportParameters(rsaParams);

            return(rsaCsp);
        }
Example #2
0
        public Object decrypt(jwkPrivateKey privateKey, byte[] cipherBytes, string hashAlgorithm)
        {
            rsaPrivateKey            rsaKey    = new rsaPrivateKey(privateKey);
            RSACryptoServiceProvider rsaCsp    = new RSACryptoServiceProvider();
            RSAParameters            rsaParams = rsaKey.toRSAParameters();

            rsaCsp.ImportParameters(rsaParams);

            byte[] decryptedBytes;

            try
            {
                if (hashAlgorithm == null)
                {
                    decryptedBytes = rsaCsp.Decrypt(cipherBytes, false);
                }
                else
                {
                    decryptedBytes = decryptCamelotOAEP(rsaParams, hashAlgorithm, cipherBytes);
                }
            }
            catch (Exception ex)
            {
                return(new Error(ex));
            }

            return(decryptedBytes);
        }
Example #3
0
        public Object sign(string mode, jwkPrivateKey privateKey, byte[] plainBytes, string hashAlgorithm, string curveName)
        {
            byte[] signature;

            if (mode == "ecdsa")
            {
                EllipticCurveFp curve = selectCamelotCurve(curveName);

                ecPrivateKey ecPrivateKey = new ecPrivateKey(privateKey);

                camelot.ECKeyPair ecKeyPairPrivate = new camelot.ECKeyPair(curve, ecPrivateKey.D, null);

                camelot.HashAlgorithm h = selectCamelotHashAlgorithm(hashAlgorithm);

                byte[] digest = h.ComputeHash(plainBytes);

                signature = signCamelotEcdsa(ecKeyPairPrivate, digest);
            }
            else
            {
                rsaPrivateKey            rsaKey    = new rsaPrivateKey(privateKey);
                RSACryptoServiceProvider rsaCsp    = new RSACryptoServiceProvider();
                RSAParameters            rsaParams = rsaKey.toRSAParameters();
                rsaCsp.ImportParameters(rsaParams);

                if (mode == "rsa-pss")
                {
                    signature = signCamelotPSS(rsaParams, hashAlgorithm, plainBytes);
                }
                else if (mode == "rsassa-pkcs1-v1_5")
                {
                    signature = rsaCsp.SignData(plainBytes, selectCSPHashAlgorithm(hashAlgorithm));
                }
                else
                {
                    throw new InvalidOperationException("Unsupported mode");
                }
            }

            return(signature);
        }