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); }
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); }