Example #1
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 #2
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 #3
0
 public rsaPrivateKey(jwkPrivateKey jwkFormatKey)
 {
     this.Modulus  = Base64Url.from(jwkFormatKey.n);
     this.Exponent = Base64Url.from(jwkFormatKey.e);
     this.D        = Base64Url.from(jwkFormatKey.d);
     this.P        = Base64Url.from(jwkFormatKey.p);
     this.Q        = Base64Url.from(jwkFormatKey.q);
     this.DP       = Base64Url.from(jwkFormatKey.dp);
     this.DQ       = Base64Url.from(jwkFormatKey.dq);
     this.InverseQ = Base64Url.from(jwkFormatKey.qi);
 }
Example #4
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);
        }
Example #5
0
 public ecPrivateKey(jwkPrivateKey jwkFormatKey)
 {
     this.D = Base64Url.from(jwkFormatKey.d);
 }