public void PrivateKeyDataComputation()
    {
        var    algorithmName = AsymmetricAlgorithm.RsaOaepSha1;
        string base64BCrypt  = Helper.PrivateKeyFormatsAndBlobs[Tuple.Create(algorithmName, CryptographicPrivateKeyBlobType.BCryptPrivateKey)];
        string base64Pkcs1   = Helper.PrivateKeyFormatsAndBlobs[Tuple.Create(algorithmName, CryptographicPrivateKeyBlobType.Pkcs1RsaPrivateKey)];

        var algorithm = WinRTCrypto.AsymmetricKeyAlgorithmProvider.OpenAlgorithm(algorithmName);
        ICryptographicKey bcryptKey = algorithm.ImportKeyPair(Convert.FromBase64String(base64BCrypt), CryptographicPrivateKeyBlobType.BCryptPrivateKey);
        ICryptographicKey pkcs1Key  = algorithm.ImportKeyPair(Convert.FromBase64String(base64Pkcs1), CryptographicPrivateKeyBlobType.Pkcs1RsaPrivateKey);

        var bcryptParameters = bcryptKey.ExportParameters(true);
        var pkcs1Parameters  = pkcs1Key.ExportParameters(true);

        this.logger.WriteLine("PKCS1  P: {0}", WinRTCrypto.CryptographicBuffer.EncodeToHexString(pkcs1Parameters.P));
        this.logger.WriteLine("BCrypt P: {0}", WinRTCrypto.CryptographicBuffer.EncodeToHexString(bcryptParameters.P));
        this.logger.WriteLine("PKCS1  Q: {0}", WinRTCrypto.CryptographicBuffer.EncodeToHexString(pkcs1Parameters.Q));
        this.logger.WriteLine("BCrypt Q: {0}", WinRTCrypto.CryptographicBuffer.EncodeToHexString(bcryptParameters.Q));
        this.logger.WriteLine("PKCS1  D: {0}", WinRTCrypto.CryptographicBuffer.EncodeToHexString(pkcs1Parameters.D));
        this.logger.WriteLine("BCrypt D: {0}", WinRTCrypto.CryptographicBuffer.EncodeToHexString(bcryptParameters.D));
        this.logger.WriteLine("PKCS1  DP: {0}", WinRTCrypto.CryptographicBuffer.EncodeToHexString(pkcs1Parameters.DP));
        this.logger.WriteLine("BCrypt DP: {0}", WinRTCrypto.CryptographicBuffer.EncodeToHexString(bcryptParameters.DP));
        this.logger.WriteLine("PKCS1  DQ: {0}", WinRTCrypto.CryptographicBuffer.EncodeToHexString(pkcs1Parameters.DQ));
        this.logger.WriteLine("BCrypt DQ: {0}", WinRTCrypto.CryptographicBuffer.EncodeToHexString(bcryptParameters.DQ));
        this.logger.WriteLine("PKCS1  InverseQ: {0}", WinRTCrypto.CryptographicBuffer.EncodeToHexString(pkcs1Parameters.InverseQ));
        this.logger.WriteLine("BCrypt InverseQ: {0}", WinRTCrypto.CryptographicBuffer.EncodeToHexString(bcryptParameters.InverseQ));

        Assert.Equal <byte>(pkcs1Parameters.P, bcryptParameters.P);
        Assert.Equal <byte>(pkcs1Parameters.Q, bcryptParameters.Q);
        ////Assert.Equal<byte>(pkcs1Parameters.D, bcryptParameters.D); // not equal when computed ourselves, but equivalent
        Assert.Equal <byte>(pkcs1Parameters.DP, bcryptParameters.DP);
        Assert.Equal <byte>(pkcs1Parameters.DQ, bcryptParameters.DQ);
        Assert.Equal <byte>(pkcs1Parameters.InverseQ, bcryptParameters.InverseQ);
    }
Example #2
0
        public static JsonWebKey ToJsonWebKey(this ICryptographicKey key,
                                              string alg = "RS256", string kid = null)
        {
            var parameters = key.ExportParameters(false);

            var n = Base64Url.Encode(parameters.Modulus);
            var e = Base64Url.Encode(parameters.Exponent);

            return(new JsonWebKey()
            {
                N = n,
                E = e,
                Kid = kid ?? CryptoRandom.CreateUniqueId(),
                Kty = "RSA",
                Alg = alg,
            });
        }
Example #3
0
    public void PrivateKeyDataComputation()
    {
        var    algorithmName = AsymmetricAlgorithm.RsaOaepSha1;
        string base64BCrypt  = Helper.PrivateKeyFormatsAndBlobs[Tuple.Create(algorithmName, CryptographicPrivateKeyBlobType.BCryptPrivateKey)];
        string base64Pkcs1   = Helper.PrivateKeyFormatsAndBlobs[Tuple.Create(algorithmName, CryptographicPrivateKeyBlobType.Pkcs1RsaPrivateKey)];

        var algorithm = WinRTCrypto.AsymmetricKeyAlgorithmProvider.OpenAlgorithm(algorithmName);
        ICryptographicKey bcryptKey = algorithm.ImportKeyPair(Convert.FromBase64String(base64BCrypt), CryptographicPrivateKeyBlobType.BCryptPrivateKey);
        ICryptographicKey pkcs1Key  = algorithm.ImportKeyPair(Convert.FromBase64String(base64Pkcs1), CryptographicPrivateKeyBlobType.Pkcs1RsaPrivateKey);

        var bcryptParameters = bcryptKey.ExportParameters(true);
        var pkcs1Parameters  = pkcs1Key.ExportParameters(true);

        this.LogRSAParameterComparison("BCrypt", bcryptParameters, "PKCS1", pkcs1Parameters);

        Assert.Equal <byte>(pkcs1Parameters.P, bcryptParameters.P);
        Assert.Equal <byte>(pkcs1Parameters.Q, bcryptParameters.Q);
        ////Assert.Equal<byte>(pkcs1Parameters.D, bcryptParameters.D); // not equal when computed ourselves, but equivalent
        Assert.Equal <byte>(pkcs1Parameters.DP, bcryptParameters.DP);
        Assert.Equal <byte>(pkcs1Parameters.DQ, bcryptParameters.DQ);
        Assert.Equal <byte>(pkcs1Parameters.InverseQ, bcryptParameters.InverseQ);
    }