Ejemplo n.º 1
0
		public void TestWithoutCfg() {
			BigNumber bn = 0x10001;
			CryptoKey key;
			using (RSA rsa = new RSA()) {
				rsa.GenerateKeys(2048, bn, OnGenerator, null);
				key = new CryptoKey(rsa);
				// rsa is assigned, we no longer need this instance
			}

			X509V3ExtensionList extList = new X509V3ExtensionList();
			extList.Add(new X509V3ExtensionValue("subjectKeyIdentifier", false, "hash"));
			extList.Add(new X509V3ExtensionValue("authorityKeyIdentifier", false, "keyid:always,issuer:always"));
			extList.Add(new X509V3ExtensionValue("basicConstraints", true, "critical,CA:true"));
			extList.Add(new X509V3ExtensionValue("keyUsage", false, "cRLSign,keyCertSign"));

			using (X509CertificateAuthority root = X509CertificateAuthority.SelfSigned(
				new SimpleSerialNumber(),
				key,
				MessageDigest.SHA1,
				"Root1",
				DateTime.Now,
				TimeSpan.FromDays(365),
				extList)) {
				Console.WriteLine(root.Certificate);
				// Iterate the extensions
				Console.WriteLine("X509v3 Extensions:");
				using (OpenSSL.Core.Stack<X509Extension> ext_stack = root.Certificate.Extensions) {
					foreach (X509Extension ext in ext_stack) {
						Console.WriteLine("Name:{0}, IsCritical:{1}, Value:{2}", ext.Name, ext.IsCritical, ext);
					}
				}
			}
		}
Ejemplo n.º 2
0
        public void TestSealOpen()
        {
            string inputMsg = "This is a message";
            byte[] input = Encoding.ASCII.GetBytes(inputMsg);
            const int numKeys = 10;
            var rsas = new RSA[numKeys];
            var pkeys = new CryptoKey[numKeys];
            for (int i = 0; i < numKeys; i++) {
                rsas[i] = new RSA();
                rsas[i].GenerateKeys(1024, BigNumber.One, null, null);
                pkeys[i] = new CryptoKey(rsas[i]);
            }

            try {
                foreach (var cipher in Ciphers(true)) {
                    using (var cc = new CipherContext(cipher)) {
                        var env = cc.Seal(pkeys, input);
                        Assert.AreNotEqual(input, env.Data);

                        for (int i = 0; i < numKeys; i++) {
                            var result = cc.Open(env.Data, env.Keys[i], env.IV, pkeys[i]);
                            Assert.AreEqual(input, result);
                        }
                    }
                }
            }
            finally {
                for (int i = 0; i < numKeys; i++) {
                    pkeys[i].Dispose();
                    rsas[i].Dispose();
                }
            }
        }
Ejemplo n.º 3
0
		public void TestRsaSha1()
		{
			using (Configuration cfg = new Configuration("openssl.cnf"))
			{
				// Test RSA/SHA1 with other SelfSigned method
				BigNumber bn = 0x10001;
				CryptoKey key;

				using (RSA rsa = new RSA())
				{
					rsa.GenerateKeys(2048, bn, OnGenerator, null);
					key = new CryptoKey(rsa);
					// rsa is assigned, we no longer need this instance
				}

				using (var root = X509CertificateAuthority.SelfSigned(
					                  cfg,
					                  new SimpleSerialNumber(),
					                  key,
					                  MessageDigest.SHA1,
					                  "Root1",
					                  DateTime.Now,
					                  TimeSpan.FromDays(365)))
				{
					Console.WriteLine(root.Certificate);
				}
			}
		}
Ejemplo n.º 4
0
		byte[] Key1(RSA key)
		{
			byte[] n = {
				0x00, 0xAA, 0x36, 0xAB, 0xCE, 0x88, 0xAC, 0xFD, 0xFF, 0x55, 0x52, 0x3C, 0x7F, 0xC4, 0x52, 0x3F,
				0x90, 0xEF, 0xA0, 0x0D, 0xF3, 0x77, 0x4A, 0x25, 0x9F, 0x2E, 0x62, 0xB4, 0xC5, 0xD9, 0x9C, 0xB5,
				0xAD, 0xB3, 0x00, 0xA0, 0x28, 0x5E, 0x53, 0x01, 0x93, 0x0E, 0x0C, 0x70, 0xFB, 0x68, 0x76, 0x93,
				0x9C, 0xE6, 0x16, 0xCE, 0x62, 0x4A, 0x11, 0xE0, 0x08, 0x6D, 0x34, 0x1E, 0xBC, 0xAC, 0xA0, 0xA1,
				0xF5
			};

			byte[] e = { 0x11 };

			byte[] d = {
				0x0A, 0x03, 0x37, 0x48, 0x62, 0x64, 0x87, 0x69, 0x5F, 0x5F, 0x30, 0xBC, 0x38, 0xB9, 0x8B, 0x44,
				0xC2, 0xCD, 0x2D, 0xFF, 0x43, 0x40, 0x98, 0xCD, 0x20, 0xD8, 0xA1, 0x38, 0xD0, 0x90, 0xBF, 0x64,
				0x79, 0x7C, 0x3F, 0xA7, 0xA2, 0xCD, 0xCB, 0x3C, 0xD1, 0xE0, 0xBD, 0xBA, 0x26, 0x54, 0xB4, 0xF9,
				0xDF, 0x8E, 0x8A, 0xE5, 0x9D, 0x73, 0x3D, 0x9F, 0x33, 0xB3, 0x01, 0x62, 0x4A, 0xFD, 0x1D, 0x51,
			};

			byte[] p = {
				0x00, 0xD8, 0x40, 0xB4, 0x16, 0x66, 0xB4, 0x2E, 0x92, 0xEA, 0x0D, 0xA3, 0xB4, 0x32, 0x04, 0xB5,
				0xCF, 0xCE, 0x33, 0x52, 0x52, 0x4D, 0x04, 0x16, 0xA5, 0xA4, 0x41, 0xE7, 0x00, 0xAF, 0x46, 0x12,
				0x0D
			};

			byte[] q = {
				0x00, 0xC9, 0x7F, 0xB1, 0xF0, 0x27, 0xF4, 0x53, 0xF6, 0x34, 0x12, 0x33, 0xEA, 0xAA, 0xD1, 0xD9,
				0x35, 0x3F, 0x6C, 0x42, 0xD0, 0x88, 0x66, 0xB1, 0xD0, 0x5A, 0x0F, 0x20, 0x35, 0x02, 0x8B, 0x9D,
				0x89
			};

			byte[] dmp1 = {
				0x59, 0x0B, 0x95, 0x72, 0xA2, 0xC2, 0xA9, 0xC4, 0x06, 0x05, 0x9D, 0xC2, 0xAB, 0x2F, 0x1D, 0xAF,
				0xEB, 0x7E, 0x8B, 0x4F, 0x10, 0xA7, 0x54, 0x9E, 0x8E, 0xED, 0xF5, 0xB4, 0xFC, 0xE0, 0x9E, 0x05,
			};

			byte[] dmq1 = {
				0x00, 0x8E, 0x3C, 0x05, 0x21, 0xFE, 0x15, 0xE0, 0xEA, 0x06, 0xA3, 0x6F, 0xF0, 0xF1, 0x0C, 0x99,
				0x52, 0xC3, 0x5B, 0x7A, 0x75, 0x14, 0xFD, 0x32, 0x38, 0xB8, 0x0A, 0xAD, 0x52, 0x98, 0x62, 0x8D,
				0x51
			};

			byte[] iqmp = {
				0x36, 0x3F, 0xF7, 0x18, 0x9D, 0xA8, 0xE9, 0x0B, 0x1D, 0x34, 0x1F, 0x71, 0xD0, 0x9B, 0x76, 0xA8,
				0xA9, 0x43, 0xE1, 0x1D, 0x10, 0xB2, 0x4D, 0x24, 0x9F, 0x2D, 0xEA, 0xFE, 0xF8, 0x0C, 0x18, 0x26,
			};

			byte[] ctext_ex = {
				0x1b, 0x8f, 0x05, 0xf9, 0xca, 0x1a, 0x79, 0x52, 0x6e, 0x53, 0xf3, 0xcc, 0x51, 0x4f, 0xdb, 0x89,
				0x2b, 0xfb, 0x91, 0x93, 0x23, 0x1e, 0x78, 0xb9, 0x92, 0xe6, 0x8d, 0x50, 0xa4, 0x80, 0xcb, 0x52,
				0x33, 0x89, 0x5c, 0x74, 0x95, 0x8d, 0x5d, 0x02, 0xab, 0x8c, 0x0f, 0xd0, 0x40, 0xeb, 0x58, 0x44,
				0xb0, 0x05, 0xc3, 0x9e, 0xd8, 0x27, 0x4a, 0x9d, 0xbf, 0xa8, 0x06, 0x71, 0x40, 0x94, 0x39, 0xd2,
			};

			SetKey(key, n, e, d, p, q, dmp1, dmq1, iqmp);
			return ctext_ex;
		}
        /// <summary>
        /// Creates a new CryptoKey with public and private keys generated by the
        /// RSA algorithm.
        /// </summary>
        /// <param name="numberOfBits">The bit strength to be used for the RSA algorithm. A value greater than 1024 is recommended.</param>
        /// <returns>A new CryptoKey with both private and public keys generated used the RSA algorithm.</returns>
        public static CryptoKey CreateNewRsaKey(int numberOfBits)
        {
            using (var rsa = new RSA())
            {
                BigNumber exponent = 0x10001; // this needs to be a prime number
                rsa.GenerateKeys(numberOfBits, exponent, OnGenerator, null);

                return new CryptoKey(rsa);
            }
        }
Ejemplo n.º 6
0
 static void Main(string[] args)
 {
     Console.WriteLine("Generating RSA Key ...");
     RSA rsa = new RSA();
     rsa.GenerateKeys(2048, 1, null, null);
     string pem = rsa.PublicKeyAsPEM.Replace("\n", "").Replace("-----BEGIN PUBLIC KEY-----", "").Replace("-----END PUBLIC KEY-----", "");
     byte[] bytes = Convert.FromBase64String(pem);
     Console.WriteLine(Utility.ToHexString(bytes, false));
     Console.ReadLine();
 }
Ejemplo n.º 7
0
 /// <summary>
 /// 私钥加密
 /// </summary>
 public static string PrivateEncrypt(string privateKey, string text, Encoding encoding, int padding)
 {
     byte[] textBytes = encoding.GetBytes(text);
     using (BIO bio = new BIO(privateKey))
     {
         using (OpenSSL.Crypto.RSA rsa = OpenSSL.Crypto.RSA.FromPrivateKey(bio))
         {
             textBytes = rsa.PrivateEncrypt(textBytes, (OpenSSL.Crypto.RSA.Padding)padding);
         }
     }
     return(Convert.ToBase64String(textBytes));
 }
Ejemplo n.º 8
0
    public static string DecryptRSA(string privateKeyAsPem, byte[] payload, string passphrase = null)
    {
        var encoder = new UTF8Encoding();

        byte[]    byte_payload = payload;
        CryptoKey d            = CryptoKey.FromPrivateKey(privateKeyAsPem, passphrase);

        OpenSSL.Crypto.RSA rsa    = d.GetRSA();
        byte[]             result = rsa.PrivateDecrypt(byte_payload, OpenSSL.Crypto.RSA.Padding.PKCS1);
        rsa.Dispose();
        return(encoder.GetString(result));
    }
Ejemplo n.º 9
0
        private void button3_Click(object sender, EventArgs e)
        {
            Encoding encoding = Encoding.UTF8;

            using (OpenSSL.Crypto.RSA rsa = new OpenSSL.Crypto.RSA())
            {
                rsa.GenerateKeys(4096, BigNumber.One, null, null);
                privateKey = rsa.PrivateKeyAsPEM;
                publicKey  = rsa.PublicKeyAsPEM;
                MessageBox.Show("publicKey: " + publicKey + "\nprivateKey: " + privateKey);
            }
        }
Ejemplo n.º 10
0
 /// <summary>
 /// 公钥解密
 /// </summary>
 public static string PublicDecrypt(string publicKey, string text, Encoding encoding, int padding)
 {
     byte[] textBytes = Convert.FromBase64String(text);
     using (BIO bio = new BIO(publicKey))
     {
         using (OpenSSL.Crypto.RSA rsa = OpenSSL.Crypto.RSA.FromPublicKey(bio))
         {
             textBytes = rsa.PublicDecrypt(textBytes, (OpenSSL.Crypto.RSA.Padding)padding);
         }
     }
     return(encoding.GetString(textBytes));
 }
Ejemplo n.º 11
0
    public static byte[] EncryptRSA(string publicKeyAsPem, string payload, string passphrase = null)
    {
        var encoder = new UTF8Encoding();

        byte[]    byte_payload = encoder.GetBytes(payload);
        CryptoKey d            = CryptoKey.FromPublicKey(publicKeyAsPem, passphrase);

        OpenSSL.Crypto.RSA rsa    = d.GetRSA();
        byte[]             result = rsa.PublicEncrypt(byte_payload, OpenSSL.Crypto.RSA.Padding.PKCS1);
        rsa.Dispose();
        return(result);
    }
Ejemplo n.º 12
0
		static TestCipher()
		{
			const int numKeys = 10;
			Keys = new CryptoKey[numKeys];
			for (int i = 0; i < numKeys; i++)
			{
				using (var rsa = new RSA())
				{
					rsa.GenerateKeys(1024, BigNumber.One, null, null);
					Keys[i] = new CryptoKey(rsa);
				}
			}
		}
Ejemplo n.º 13
0
        public void TestGenRSA()
        {
            BigNumber e = null;
            //if (options.IsSet("3"))
            //    e = 3;
            //else if (options.IsSet("f4"))
            //    e = 0x10001;
            e = 0x10001;

            var rsagen = new RSA();
            rsagen.GenerateKeys(2048, e, GeneratorHandler, null);

            Cipher enc = null;
            //if (options.IsSet("des"))
            //    enc = Cipher.DES_CBC;
            //else if (options.IsSet("des3"))
            //    enc = Cipher.DES_EDE3_CBC;
            //else if (options.IsSet("idea"))
            //    enc = Cipher.Idea_CBC;
            //else if (options.IsSet("aes128"))
            //    enc = Cipher.AES_128_CBC;
            //else if (options.IsSet("aes192"))
            //    enc = Cipher.AES_192_CBC;
            //else if (options.IsSet("aes256"))
            //    enc = Cipher.AES_256_CBC;

            string passwd = null;

            using (var bio = BIO.MemoryBuffer())
            {
                rsagen.WritePrivateKey(bio, enc, OnPassword, passwd);

                var outfile = "openssl-rsagen-privatekey.txt";
                if (string.IsNullOrEmpty(outfile))
                    Console.WriteLine(bio.ReadString());
                else
                    File.WriteAllText(outfile, bio.ReadString());
            }

            using (var bio = BIO.MemoryBuffer())
            {
                rsagen.WritePublicKey(bio);

                var outfile = "openssl-rsagen-publickey.txt";
                if (string.IsNullOrEmpty(outfile))
                    Console.WriteLine(bio.ReadString());
                else
                    File.WriteAllText(outfile, bio.ReadString());
            }
        }
Ejemplo n.º 14
0
        private static X509Certificate CreateCertificate()
        {
            BigNumber bn = 0x10001;
            var rsa = new RSA();
            rsa.GenerateKeys(2048, bn, null, null);
            var key = new CryptoKey(rsa);

            var cert = new X509Certificate(
                new SimpleSerialNumber().Next(),
                new X509Name("Mooege"),
                new X509Name("Mooege"),
                key,
                DateTime.Now,
                DateTime.Now + TimeSpan.FromDays(365));

            cert.PrivateKey = key;
            return cert;
        }
Ejemplo n.º 15
0
		X509Certificate CreateCertificate(X509CertificateAuthority ca, string name, Configuration cfg, string section)
		{
			var now = DateTime.Now;
			var future = now + TimeSpan.FromDays(365);

			using (var subject = new X509Name(name))
			using (var rsa = new RSA())
			{
				rsa.GenerateKeys(1024, BigNumber.One, null, null);
				using (var key = new CryptoKey(rsa))
				{
					var request = new X509Request(1, subject, key);
					var cert = ca.ProcessRequest(request, now, future, cfg, section);
					cert.PrivateKey = key;
					return cert;
				}
			}
		}
Ejemplo n.º 16
0
		public void CanCompare()
		{
			using (DSA dsa = new DSA(true))
			{
				using (CryptoKey lhs = new CryptoKey(dsa))
				{
					Assert.AreEqual(lhs, lhs);
					using (CryptoKey rhs = new CryptoKey(dsa))
					{
						Assert.AreEqual(lhs, rhs);
					}

					using (DSA dsa2 = new DSA(true))
					{
						using (CryptoKey other = new CryptoKey(dsa2))
						{
							Assert.IsFalse(lhs == other);
						}
					}
				}
			}

			using (RSA rsa = new RSA())
			{
				rsa.GenerateKeys(1024, BigNumber.One, null, null);
				using (CryptoKey lhs = new CryptoKey(rsa))
				{
					Assert.AreEqual(lhs, lhs);
					using (CryptoKey rhs = new CryptoKey(rsa))
					{
						Assert.AreEqual(lhs, rhs);
					}

					using (RSA rsa2 = new RSA())
					{
						rsa2.GenerateKeys(1024, BigNumber.One, null, null);
						using (CryptoKey other = new CryptoKey(rsa2))
						{
							Assert.IsFalse(lhs == other);
						}
					}
				}
			}
		}
Ejemplo n.º 17
0
		public void CanCompareRSA()
		{
			using (var rsa = new RSA())
			{
				rsa.GenerateKeys(1024, BigNumber.One, null, null);
				using (var lhs = new CryptoKey(rsa))
				{
					Assert.AreEqual(lhs, lhs);
					using (var rhs = new CryptoKey(rsa))
					{
						Assert.AreEqual(lhs, rhs);
					}

					using (var rsa2 = new RSA())
					{
						rsa2.GenerateKeys(1024, BigNumber.One, null, null);
						using (var other = new CryptoKey(rsa2))
						{
							Assert.AreNotEqual(lhs, other);
						}
					}
				}
			}
		}
Ejemplo n.º 18
0
		public void CanCreateFromRSA()
		{
			using (var rsa = new RSA())
			{
				rsa.GenerateKeys(1024, BigNumber.One, null, null);
				using (var key = new CryptoKey(rsa))
				{
					Assert.AreEqual(CryptoKey.KeyType.RSA, key.Type);
					Assert.AreEqual(rsa.Size, key.Size);
					Assert.AreEqual(rsa.Handle, key.GetRSA().Handle);
				}

				using (var key = new CryptoKey())
				{
					key.Assign(rsa);
					Assert.AreEqual(rsa.Handle, key.GetRSA().Handle);
				}
			}
		}
Ejemplo n.º 19
0
		byte[] Key2(RSA key)
		{
			byte[] n = {
				0x00, 0xA3, 0x07, 0x9A, 0x90, 0xDF, 0x0D, 0xFD, 0x72, 0xAC, 0x09, 0x0C, 0xCC, 0x2A, 0x78, 0xB8,
				0x74, 0x13, 0x13, 0x3E, 0x40, 0x75, 0x9C, 0x98, 0xFA, 0xF8, 0x20, 0x4F, 0x35, 0x8A, 0x0B, 0x26,
				0x3C, 0x67, 0x70, 0xE7, 0x83, 0xA9, 0x3B, 0x69, 0x71, 0xB7, 0x37, 0x79, 0xD2, 0x71, 0x7B, 0xE8,
				0x34, 0x77, 0xCF,
			};

			byte[] e = { 0x03 };

			byte[] d = {
				0x6C, 0xAF, 0xBC, 0x60, 0x94, 0xB3, 0xFE, 0x4C, 0x72, 0xB0, 0xB3, 0x32, 0xC6, 0xFB, 0x25, 0xA2,
				0xB7, 0x62, 0x29, 0x80, 0x4E, 0x68, 0x65, 0xFC, 0xA4, 0x5A, 0x74, 0xDF, 0x0F, 0x8F, 0xB8, 0x41,
				0x3B, 0x52, 0xC0, 0xD0, 0xE5, 0x3D, 0x9B, 0x59, 0x0F, 0xF1, 0x9B, 0xE7, 0x9F, 0x49, 0xDD, 0x21,
				0xE5, 0xEB
			};

			byte[] p = {
				0x00, 0xCF, 0x20, 0x35, 0x02, 0x8B, 0x9D, 0x86, 0x98, 0x40, 0xB4, 0x16, 0x66, 0xB4, 0x2E, 0x92,
				0xEA, 0x0D, 0xA3, 0xB4, 0x32, 0x04, 0xB5, 0xCF, 0xCE, 0x91
			};

			byte[] q = {
				0x00, 0xC9, 0x7F, 0xB1, 0xF0, 0x27, 0xF4, 0x53, 0xF6, 0x34, 0x12, 0x33, 0xEA, 0xAA, 0xD1, 0xD9,
				0x35, 0x3F, 0x6C, 0x42, 0xD0, 0x88, 0x66, 0xB1, 0xD0, 0x5F
			};

			byte[] dmp1 = {
				0x00, 0x8A, 0x15, 0x78, 0xAC, 0x5D, 0x13, 0xAF, 0x10, 0x2B, 0x22, 0xB9, 0x99, 0xCD, 0x74, 0x61,
				0xF1, 0x5E, 0x6D, 0x22, 0xCC, 0x03, 0x23, 0xDF, 0xDF, 0x0B
			};

			byte[] dmq1 =
			{
				0x00, 0x86, 0x55, 0x21, 0x4A, 0xC5, 0x4D, 0x8D, 0x4E, 0xCD, 0x61, 0x77, 0xF1, 0xC7, 0x36, 0x90,
				0xCE, 0x2A, 0x48, 0x2C, 0x8B, 0x05, 0x99, 0xCB, 0xE0, 0x3F
			};

			byte[] iqmp = {
				0x00, 0x83, 0xEF, 0xEF, 0xB8, 0xA9, 0xA4, 0x0D, 0x1D, 0xB6, 0xED, 0x98, 0xAD, 0x84, 0xED, 0x13,
				0x35, 0xDC, 0xC1, 0x08, 0xF3, 0x22, 0xD0, 0x57, 0xCF, 0x8D
			};

			byte[] ctext_ex = {
				0x14, 0xbd, 0xdd, 0x28, 0xc9, 0x83, 0x35, 0x19, 0x23, 0x80, 0xe8, 0xe5, 0x49, 0xb1, 0x58, 0x2a,
				0x8b, 0x40, 0xb4, 0x48, 0x6d, 0x03, 0xa6, 0xa5, 0x31, 0x1f, 0x1f, 0xd5, 0xf0, 0xa1, 0x80, 0xe4,
				0x17, 0x53, 0x03, 0x29, 0xa9, 0x34, 0x90, 0x74, 0xb1, 0x52, 0x13, 0x54, 0x29, 0x08, 0x24, 0x52,
				0x62, 0x51
			};

			SetKey(key, n, e, d, p, q, dmp1, dmq1, iqmp);
			return ctext_ex;
		}
Ejemplo n.º 20
0
		public void TestCase()
		{
			OpenSSL.Core.Random.Seed(rnd_seed);

			for (int v = 0; v < 6; v++) {
				using (RSA key = new RSA()) {
					TestKey(v, key);
				}
			}
		}
Ejemplo n.º 21
0
		public void Execute(string[] args)
		{
			try
			{
				options.ParseArguments(args);
			}
			catch (Exception)
			{
				Usage();
				return;
			}

			int bits = 512;
			if (this.options.Arguments.Count == 1)
				bits = Convert.ToInt32(this.options.Arguments[0]);

			BigNumber e = null;
			if (options.IsSet("3"))
				e = 3;
			else if (options.IsSet("f4"))
				e = 0x10001;

			Console.Error.WriteLine("Generating RSA private key, {0} bit long modulus", bits);

			RSA rsa = new RSA();
			rsa.GenerateKeys(bits, e, Program.OnGenerator, null);

			Console.Error.WriteLine("e is {0} (0x{1})", e.ToDecimalString(), e.ToHexString());

			Cipher enc = null;
			if (options.IsSet("des"))
				enc = Cipher.DES_CBC;
			else if (options.IsSet("des3"))
				enc = Cipher.DES_EDE3_CBC;
			else if (options.IsSet("idea"))
				enc = Cipher.Idea_CBC;
			else if (options.IsSet("aes128"))
				enc = Cipher.AES_128_CBC;
			else if (options.IsSet("aes192"))
				enc = Cipher.AES_192_CBC;
			else if (options.IsSet("aes256"))
				enc = Cipher.AES_256_CBC;

			using (BIO bio = BIO.MemoryBuffer())
			{
				rsa.WritePrivateKey(bio, enc, Program.OnPassword, this.options["passout"]);

				string outfile = this.options["out"] as string;
				if (string.IsNullOrEmpty(outfile))
					Console.WriteLine(bio.ReadString());
				else
					File.WriteAllText(outfile, bio.ReadString());
			}
		}
Ejemplo n.º 22
0
		public void CanCreateFromRSA()
		{
			using (RSA rsa = new RSA())
			{
				rsa.GenerateKeys(1024, BigNumber.One, null, null);
				using (CryptoKey key = new CryptoKey(rsa))
				{
					Assert.AreEqual(CryptoKey.KeyType.RSA, key.Type);
					Assert.AreEqual(rsa.Size, key.Size);
				}
			}
		}
Ejemplo n.º 23
0
		private void SetKey(RSA key, byte[] n, byte[] e, byte[] d, byte[] p, byte[] q, byte[] dmp1, byte[] dmq1, byte[] iqmp)
		{
			using (BigNumber bn = BigNumber.FromArray(n))
				key.PublicModulus = bn;
			using (BigNumber bn = BigNumber.FromArray(e))
				key.PublicExponent = bn;
			using (BigNumber bn = BigNumber.FromArray(d))
				key.PrivateExponent = bn;
			using (BigNumber bn = BigNumber.FromArray(p))
				key.SecretPrimeFactorP = bn;
			using (BigNumber bn = BigNumber.FromArray(q))
				key.SecretPrimeFactorQ = bn;
			using (BigNumber bn = BigNumber.FromArray(dmp1))
				key.DmodP1 = bn;
			using (BigNumber bn = BigNumber.FromArray(dmq1))
				key.DmodQ1 = bn;
			using (BigNumber bn = BigNumber.FromArray(iqmp))
				key.IQmodP = bn;
		}
Ejemplo n.º 24
0
		byte[] Key3(RSA key)
		{
			byte[] n = {
				0x00, 0xBB, 0xF8, 0x2F, 0x09, 0x06, 0x82, 0xCE, 0x9C, 0x23, 0x38, 0xAC, 0x2B, 0x9D, 0xA8, 0x71,
				0xF7, 0x36, 0x8D, 0x07, 0xEE, 0xD4, 0x10, 0x43, 0xA4, 0x40, 0xD6, 0xB6, 0xF0, 0x74, 0x54, 0xF5,
				0x1F, 0xB8, 0xDF, 0xBA, 0xAF, 0x03, 0x5C, 0x02, 0xAB, 0x61, 0xEA, 0x48, 0xCE, 0xEB, 0x6F, 0xCD,
				0x48, 0x76, 0xED, 0x52, 0x0D, 0x60, 0xE1, 0xEC, 0x46, 0x19, 0x71, 0x9D, 0x8A, 0x5B, 0x8B, 0x80,
				0x7F, 0xAF, 0xB8, 0xE0, 0xA3, 0xDF, 0xC7, 0x37, 0x72, 0x3E, 0xE6, 0xB4, 0xB7, 0xD9, 0x3A, 0x25,
				0x84, 0xEE, 0x6A, 0x64, 0x9D, 0x06, 0x09, 0x53, 0x74, 0x88, 0x34, 0xB2, 0x45, 0x45, 0x98, 0x39,
				0x4E, 0xE0, 0xAA, 0xB1, 0x2D, 0x7B, 0x61, 0xA5, 0x1F, 0x52, 0x7A, 0x9A, 0x41, 0xF6, 0xC1, 0x68,
				0x7F, 0xE2, 0x53, 0x72, 0x98, 0xCA, 0x2A, 0x8F, 0x59, 0x46, 0xF8, 0xE5, 0xFD, 0x09, 0x1D, 0xBD,
				0xCB,
			};

			byte[] e = { 0x11 };

			byte[] d = {
				0x00, 0xA5, 0xDA, 0xFC, 0x53, 0x41, 0xFA, 0xF2, 0x89, 0xC4, 0xB9, 0x88, 0xDB, 0x30, 0xC1, 0xCD,
				0xF8, 0x3F, 0x31, 0x25, 0x1E, 0x06, 0x68, 0xB4, 0x27, 0x84, 0x81, 0x38, 0x01, 0x57, 0x96, 0x41,
				0xB2, 0x94, 0x10, 0xB3, 0xC7, 0x99, 0x8D, 0x6B, 0xC4, 0x65, 0x74, 0x5E, 0x5C, 0x39, 0x26, 0x69,
				0xD6, 0x87, 0x0D, 0xA2, 0xC0, 0x82, 0xA9, 0x39, 0xE3, 0x7F, 0xDC, 0xB8, 0x2E, 0xC9, 0x3E, 0xDA,
				0xC9, 0x7F, 0xF3, 0xAD, 0x59, 0x50, 0xAC, 0xCF, 0xBC, 0x11, 0x1C, 0x76, 0xF1, 0xA9, 0x52, 0x94,
				0x44, 0xE5, 0x6A, 0xAF, 0x68, 0xC5, 0x6C, 0x09, 0x2C, 0xD3, 0x8D, 0xC3, 0xBE, 0xF5, 0xD2, 0x0A,
				0x93, 0x99, 0x26, 0xED, 0x4F, 0x74, 0xA1, 0x3E, 0xDD, 0xFB, 0xE1, 0xA1, 0xCE, 0xCC, 0x48, 0x94,
				0xAF, 0x94, 0x28, 0xC2, 0xB7, 0xB8, 0x88, 0x3F, 0xE4, 0x46, 0x3A, 0x4B, 0xC8, 0x5B, 0x1C, 0xB3,
				0xC1,
			};

			byte[] p = {
				0x00, 0xEE, 0xCF, 0xAE, 0x81, 0xB1, 0xB9, 0xB3, 0xC9, 0x08, 0x81, 0x0B, 0x10, 0xA1, 0xB5, 0x60,
				0x01, 0x99, 0xEB, 0x9F, 0x44, 0xAE, 0xF4, 0xFD, 0xA4, 0x93, 0xB8, 0x1A, 0x9E, 0x3D, 0x84, 0xF6,
				0x32, 0x12, 0x4E, 0xF0, 0x23, 0x6E, 0x5D, 0x1E, 0x3B, 0x7E, 0x28, 0xFA, 0xE7, 0xAA, 0x04, 0x0A,
				0x2D, 0x5B, 0x25, 0x21, 0x76, 0x45, 0x9D, 0x1F, 0x39, 0x75, 0x41, 0xBA, 0x2A, 0x58, 0xFB, 0x65,
				0x99,
			};

			byte[] q = {
				0x00, 0xC9, 0x7F, 0xB1, 0xF0, 0x27, 0xF4, 0x53, 0xF6, 0x34, 0x12, 0x33, 0xEA, 0xAA, 0xD1, 0xD9,
				0x35, 0x3F, 0x6C, 0x42, 0xD0, 0x88, 0x66, 0xB1, 0xD0, 0x5A, 0x0F, 0x20, 0x35, 0x02, 0x8B, 0x9D,
				0x86, 0x98, 0x40, 0xB4, 0x16, 0x66, 0xB4, 0x2E, 0x92, 0xEA, 0x0D, 0xA3, 0xB4, 0x32, 0x04, 0xB5,
				0xCF, 0xCE, 0x33, 0x52, 0x52, 0x4D, 0x04, 0x16, 0xA5, 0xA4, 0x41, 0xE7, 0x00, 0xAF, 0x46, 0x15,
				0x03,
			};

			byte[] dmp1 = {
				0x54, 0x49, 0x4C, 0xA6, 0x3E, 0xBA, 0x03, 0x37, 0xE4, 0xE2, 0x40, 0x23, 0xFC, 0xD6, 0x9A, 0x5A,
				0xEB, 0x07, 0xDD, 0xDC, 0x01, 0x83, 0xA4, 0xD0, 0xAC, 0x9B, 0x54, 0xB0, 0x51, 0xF2, 0xB1, 0x3E,
				0xD9, 0x49, 0x09, 0x75, 0xEA, 0xB7, 0x74, 0x14, 0xFF, 0x59, 0xC1, 0xF7, 0x69, 0x2E, 0x9A, 0x2E,
				0x20, 0x2B, 0x38, 0xFC, 0x91, 0x0A, 0x47, 0x41, 0x74, 0xAD, 0xC9, 0x3C, 0x1F, 0x67, 0xC9, 0x81,
			};

			byte[] dmq1 = {
				0x47, 0x1E, 0x02, 0x90, 0xFF, 0x0A, 0xF0, 0x75, 0x03, 0x51, 0xB7, 0xF8, 0x78, 0x86, 0x4C, 0xA9,
				0x61, 0xAD, 0xBD, 0x3A, 0x8A, 0x7E, 0x99, 0x1C, 0x5C, 0x05, 0x56, 0xA9, 0x4C, 0x31, 0x46, 0xA7,
				0xF9, 0x80, 0x3F, 0x8F, 0x6F, 0x8A, 0xE3, 0x42, 0xE9, 0x31, 0xFD, 0x8A, 0xE4, 0x7A, 0x22, 0x0D,
				0x1B, 0x99, 0xA4, 0x95, 0x84, 0x98, 0x07, 0xFE, 0x39, 0xF9, 0x24, 0x5A, 0x98, 0x36, 0xDA, 0x3D,
			};

			byte[] iqmp = {
				0x00, 0xB0, 0x6C, 0x4F, 0xDA, 0xBB, 0x63, 0x01, 0x19, 0x8D, 0x26, 0x5B, 0xDB, 0xAE, 0x94, 0x23,
				0xB3, 0x80, 0xF2, 0x71, 0xF7, 0x34, 0x53, 0x88, 0x50, 0x93, 0x07, 0x7F, 0xCD, 0x39, 0xE2, 0x11,
				0x9F, 0xC9, 0x86, 0x32, 0x15, 0x4F, 0x58, 0x83, 0xB1, 0x67, 0xA9, 0x67, 0xBF, 0x40, 0x2B, 0x4E,
				0x9E, 0x2E, 0x0F, 0x96, 0x56, 0xE6, 0x98, 0xEA, 0x36, 0x66, 0xED, 0xFB, 0x25, 0x79, 0x80, 0x39,
				0xF7,
			};

			byte[] ctext_ex = {
				0xb8, 0x24, 0x6b, 0x56, 0xa6, 0xed, 0x58, 0x81, 0xae, 0xb5, 0x85, 0xd9, 0xa2, 0x5b, 0x2a, 0xd7,
				0x90, 0xc4, 0x17, 0xe0, 0x80, 0x68, 0x1b, 0xf1, 0xac, 0x2b, 0xc3, 0xde, 0xb6, 0x9d, 0x8b, 0xce,
				0xf0, 0xc4, 0x36, 0x6f, 0xec, 0x40, 0x0a, 0xf0, 0x52, 0xa7, 0x2e, 0x9b, 0x0e, 0xff, 0xb5, 0xb3,
				0xf2, 0xf1, 0x92, 0xdb, 0xea, 0xca, 0x03, 0xc1, 0x27, 0x40, 0x05, 0x71, 0x13, 0xbf, 0x1f, 0x06,
				0x69, 0xac, 0x22, 0xe9, 0xf3, 0xa7, 0x85, 0x2e, 0x3c, 0x15, 0xd9, 0x13, 0xca, 0xb0, 0xb8, 0x86,
				0x3a, 0x95, 0xc9, 0x92, 0x94, 0xce, 0x86, 0x74, 0x21, 0x49, 0x54, 0x61, 0x03, 0x46, 0xf4, 0xd4,
				0x74, 0xb2, 0x6f, 0x7c, 0x48, 0xb4, 0x2e, 0xe6, 0x8e, 0x1f, 0x57, 0x2a, 0x1f, 0xc4, 0x02, 0x6a,
				0xc4, 0x56, 0xb4, 0xf5, 0x9f, 0x7b, 0x62, 0x1e, 0xa1, 0xb9, 0xd8, 0x8f, 0x64, 0x20, 0x2f, 0xb1,
			};

			SetKey(key, n, e, d, p, q, dmp1, dmq1, iqmp);
			return ctext_ex;
		}
Ejemplo n.º 25
0
		/// <summary>
		/// Calls EVP_PKEY_set1_RSA()
		/// </summary>
		/// <param name="rsa"></param>
		public CryptoKey(RSA rsa)
			: this()
		{
			Native.ExpectSuccess(Native.EVP_PKEY_set1_RSA(ptr, rsa.Handle));
		}
Ejemplo n.º 26
0
		/// <summary>
		/// Calls EVP_PKEY_assign()
		/// </summary>
		/// <param name="key">Key.</param>
		public void Assign(RSA key)
		{
			key.AddRef();
			Native.ExpectSuccess(Native.EVP_PKEY_assign(ptr, (int)KeyType.RSA, key.Handle));
		}
Ejemplo n.º 27
0
		public void Execute(string[] args)
		{
			OpenSSL.Core.Random.Seed(rnd_seed);

			for (int v = 0; v < 6; v++)
			{
				using (RSA key = new RSA())
				{
					TestKey(v, key);
				}
			}
		}
Ejemplo n.º 28
0
		public RSAWrapper()
		{
			Rsa = new RSA();
		}
Ejemplo n.º 29
0
		private void TestKey(int v, RSA key)
		{
			byte[] ctext_ex = null;
			switch (v % 3)
			{
				case 0: ctext_ex = Key1(key); break;
				case 1: ctext_ex = Key2(key); break;
				case 2: ctext_ex = Key3(key); break;
			}

			if (v / 3 >= 1)
				key.ConstantTime = false;

			byte[] ctext = key.PublicEncrypt(ptext_ex, RSA.Padding.PKCS1);
			Assert.AreEqual(ctext_ex.Length, ctext.Length);

			byte[] ptext = key.PrivateDecrypt(ctext, RSA.Padding.PKCS1);
			string str1 = BitConverter.ToString(ptext);
			string str2 = BitConverter.ToString(ptext_ex);
			Assert.AreEqual(str2, str1);

			Console.WriteLine("PKCS #1 v1.5 encryption/decryption ok");

			ctext = key.PublicEncrypt(ptext_ex, RSA.Padding.OAEP);
			Assert.AreEqual(ctext_ex.Length, ctext.Length);

			ptext = key.PrivateDecrypt(ctext, RSA.Padding.OAEP);
			str1 = BitConverter.ToString(ptext);
			str2 = BitConverter.ToString(ptext_ex);
			Assert.AreEqual(str2, str1);

			Console.WriteLine("OAEP test vector {0} passed!", v);

			// Different ciphertexts (rsa_oaep.c without -DPKCS_TESTVECT).
			// Try decrypting ctext_ex
			ptext = key.PrivateDecrypt(ctext_ex, RSA.Padding.OAEP);
			str1 = BitConverter.ToString(ptext);
			str2 = BitConverter.ToString(ptext_ex);
			Assert.AreEqual(str2, str1);

			Console.WriteLine("OAEP encryption/decryption ok!", v);
			for (int n = 0; n < ctext.Length; ++n)
			{
				byte saved = ctext[n];
				for (byte b = 0; b < byte.MaxValue; ++b)
				{
					if (b == saved)
						continue;

					ctext[n] = b;
					bool error = false;
					try
					{
						ptext = key.PrivateDecrypt(ctext, RSA.Padding.OAEP);
					}
					catch (Exception)
					{
						error = true;
					}
					Assert.IsTrue(error, "Corrupt data decrypted!");
				}
			}
		}
Ejemplo n.º 30
0
		public void TestCase(int v)
		{
			OpenSSL.Core.Random.Seed(rnd_seed);

			using (RSA key = new RSA())
			{
				TestKey(v, key);
			}
		}
Ejemplo n.º 31
0
 /// <summary>
 /// Calls EVP_PKEY_set1_RSA()
 /// </summary>
 /// <param name="rsa"></param>
 public CryptoKey(RSA rsa)
     : this()
 {
     Native.ExpectSuccess(Native.EVP_PKEY_set1_RSA(ptr, rsa.Handle));
 }
Ejemplo n.º 32
0
        public override PrivateKey GeneratePrivateKey(PrivateKeyParams pkp)
        {
            var rsaPkParams = pkp as RsaPrivateKeyParams;
            var ecPkParams  = pkp as EcPrivateKeyParams;

            if (rsaPkParams != null)
            {
                int bits;
                // Bits less than 1024 are weak Ref: http://openssl.org/docs/manmaster/crypto/RSA_generate_key_ex.html
                if (rsaPkParams.NumBits < RSA_BITS_MINIMUM)
                {
                    bits = RSA_BITS_DEFAULT;
                }
                else
                {
                    bits = rsaPkParams.NumBits;
                }

                BigNumber e;
                if (string.IsNullOrEmpty(rsaPkParams.PubExp))
                {
                    e = RSA_E_F4;
                }
                else if (rsaPkParams.PubExp.StartsWith("0x", StringComparison.OrdinalIgnoreCase))
                {
                    e = BigNumber.FromHexString(rsaPkParams.PubExp);
                }
                else
                {
                    e = BigNumber.FromDecimalString(rsaPkParams.PubExp);
                }

                using (var rsa = new OSSL_RSA())
                {
                    BigNumber.GeneratorHandler cbWrapper = null;
                    if (rsaPkParams.Callback != null)
                    {
                        cbWrapper = (x, y, z) => rsaPkParams.Callback(x, y, z);
                    }

                    Cipher          enc   = null;
                    string          pwd   = null;
                    PasswordHandler pwdCb = null;
                    // If we choose to encrypt:
                    //      Cipher.DES_CBC;
                    //      Cipher.DES_EDE3_CBC;
                    //      Cipher.Idea_CBC;
                    //      Cipher.AES_128_CBC;
                    //      Cipher.AES_192_CBC;
                    //      Cipher.AES_256_CBC;
                    //   and pwd != null || pwdCb != null
                    // We can use a pwdCb to get a password interactively or we can
                    // simply pass in a fixed password string (no cbPwd, just pwd)
                    if (pwd != null)
                    {
                        pwdCb = DefaultPasswordHandler;
                    }

                    // Ref:  http://openssl.org/docs/manmaster/crypto/RSA_generate_key_ex.html
                    rsa.GenerateKeys(bits, e, cbWrapper, rsaPkParams.CallbackArg);

                    using (var bio = BIO.MemoryBuffer())
                    {
                        // Ref:  http://openssl.org/docs/manmaster/crypto/PEM_write_bio_RSAPrivateKey.html
                        rsa.WritePrivateKey(bio, enc, pwdCb, pwd);
                        return(new RsaPrivateKey(bits, e.ToHexString(), bio.ReadString()));
                    }
                }
            }
            else if (ecPkParams != null)
            {
                throw new NotImplementedException("EC private keys have not yet been implemented");

                //var curveName = Asn1Object.FromShortName("P-256");
                ////var curveName = new Asn1Object("P-256");
                //using (var ec =OpenSSL.Crypto.EC.Key.FromCurveName(curveName))
                //{
                //    ec.GenerateKey();
                //}
            }
            else
            {
                throw new NotSupportedException("unsupported private key parameter type");
            }
        }
Ejemplo n.º 33
0
        public static RsaKeyPair GenerateRsaPrivateKey(int bits = 2048, BigNumber e = null,
                RsaKeyGeneratorCallback cb = null, object cbArg = null)
        {
            if (e == null)
                e = E_F4;

            using (var rsa = new RSA())
            {
                BigNumber.GeneratorHandler cbWrapper = null;
                if (cb != null)
                    cbWrapper = (x,y,z) => cb(x,y,z);

                Cipher enc = null;
                string pwd = null;
                PasswordHandler pwdCb = null;
                // If we choose to encrypt:
                //      Cipher.DES_CBC;
                //      Cipher.DES_EDE3_CBC;
                //      Cipher.Idea_CBC;
                //      Cipher.AES_128_CBC;
                //      Cipher.AES_192_CBC;
                //      Cipher.AES_256_CBC;
                //   and pwd != null || pwdCb != null
                // We can use a pwdCb to get a password interactively or we can
                // simply pass in a fixed password string (no cbPwd, just pwd)
                if (pwd != null)
                    pwdCb = DefaultPasswordHandler;

                // Ref:  http://openssl.org/docs/manmaster/crypto/RSA_generate_key_ex.html
                rsa.GenerateKeys(bits, e, cbWrapper, cbArg);

                using (var bio = BIO.MemoryBuffer())
                {
                    // Ref:  http://openssl.org/docs/manmaster/crypto/PEM_write_bio_RSAPrivateKey.html
                    rsa.WritePrivateKey(bio, enc, pwdCb, pwd);
                    return new RsaKeyPair(bits, e.ToHexString(), bio.ReadString());
                }
            }
        }