public Object verify(String mode, jwkPublicKey publicKey, byte[] plainBytes, string hashAlgorithm, string curveName, byte[] signatureBytes) { bool verified = false; if (mode == "ecdsa") { EllipticCurveFp curve = selectCamelotCurve(curveName); ecPublicKey ecPublicKey = new ecPublicKey(publicKey); EllipticCurvePointFp point = new EllipticCurvePointFp(curve, false, ecPublicKey.X, ecPublicKey.Y); camelot.ECKeyPair ecKeyPair = new camelot.ECKeyPair(curve, SEC1EncodingFp.EncodePoint(point)); camelot.HashAlgorithm h = selectCamelotHashAlgorithm(hashAlgorithm); byte[] digest = h.ComputeHash(plainBytes); verified = verifyCamelotEcdsa(ecKeyPair, digest, signatureBytes); } else { rsaPublicKey rsaKey = new rsaPublicKey(publicKey); RSACryptoServiceProvider rsaCsp = new RSACryptoServiceProvider(); RSAParameters rsaParams = rsaKey.toRSAParameters(); rsaCsp.ImportParameters(rsaParams); if (mode == "rsa-pss") { verified = verifyCamelotPSS(rsaParams, hashAlgorithm, plainBytes, signatureBytes); } else if (mode == "rsassa-pkcs1-v1_5") { verified = rsaCsp.VerifyData(plainBytes, selectCSPHashAlgorithm(hashAlgorithm), signatureBytes); } else { throw new InvalidOperationException("Unsupported mode"); } } return(verified); }
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); }
private Boolean verifyCamelotPSS(RSAParameters rsaParams, string hashAlgorithm, byte[] plainBytes, byte[] signatureBytes) { try { camelot.RSAManaged myRsaCsp = new camelot.RSAManaged(null, rsaParams.Exponent, rsaParams.Modulus); camelot.HashAlgorithm hashAlg = selectCamelotHashAlgorithm(hashAlgorithm); // Decrypt using Camelot OAEP + SHA2 camelot.RSAPSSSignatureDeformatter netPssDeformatter = new camelot.RSAPSSSignatureDeformatter(myRsaCsp, hashAlg); Boolean verified = netPssDeformatter.VerifySignature(plainBytes, signatureBytes); return(verified); } catch (Exception ex) { throw ex; } }
private byte[] signCamelotPSS(RSAParameters rsaParams, string hashAlgorithm, byte[] plainBytes) { try { camelot.RSAManaged myRsaCsp = new camelot.RSAManaged(rsaParams.D, null, rsaParams.Modulus); camelot.HashAlgorithm hashAlg = selectCamelotHashAlgorithm(hashAlgorithm); // Decrypt using Camelot OAEP + SHA2 camelot.RSAPSSSignatureFormatter netPssFormatter = new camelot.RSAPSSSignatureFormatter(myRsaCsp, hashAlg, rng); byte[] signatureBytes = netPssFormatter.CreateSignature(plainBytes); return(signatureBytes); } catch (Exception ex) { return(ByteConverter.GetBytes(ex.Message)); } }
private byte[] encryptCamelotOAEP(RSAParameters rsaParams, string hashAlgorithm, byte[] plainBytes) { try { camelot.RSAManaged myRsaCsp = new camelot.RSAManaged(null, rsaParams.Exponent, rsaParams.Modulus); camelot.HashAlgorithm hashAlg = selectCamelotHashAlgorithm(hashAlgorithm); // Decrypt using Camelot OAEP + SHA2 camelot.RSAOAEPKeyExchangeFormatter netOaepFormatter = new camelot.RSAOAEPKeyExchangeFormatter(myRsaCsp, hashAlg, rng); byte[] cipherBytes = netOaepFormatter.CreateKeyExchange(plainBytes); return(cipherBytes); } catch (Exception ex) { return(ByteConverter.GetBytes(ex.Message)); } }
private byte[] decryptCamelotOAEP(RSAParameters rsaParams, string hashAlgorithm, byte[] cipherBytes) { try { camelot.RSAManaged myRsaCsp = new camelot.RSAManaged(rsaParams.D, null, rsaParams.Modulus); camelot.HashAlgorithm hashAlg = selectCamelotHashAlgorithm(hashAlgorithm); // Decrypt using Camelot OAEP + SHA2 camelot.RSAOAEPKeyExchangeDeformatter netOaepDeformatter1 = new camelot.RSAOAEPKeyExchangeDeformatter(myRsaCsp, hashAlg); byte[] decryptedActual = netOaepDeformatter1.DecryptKeyExchange(cipherBytes); return(decryptedActual); } catch (Exception ex) { return(ByteConverter.GetBytes(ex.Message)); } }