예제 #1
0
            public static string PrivateKeyPkcs8ToXml(string privateKey)
            {
                var privateKeyBytes = BaseConv.FromBase64(privateKey.RemovePkcs8PrivateKeyFormat());
                var privateKeyParam = (RsaPrivateCrtKeyParameters)PrivateKeyFactory.CreateKey(privateKeyBytes);

                //Key
                var element = new XElement("RSAKeyValue");

                var privateModulus  = new XElement("Modulus", Convert.ToBase64String(privateKeyParam.Modulus.ToByteArrayUnsigned()));
                var privateExponent = new XElement("Exponent", Convert.ToBase64String(privateKeyParam.PublicExponent.ToByteArrayUnsigned()));
                var privateP        = new XElement("P", Convert.ToBase64String(privateKeyParam.P.ToByteArrayUnsigned()));
                var privateQ        = new XElement("Q", Convert.ToBase64String(privateKeyParam.Q.ToByteArrayUnsigned()));
                var privateDp       = new XElement("DP", Convert.ToBase64String(privateKeyParam.DP.ToByteArrayUnsigned()));
                var privateDq       = new XElement("DQ", Convert.ToBase64String(privateKeyParam.DQ.ToByteArrayUnsigned()));
                var privateInverseQ = new XElement("InverseQ", Convert.ToBase64String(privateKeyParam.QInv.ToByteArrayUnsigned()));
                var privateD        = new XElement("D", Convert.ToBase64String(privateKeyParam.Exponent.ToByteArrayUnsigned()));

                element.Add(privateModulus);
                element.Add(privateExponent);
                element.Add(privateP);
                element.Add(privateQ);
                element.Add(privateDp);
                element.Add(privateDq);
                element.Add(privateInverseQ);
                element.Add(privateD);

                return(element.ToString());
            }
예제 #2
0
        public void Base64BytesTest()
        {
            var byteArray         = Encoding.UTF8.GetBytes("Alex LEWIS");
            var baseVal           = BaseConv.ToBase64(byteArray);
            var originalByteArray = BaseConv.FromBase64(baseVal);
            var originalVal       = Encoding.UTF8.GetString(originalByteArray);

            Assert.Equal("Alex LEWIS", originalVal);
        }
        public static void TouchFromPublicKeyInPkcs8(this MsRSA rsa, string publicKey, out RSAParameters parameters)
        {
            var publicKeyBytes = BaseConv.FromBase64(publicKey.RemovePkcs8PublicKeyFormat());
            var publicKeyParam = (RsaKeyParameters)PublicKeyFactory.CreateKey(publicKeyBytes);

            parameters = new RSAParameters
            {
                Modulus  = publicKeyParam.Modulus.ToByteArrayUnsigned(),
                Exponent = publicKeyParam.Exponent.ToByteArrayUnsigned()
            };

            rsa.ImportParameters(parameters);
        }
예제 #4
0
        public static byte[] GetBytes(this CipherTextTypes signatureTextType, string signature, Encoding encoding = default, Func <string, byte[]> customCipherTextConverter = null)
        {
            encoding = encoding.SafeEncodingValue();

            return(signatureTextType switch
            {
                CipherTextTypes.PlainText => encoding.GetBytes(signature),
                CipherTextTypes.Base32Text => BaseConv.FromBase32(signature),
                CipherTextTypes.Base64Text => BaseConv.FromBase64(signature),
                CipherTextTypes.Base91Text => BaseConv.FromBase91(signature),
                CipherTextTypes.Base256Text => BaseConv.FromBase256(signature),
                CipherTextTypes.ZBase32Text => BaseConv.FromZBase32(signature),
                CipherTextTypes.Hex => Org.BouncyCastle.Utilities.Encoders.Hex.Decode(signature),
                _ => customCipherTextConverter is null?encoding.GetBytes(signature) : customCipherTextConverter(signature)
            });
        public static void TouchFromPrivateKeyInPkcs8(this MsRSA rsa, string privateKey, out RSAParameters parameters)
        {
            var privateKeyBytes = BaseConv.FromBase64(privateKey.RemovePkcs8PrivateKeyFormat());
            var privateKeyParam = (RsaPrivateCrtKeyParameters)PrivateKeyFactory.CreateKey(privateKeyBytes);

            parameters = new RSAParameters
            {
                Modulus  = privateKeyParam.Modulus.ToByteArrayUnsigned(),
                Exponent = privateKeyParam.PublicExponent.ToByteArrayUnsigned(),
                P        = privateKeyParam.P.ToByteArrayUnsigned(),
                Q        = privateKeyParam.Q.ToByteArrayUnsigned(),
                DP       = privateKeyParam.DP.ToByteArrayUnsigned(),
                DQ       = privateKeyParam.DQ.ToByteArrayUnsigned(),
                InverseQ = privateKeyParam.QInv.ToByteArrayUnsigned(),
                D        = privateKeyParam.Exponent.ToByteArrayUnsigned()
            };

            rsa.ImportParameters(parameters);
        }
예제 #6
0
        /// <summary>
        /// Import RSA public key
        /// </summary>
        /// <param name="rsa"></param>
        /// <param name="format"></param>
        /// <param name="publicKey"></param>
        /// <param name="isPem"></param>
        public static void ImportPublicKey(this MsRSA rsa, RsaKeyFormat format, string publicKey, bool isPem = false)
        {
            if (isPem)
            {
                publicKey = format switch
                {
                    RsaKeyFormat.XML => publicKey,
                    RsaKeyFormat.JSON => publicKey,
                    RsaKeyFormat.Pkcs1 => publicKey.RemovePkcs1PublicKeyFormat(),
                    RsaKeyFormat.Pkcs8 => publicKey.RemovePkcs8PublicKeyFormat(),
                    _ => throw new NotSupportedException("Unknown RSA key type.")
                };
            }

            switch (format)
            {
            case RsaKeyFormat.XML:
                rsa.ImportKeyInLvccXml(publicKey);
                break;

            case RsaKeyFormat.JSON:
                rsa.ImportKeyInJson(publicKey);
                break;

            case RsaKeyFormat.Pkcs1:
#if NETCOREAPP3_1 || NETSTANDARD2_1
                rsa.ImportRSAPublicKey(BaseConv.FromBase64(publicKey), out _);
#else
                rsa.TouchFromPublicKeyInPkcs1(publicKey, out _);
#endif
                break;

            case RsaKeyFormat.Pkcs8:
#if NETCOREAPP3_1 || NETSTANDARD2_1
                rsa.ImportRSAPublicKey(BaseConv.FromBase64(publicKey), out _);
#else
                rsa.TouchFromPublicKeyInPkcs8(publicKey, out _);
#endif
                break;
            }
        }
    }