Example #1
0
 /// <summary>
 /// Initializes a new instance of the KeyContainer class
 /// </summary>
 /// <param name="containerName">Name of container</param>
 /// <param name="keySize">Size of Key</param>
 /// <param name="publicFileName">Public file name</param>
 /// <param name="privateFileName">Private file name</param>
 public KeyContainer(string containerName, RsaKeySize keySize, string publicFileName, string privateFileName)
 {
     ContainerName = containerName;
     KeySize = keySize;
     PublicFileName = publicFileName;
     PrivateFileName = privateFileName;
 }
Example #2
0
 private RsaKey(string publicKey, string privateKey, RsaKeySize size, AsymmetricKeyMode mode)
 {
     PublicKey  = publicKey;
     PrivateKey = privateKey;
     Size       = (int)size;
     Mode       = mode;
 }
Example #3
0
        /// <summary>
        /// Generate a RSA key with a defined size in bits
        /// </summary>
        /// <param name="keySizeInBits">Size in bits</param>
        /// <returns>Key Pair containing Public and Private key</returns>
        public AsymmetricCipherKeyPair GenerateRsaKey(RsaKeySize keySizeInBits)
        {
            var r = new RsaKeyPairGenerator();

            CryptoApiRandomGenerator randomGenerator = new CryptoApiRandomGenerator();

            r.Init(new KeyGenerationParameters(new SecureRandom(randomGenerator), (int)keySizeInBits));

            var keys = r.GenerateKeyPair();

            return(keys);
        }
Example #4
0
        public static CryptRSAKey GenerateRSAKey(RsaKeySize keySize)
        {
            var rsaKey = EncryptProvider.CreateRsaKey(
                (RsaSize)(Enum.Parse(typeof(RsaSize), $"{(int)keySize}")));    //default is 2048

            // var rsaKey = EncryptProvider.CreateRsaKey(RsaSize.R3072);

            return(new CryptRSAKey()
            {
                Exponent = rsaKey.Exponent,
                Modulus = rsaKey.Modulus,
                PublicKey = rsaKey.PublicKey,
                PrivateKey = rsaKey.PrivateKey,
            });
        }
Example #5
0
        /// <summary>
        /// Create a Key
        /// </summary>
        /// <param name="containerName">Name of container</param>
        /// <param name="strength">Key strength</param>
        /// <param name="persistKeyInCsp">A boolean value to indicate whether to persist the Key in the CryptoServiceProvider</param>
        /// <returns>Returns the CryptoServiceProvider</returns>
        public static RSACryptoServiceProvider CreateKey(string containerName, RsaKeySize strength, bool persistKeyInCsp)
        {
            CspParameters cspParams = new CspParameters((int)eProvType.PROV_RSA_FULL, null, containerName);
            cspParams.KeyNumber = (int)KeyNumber.Exchange;
            cspParams.Flags = RSA_KEY_FLAGS;

            RSACryptoServiceProvider csp = new RSACryptoServiceProvider((int)strength, cspParams);
            csp.PersistKeyInCsp = persistKeyInCsp;

            return csp;
        }
Example #6
0
 /// <summary>
 /// Generate Key pairs
 /// </summary>
 /// <param name="containerName">Name of container</param>
 /// <param name="strength">Key strength</param>
 /// <param name="persistKeyInCsp">A boolean value to indicate whether to persist the Key in the CryptoServiceProvider</param>
 /// <returns>Returns the CryptoServiceProvider</returns>
 public static RSACryptoServiceProvider GenerateKeyPairs(string containerName, RsaKeySize strength, bool persistKeyInCsp)
 {
     return CreateKey(containerName, strength, persistKeyInCsp);
 }
Example #7
0
 /// <summary>
 /// Generate XML Format RSA Key.
 /// </summary>
 /// <param name="mode"></param>
 /// <param name="keySize"></param>
 /// <returns></returns>
 public static RsaKey GenerateKeyInXml(AsymmetricKeyMode mode, RsaKeySize keySize) => RsaKeyGenerator.GenerateInXml(mode, keySize);
Example #8
0
 /// <summary>
 /// Generate JSON Format RSA public key.
 /// </summary>
 /// <param name="keySize"></param>
 /// <returns></returns>
 public static RsaKey GeneratePublicKeyInJson(RsaKeySize keySize) => Factory.GeneratePublicKeyInJson(keySize);
Example #9
0
 /// <summary>
 /// Generate JSON Format RSA Key.
 /// </summary>
 /// <param name="mode"></param>
 /// <param name="keySize">Key Size.Unit: bits</param>
 /// <returns></returns>
 public static RsaKey GenerateKeyInJson(AsymmetricKeyMode mode, RsaKeySize keySize) => Factory.GenerateKeyInJson(mode, keySize);
Example #10
0
 /// <summary>
 /// Generate Pkcs1 format RSA private key.
 /// </summary>
 /// <param name="keySize">Key Size.Unit: bits</param>
 /// <param name="keepingFormat">Whether the format is true If it is standard pem file format</param>
 /// <returns></returns>
 public static RsaKey GeneratePrivateKeyInPkcs1(RsaKeySize keySize, bool keepingFormat) => Factory.GeneratePrivateKeyInPkcs1(keySize, keepingFormat);
Example #11
0
 /// <summary>
 /// Generate Pkcs1 format RSA public key.
 /// </summary>
 /// <param name="keySize">Key Size.Unit: bits</param>
 /// <param name="keepingFormat">Whether the format is true If it is standard pem file format</param>
 /// <returns></returns>
 public static RsaKey GeneratePublicKeyInPkcs1(RsaKeySize keySize, bool keepingFormat) => RsaKeyGenerator.GeneratePublicKeyInPkcs1(keySize, keepingFormat);
Example #12
0
        /// <summary>
        /// Overloaded method to generate key pairs
        /// </summary>
        /// <param name="privateKeyFilename">Private Key Filename</param>
        /// <param name="publicKeyFilename">Public Key Filename</param>
        /// <param name="containerName">Name of container</param>
        /// <param name="strength">Key Strength</param>
        /// <param name="persistKeyInCsp">A boolean value to indicate whether to persist the Key in the CryptoServiceProvider</param>
        /// <param name="overwrite">A boolean value to indicate whether to over write or not</param>
        /// <returns>Returns the CryptoServiceProvider</returns>
        public static RSACryptoServiceProvider GenerateKeyPairs(string privateKeyFilename, string publicKeyFilename, string containerName, RsaKeySize strength, bool persistKeyInCsp, bool overwrite)
        {
            if (string.IsNullOrEmpty(privateKeyFilename))
            {
                throw new PrivateKeyFilenameRequiredException();
            }

            if (string.IsNullOrEmpty(publicKeyFilename))
            {
                throw new PublicKeyFilenameRequiredException();
            }

            if (!overwrite && File.Exists(privateKeyFilename))
            {
                throw new PrivateKeyFileExistsException(privateKeyFilename);
            }

            if (!overwrite && File.Exists(publicKeyFilename))
            {
                throw new PublicKeyFileExistsException(publicKeyFilename);
            }

            RSACryptoServiceProvider csp = CreateKey(containerName, strength, persistKeyInCsp);

            WriteKeyToXml(csp.ToXmlString(true), privateKeyFilename);
            WriteKeyToXml(csp.ToXmlString(false), publicKeyFilename);

            return csp;
        }
Example #13
0
 /// <summary>
 /// Generate JSON Format RSA private key.
 /// </summary>
 /// <param name="keySize"></param>
 /// <returns></returns>
 public static RsaKey GeneratePrivateKeyInJson(RsaKeySize keySize) => RsaKeyGenerator.GeneratePrivateKeyInJson(keySize);
Example #14
0
 /// <summary>
 /// Generate RSA key.
 /// </summary>
 /// <param name="mode"></param>
 /// <param name="keySize"></param>
 /// <param name="keyFormat"></param>
 /// <param name="keepingFormat"></param>
 /// <returns></returns>
 public static RsaKey GenerateKey(AsymmetricKeyMode mode, RsaKeySize keySize, RsaKeyFormat keyFormat = RsaKeyFormat.XML, bool keepingFormat = false) => RsaKeyGenerator.Generate(mode, keySize, keyFormat, keepingFormat);
Example #15
0
 /// <summary>
 /// Generate XML Format RSA public key.
 /// </summary>
 /// <param name="keySize"></param>
 /// <returns></returns>
 public static RsaKey GeneratePublicKeyInXml(RsaKeySize keySize) => RsaKeyGenerator.GeneratePublicKeyInXml(keySize);
Example #16
0
 /// <summary>
 /// Generate RSA public key.
 /// </summary>
 /// <param name="keySize"></param>
 /// <param name="keyFormat"></param>
 /// <param name="keepingFormat"></param>
 /// <returns></returns>
 public static RsaKey GeneratePrivateKey(RsaKeySize keySize, RsaKeyFormat keyFormat = RsaKeyFormat.XML, bool keepingFormat = false) => Factory.GeneratePrivateKey(keySize, keyFormat, keepingFormat);
Example #17
0
 /// <summary>
 /// Generate Pkcs8 format RSA public key.
 /// </summary>
 /// <param name="keySize">Key Size.Unit: bits</param>
 /// <param name="keepingFormat">Whether the format is true If it is standard pem file format</param>
 /// <returns></returns>
 public static RsaKey GeneratePublicKeyInPkcs8(RsaKeySize keySize, bool keepingFormat) => Factory.GeneratePublicKeyInPkcs8(keySize, keepingFormat);
Example #18
0
 /// <summary>
 /// Overloaded method to generate key pairs
 /// </summary>
 /// <param name="privateKeyFilename">Private Key Filename</param>
 /// <param name="publicKeyFilename">Public Key Filename</param>
 /// <param name="strength">Key Strength</param>
 /// <returns>Returns the CryptoServiceProvider</returns>
 public static RSACryptoServiceProvider GenerateKeyPairs(string privateKeyFilename, string publicKeyFilename, RsaKeySize strength)
 {
     return GenerateKeyPairs(privateKeyFilename, publicKeyFilename, string.Empty, strength, false, true);
 }
Example #19
0
 /// <summary>
 /// Generate XML Format RSA private key.
 /// </summary>
 /// <param name="keySize"></param>
 /// <returns></returns>
 public static RsaKey GeneratePrivateKeyInXml(RsaKeySize keySize) => Factory.GeneratePrivateKeyInXml(keySize);
Example #20
0
 /// <summary>
 /// Overloaded method to generate key pairs
 /// </summary>
 /// <param name="privateKeyFilename">Private Key Filename</param>
 /// <param name="publicKeyFilename">Public Key Filename</param>
 /// <param name="containerName">Name of container</param>
 /// <param name="strength">Key Strength</param>
 /// <param name="persistKeyInCsp">A boolean value to indicate whether to persist the Key in the CryptoServiceProvider</param>
 /// <returns>Returns the CryptoServiceProvider</returns>
 public static RSACryptoServiceProvider GenerateKeyPairs(string privateKeyFilename, string publicKeyFilename, string containerName, RsaKeySize strength, bool persistKeyInCsp)
 {
     return GenerateKeyPairs(privateKeyFilename, publicKeyFilename, containerName, strength, persistKeyInCsp, true);
 }
Example #21
0
 /// <summary>
 /// Generate Pkcs8 format RSA private key.
 /// </summary>
 /// <param name="keySize">Key Size.Unit: bits</param>
 /// <param name="keepingFormat">Whether the format is true If it is standard pem file format</param>
 /// <returns></returns>
 public static RsaKey GeneratePrivateKeyInPkcs8(RsaKeySize keySize, bool keepingFormat) => RsaKeyGenerator.GeneratePrivateKeyInPkcs8(keySize, keepingFormat);
Example #22
0
 /// <summary>
 /// Initializes a new instance of the KeyContainer class
 /// </summary>
 /// <param name="containerName">Name of Container</param>
 /// <param name="keySize">Size of Key</param>
 public KeyContainer(string containerName, RsaKeySize keySize)
     : this(containerName, keySize, string.Empty, string.Empty)
 {
 }
Example #23
0
 /// <summary>
 /// Generate RSA private key.
 /// </summary>
 /// <param name="keySize"></param>
 /// <param name="keyFormat"></param>
 /// <param name="keepingFormat"></param>
 /// <returns></returns>
 public static RsaKey GeneratePublicKey(RsaKeySize keySize, RsaKeyFormat keyFormat = RsaKeyFormat.XML, bool keepingFormat = false) => RsaKeyGenerator.GeneratePublicKey(keySize, keyFormat, keepingFormat);
Example #24
0
 /// <summary>
 /// Generate Pkcs8 format RSA key.
 /// </summary>
 /// <param name="mode"></param>
 /// <param name="keySize">Key Size.Unit: bits</param>
 /// <param name="keepingFormat">Whether the format is true If it is standard pem file format</param>
 /// <returns></returns>
 public static RsaKey GenerateKeyInPkcs8(AsymmetricKeyMode mode, RsaKeySize keySize, bool keepingFormat) => RsaKeyGenerator.GenerateInPkcs8(mode, keySize, keepingFormat);
Example #25
0
 /// <summary>
 /// Generate RSA key in Pkcs1 format.
 /// </summary>
 /// <param name="mode"></param>
 /// <param name="keySize">Key Size.Unit: bits</param>
 /// <param name="keepingFormat">Whether the format is true If it is standard pem file format</param>
 /// <returns></returns>
 public static RsaKey GenerateKeyInPkcs1(AsymmetricKeyMode mode, RsaKeySize keySize, bool keepingFormat) => Factory.GenerateKeyInPkcs1(mode, keySize, keepingFormat);