public PrivateKey GetPrivateKey() { rsa.Check(); if (privKey == null) { byte[] yQ = new byte[rsa.SecretPrimeFactorQ.Bytes]; //Prime Factor byte[] yP = new byte[rsa.SecretPrimeFactorQ.Bytes]; //Prime Factor byte[] yInverseQ = new byte[rsa.IQmodP.Bytes]; //Prime Factor byte[] yDP = new byte[rsa.DmodP1.Bytes]; byte[] yDQ = new byte[rsa.DmodQ1.Bytes]; //Public Part Key byte[] yPm = new byte[rsa.PublicModulus.Bytes]; byte[] yPe = new byte[rsa.PublicExponent.Bytes]; byte[] yD = new byte[rsa.PrivateExponent.Bytes]; privKey = new PrivateKey() { yQ = yQ, yP = yP, yInverseQ = yInverseQ, yDP = yDP, yDQ = yDQ, yModulus = yPm, yExponent = yPe, yD = yD, }; rsa.SecretPrimeFactorQ.ToBytes(privKey.yQ); rsa.SecretPrimeFactorP.ToBytes(privKey.yP); rsa.IQmodP.ToBytes(privKey.yInverseQ); rsa.DmodP1.ToBytes(privKey.yDP); rsa.DmodQ1.ToBytes(privKey.yDQ); rsa.PublicModulus.ToBytes(privKey.yModulus); rsa.PublicExponent.ToBytes(privKey.yExponent); rsa.PrivateExponent.ToBytes(privKey.yD); } return(privKey); }
public RSA Generate_Private_Key(string[] args) { options.ParseArguments(args); var bits = 512; if (options.Arguments.Count == 1) { bits = Convert.ToInt32(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); var rsa = new RSA(); rsa.GenerateKeys(bits, e, Program.OnGenerator, null); Console.Error.WriteLine("e is {0} (0x{1})", e.ToDecimalString(), e.ToHexString()); if (rsa.Check()) { Console.WriteLine("RSA key ok"); } else { Console.WriteLine("RSA key error"); } 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 (var bio = BIO.MemoryBuffer()) { rsa.WritePrivateKey(bio, enc, Program.OnPassword, options["passout"]); return(rsa); } }