Exemplo n.º 1
0
        public Keys RsaNewKeys(int keySize)
        {
            Keys keys = new Keys();

            // Generate a public/private key pair.
            System.Security.Cryptography.RSACryptoServiceProvider rsa;
            rsa = new System.Security.Cryptography.RSACryptoServiceProvider(keySize);
            //-----------------------------------------------------
            //Save the public key information to an RSAParameters structure.
            System.Security.Cryptography.RSAParameters publicKeyInfo;
            publicKeyInfo = rsa.ExportParameters(false);
            string publicXml = rsa.ToXmlString(false);

            keys.Public = System.Convert.ToBase64String(rsa.ExportCspBlob(false));
            //-----------------------------------------------------
            //Save the public and private key information to an RSAParameters structure.
            System.Security.Cryptography.RSAParameters privateKeyInfo;
            privateKeyInfo = rsa.ExportParameters(true);
            string privateXml = rsa.ToXmlString(true);

            keys.Private = System.Convert.ToBase64String(rsa.ExportCspBlob(true));
            //-----------------------------------------------------
            //System.Security.Cryptography.X509Certificates.PublicKey pubKey;
            //System.Security.Cryptography.X509Certificates.PublicKey pvtKey;
            return(keys);
        }
Exemplo n.º 2
0
		public void init(int key_size)
		{
			//    KeyPairGenerator keyGen = KeyPairGenerator.getInstance("RSA");
			//    keyGen.initialize(key_size, new SecureRandom());
			//    KeyPair pair = keyGen.generateKeyPair();
			//
			//    PublicKey pubKey=pair.getPublic();
			//    PrivateKey prvKey=pair.getPrivate();

			System.Security.Cryptography.RSACryptoServiceProvider rsa = new System.Security.Cryptography.RSACryptoServiceProvider(key_size);
			RSAKeyInfo = rsa.ExportParameters(true);

			//    d=((RSAPrivateKey)prvKey).getPrivateExponent().toByteArray();
			//    e=((RSAPublicKey)pubKey).getPublicExponent().toByteArray();
			//    n=((RSAKey)prvKey).getModulus().toByteArray();
			//
			//    c=((RSAPrivateCrtKey)prvKey).getCrtCoefficient().toByteArray();
			//    ep=((RSAPrivateCrtKey)prvKey).getPrimeExponentP().toByteArray();
			//    eq=((RSAPrivateCrtKey)prvKey).getPrimeExponentQ().toByteArray();
			//    p=((RSAPrivateCrtKey)prvKey).getPrimeP().toByteArray();
			//    q=((RSAPrivateCrtKey)prvKey).getPrimeQ().toByteArray();

			d= RSAKeyInfo.D ;
			e=RSAKeyInfo.Exponent ;
			n=RSAKeyInfo.Modulus ;

			c=RSAKeyInfo.InverseQ ;
			ep=RSAKeyInfo.DP ;
			eq=RSAKeyInfo.DQ ;
			p=RSAKeyInfo.P ;
			q=RSAKeyInfo.Q ;
		}
Exemplo n.º 3
0
        public void init(int key_size)
        {
            //    KeyPairGenerator keyGen = KeyPairGenerator.getInstance("RSA");
            //    keyGen.initialize(key_size, new SecureRandom());
            //    KeyPair pair = keyGen.generateKeyPair();
            //
            //    PublicKey pubKey=pair.getPublic();
            //    PrivateKey prvKey=pair.getPrivate();

            System.Security.Cryptography.RSACryptoServiceProvider rsa = new System.Security.Cryptography.RSACryptoServiceProvider(key_size);
            RSAKeyInfo = rsa.ExportParameters(true);

            //    d=((RSAPrivateKey)prvKey).getPrivateExponent().toByteArray();
            //    e=((RSAPublicKey)pubKey).getPublicExponent().toByteArray();
            //    n=((RSAKey)prvKey).getModulus().toByteArray();
            //
            //    c=((RSAPrivateCrtKey)prvKey).getCrtCoefficient().toByteArray();
            //    ep=((RSAPrivateCrtKey)prvKey).getPrimeExponentP().toByteArray();
            //    eq=((RSAPrivateCrtKey)prvKey).getPrimeExponentQ().toByteArray();
            //    p=((RSAPrivateCrtKey)prvKey).getPrimeP().toByteArray();
            //    q=((RSAPrivateCrtKey)prvKey).getPrimeQ().toByteArray();

            d = RSAKeyInfo.D;
            e = RSAKeyInfo.Exponent;
            n = RSAKeyInfo.Modulus;

            c  = RSAKeyInfo.InverseQ;
            ep = RSAKeyInfo.DP;
            eq = RSAKeyInfo.DQ;
            p  = RSAKeyInfo.P;
            q  = RSAKeyInfo.Q;
        }
        public void RSAParametersXmlSerialization()
        {
            // Arrange
            CryptographyFactory f = new CryptographyFactory();

            System.Security.Cryptography.RSAParameters rsaParameters;
            using (System.Security.Cryptography.RSACryptoServiceProvider csp = new System.Security.Cryptography.RSACryptoServiceProvider())
            {
                rsaParameters = csp.ExportParameters(true);
            }

            // Act
            System.Xml.Serialization.XmlSerializer serializer = new System.Xml.Serialization.XmlSerializer(typeof(System.Security.Cryptography.RSAParameters));
            System.IO.MemoryStream ms;

            using (ms = new System.IO.MemoryStream())
            {
                serializer.Serialize(ms, rsaParameters);
            }

            byte[] serializedBytes = ms.ToArray();
            string xml             = System.Text.Encoding.UTF8.GetString(serializedBytes);

            Console.WriteLine(xml);

            // Assert
            Assert.NotZero(serializedBytes.Length);
        }
Exemplo n.º 5
0
        public SigningConfigurations()
        {
            using (var provider = new System.Security.Cryptography.RSACryptoServiceProvider(2048)){
                Key = new RsaSecurityKey(provider.ExportParameters(true));
            }

            SigningCredentials = new SigningCredentials(Key, SecurityAlgorithms.RsaSha256Signature);
        }
Exemplo n.º 6
0
 public SigningConfigurations()
 //configurando o construtor
 { //2048 é o numero do tamanho da criptografia
     using (var provider = new System.Security.Cryptography.RSACryptoServiceProvider(2048))
     {
         Key = new RsaSecurityKey(provider.ExportParameters(true));
     }
     //configurando as credenciais
     SigningCredentials = new SigningCredentials(Key, SecurityAlgorithms.RsaSha256Signature);
 }
Exemplo n.º 7
0
 void GenerateRSAKEY(int dwKyeSize)
 {
     try {
         System.Security.Cryptography.RSACryptoServiceProvider rsa = new System.Security.Cryptography.RSACryptoServiceProvider(dwKyeSize);
         xml_format = rsa.ToXmlString(true);
         keyinfo    = rsa.ExportParameters(true);
     }
     catch (System.Security.Cryptography.CryptographicException ex)
     {
         MessageBox.Show(ex.Message);
     }
 }
Exemplo n.º 8
0
        public static bool VerifyLicenseKeyTextFallback(this string licenseKeyText, out LicenseKey key)
        {
            System.Security.Cryptography.RSAParameters publicKeyParams;
            try
            {
                var publicRsaProvider = new System.Security.Cryptography.RSACryptoServiceProvider();
                publicRsaProvider.FromXml(LicenseUtils.LicensePublicKey);
                publicKeyParams = publicRsaProvider.ExportParameters(false);
            }
            catch (Exception ex)
            {
                throw new Exception("Could not import LicensePublicKey", ex);
            }

            try
            {
                key = licenseKeyText.ToLicenseKeyFallback();
            }
            catch (Exception ex)
            {
                throw new Exception("Could not deserialize LicenseKeyText Manually", ex);
            }

            byte[] originalData;
            byte[] signedData;

            try
            {
                originalData = key.GetHashKeyToSign().ToUtf8Bytes();
            }
            catch (Exception ex)
            {
                throw new Exception("Could not convert HashKey to UTF-8", ex);
            }

            try
            {
                signedData = Convert.FromBase64String(key.Hash);
            }
            catch (Exception ex)
            {
                throw new Exception("Could not convert key.Hash from Base64", ex);
            }

            try
            {
                return(VerifySignedHash(originalData, signedData, publicKeyParams));
            }
            catch (Exception ex)
            {
                throw new Exception($"Could not Verify License Key ({originalData.Length}, {signedData.Length})", ex);
            }
        }
Exemplo n.º 9
0
        public static bool VerifyLicenseKeyText(this string licenseKeyText, out LicenseKey key)
        {
            var publicRsaProvider = new System.Security.Cryptography.RSACryptoServiceProvider();

            publicRsaProvider.FromXml(LicenseUtils.LicensePublicKey);
            var publicKeyParams = publicRsaProvider.ExportParameters(false);

            key = licenseKeyText.ToLicenseKey();
            var originalData = key.GetHashKeyToSign().ToUtf8Bytes();
            var signedData   = Convert.FromBase64String(key.Hash);

            return(VerifySignedHash(originalData, signedData, publicKeyParams));
        }
Exemplo n.º 10
0
        public static string ToXmlString(this System.Security.Cryptography.RSACryptoServiceProvider rsa)
        {
            System.Security.Cryptography.RSAParameters parameters = rsa.ExportParameters(true);

            return(string.Format("<RSAKeyValue><Modulus>{0}</Modulus><Exponent>{1}</Exponent><P>{2}</P><Q>{3}</Q><DP>{4}</DP><DQ>{5}</DQ><InverseQ>{6}</InverseQ><D>{7}</D></RSAKeyValue>",
                                 System.Convert.ToBase64String(parameters.Modulus),
                                 System.Convert.ToBase64String(parameters.Exponent),
                                 System.Convert.ToBase64String(parameters.P),
                                 System.Convert.ToBase64String(parameters.Q),
                                 System.Convert.ToBase64String(parameters.DP),
                                 System.Convert.ToBase64String(parameters.DQ),
                                 System.Convert.ToBase64String(parameters.InverseQ),
                                 System.Convert.ToBase64String(parameters.D)));
        }
Exemplo n.º 11
0
        public void init(int key_size)
        {
            System.Security.Cryptography.RSACryptoServiceProvider rsa = new System.Security.Cryptography.RSACryptoServiceProvider(key_size);
            RSAKeyInfo = rsa.ExportParameters(true);

            d = RSAKeyInfo.D;
            e = RSAKeyInfo.Exponent;
            n = RSAKeyInfo.Modulus;

            c  = RSAKeyInfo.InverseQ;
            ep = RSAKeyInfo.DP;
            eq = RSAKeyInfo.DQ;
            p  = RSAKeyInfo.P;
            q  = RSAKeyInfo.Q;
        }
Exemplo n.º 12
0
        public static AsymmetricKeyParameter TransformRSAPrivateKey(System.Security.Cryptography.AsymmetricAlgorithm privateKey)
        {
            System.Security.Cryptography.RSACryptoServiceProvider prov       = privateKey as System.Security.Cryptography.RSACryptoServiceProvider;
            System.Security.Cryptography.RSAParameters            parameters = prov.ExportParameters(true);

            // Obviously this assumes that the certificate includes a RSA Key but the same result can be achieved for DSA with DSACryptoServiceProvider and DSAParameters

            return(new RsaPrivateCrtKeyParameters(
                       new Org.BouncyCastle.Math.BigInteger(1, parameters.Modulus),
                       new Org.BouncyCastle.Math.BigInteger(1, parameters.Exponent),
                       new Org.BouncyCastle.Math.BigInteger(1, parameters.D),
                       new Org.BouncyCastle.Math.BigInteger(1, parameters.P),
                       new Org.BouncyCastle.Math.BigInteger(1, parameters.Q),
                       new Org.BouncyCastle.Math.BigInteger(1, parameters.DP),
                       new Org.BouncyCastle.Math.BigInteger(1, parameters.DQ),
                       new Org.BouncyCastle.Math.BigInteger(1, parameters.InverseQ)
                       ));
        }
Exemplo n.º 13
0
        public byte[] Encrypt(byte[] inputDataBytes, out System.Security.Cryptography.RSAParameters rsaParameters)
        {
            byte[] encryptedData;

            using (System.Security.Cryptography.RSACryptoServiceProvider csp = new System.Security.Cryptography.RSACryptoServiceProvider())
            {
                // Return the full set of public and private keys
                rsaParameters = csp.ExportParameters(true);

                //Import only the public key information
                //csp.ImportParameters(csp.ExportParameters(false));

                // Encrypt the passed byte array and specify OAEP padding.
                // OAEP padding is only available on Microsoft Windows XP or later.
                encryptedData = csp.Encrypt(inputDataBytes, false);
            }

            return(encryptedData);
        }
        public void RSAParametersJsonSerialization()
        {
            // Arrange
            CryptographyFactory f = new CryptographyFactory();

            System.Security.Cryptography.RSAParameters rsaParameters;
            using (System.Security.Cryptography.RSACryptoServiceProvider csp = new System.Security.Cryptography.RSACryptoServiceProvider())
            {
                rsaParameters = csp.ExportParameters(true);
            }

            // Act
            System.Text.Json.JsonSerializerOptions opts = new System.Text.Json.JsonSerializerOptions();
            opts.WriteIndented = true;
            string json = System.Text.Json.JsonSerializer.Serialize(rsaParameters.ToDictionary(), opts);

            Console.WriteLine(json);

            // Assert
            Assert.NotNull(json);
        }
Exemplo n.º 15
0
        private string Encrypt(string text)
        {
            // Use OAEP padding (PKCS#1 v2).
            var doOaepPadding = true;
            // ------------------------------------------------
            // RSA Keys
            // ------------------------------------------------
            var rsa = new System.Security.Cryptography.RSACryptoServiceProvider();
            //Get the xml params returned form getPublicKey which contains the public key information
            var xmlParams = publicKeyTextBox.Text;

            // Import parameters from XML string.
            rsa.FromXmlString(xmlParams);

            // Export RSA key to RSAParameters and include:
            //    false - Only public key required for encryption.
            // Export parameters and include only Public Key (Modulus + Exponent)
            // required for encryption.
            var rsaParamsPublic = rsa.ExportParameters(false);

            // ------------------------------------------------
            // Encrypt
            // ------------------------------------------------
            var decryptedBytes = System.Text.Encoding.UTF8.GetBytes(text);

            // Create a new instance of RSACryptoServiceProvider.
            rsa = new System.Security.Cryptography.RSACryptoServiceProvider();
            // Import the RSA Key information.
            rsa.ImportParameters(rsaParamsPublic);
            // Encrypt byte array.
            var encryptedBytes = rsa.Encrypt(decryptedBytes, doOaepPadding);
            // Convert bytes to base64 string.
            var encryptedString = System.Convert.ToBase64String(encryptedBytes);

            return(encryptedString);
        }
Exemplo n.º 16
0
        public RSAParameters ExportParameters(bool includePrivateParameters)
        {
#if WINDOWS_STORE
            IBuffer export = keyPair.Export(CryptographicPrivateKeyBlobType.BCryptPrivateKey);

            byte[] result;
            CryptographicBuffer.CopyToByteArray(export, out result);

            BCryptRsaKeyBlob header = BCryptRsaKeyBlob.Load(result);
            int offset = Marshal.SizeOf <BCryptRsaKeyBlob>();

            byte[] exponent = result.Skip(offset).Take((int)header.cbPublicExp).ToArray();

            offset += (int)header.cbPublicExp;
            byte[] modulus = result.Skip(offset).Take((int)header.cbModulus).ToArray();

            return(new RSAParameters(exponent, modulus));
#else
            System.Security.Cryptography.RSAParameters parameters =
                provider.ExportParameters(includePrivateParameters);

            return(new RSAParameters(parameters.Exponent, parameters.Modulus));
#endif
        }
Exemplo n.º 17
0
 /// <summary>
 /// Exports private key information.
 /// </summary>
 /// <returns>private key information</returns>
 public System.Security.Cryptography.RSAParameters getPrivateKeyInformation()
 {
     return(csp.ExportParameters(true));
 }
Exemplo n.º 18
0
 public CrypterRSA()
 {
     rsa       = new System.Security.Cryptography.RSACryptoServiceProvider(1024);
     privatKey = rsa.ExportParameters(true);
     isPrivate = true;
 }
Exemplo n.º 19
0
 /// <summary>
 /// Generates a new RSA key pair.
 /// </summary>
 /// <param name="includePrivateKey">Key contains private key</param>
 /// <param name="keySize">Requested key size</param>
 /// <returns>RSA key information</returns>
 public System.Security.Cryptography.RSAParameters generateRSAKeyInformation(bool includePrivateKey = true, int keySize = 2048)
 {
     System.Security.Cryptography.RSACryptoServiceProvider RSACSP = new System.Security.Cryptography.RSACryptoServiceProvider(keySize);
     return(RSACSP.ExportParameters(includePrivateKey));
 }
Exemplo n.º 20
0
 public RsaKey()
 {
     iKey = new System.Security.Cryptography.RSACryptoServiceProvider(kKeySizeBits);
     iKeyInfo = iKey.ExportParameters(true);
 }