/// <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; }
private RsaKey(string publicKey, string privateKey, RsaKeySize size, AsymmetricKeyMode mode) { PublicKey = publicKey; PrivateKey = privateKey; Size = (int)size; Mode = mode; }
/// <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); }
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, }); }
/// <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; }
/// <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); }
/// <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);
/// <summary> /// Generate JSON Format RSA public key. /// </summary> /// <param name="keySize"></param> /// <returns></returns> public static RsaKey GeneratePublicKeyInJson(RsaKeySize keySize) => Factory.GeneratePublicKeyInJson(keySize);
/// <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);
/// <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);
/// <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);
/// <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; }
/// <summary> /// Generate JSON Format RSA private key. /// </summary> /// <param name="keySize"></param> /// <returns></returns> public static RsaKey GeneratePrivateKeyInJson(RsaKeySize keySize) => RsaKeyGenerator.GeneratePrivateKeyInJson(keySize);
/// <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);
/// <summary> /// Generate XML Format RSA public key. /// </summary> /// <param name="keySize"></param> /// <returns></returns> public static RsaKey GeneratePublicKeyInXml(RsaKeySize keySize) => RsaKeyGenerator.GeneratePublicKeyInXml(keySize);
/// <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);
/// <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);
/// <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); }
/// <summary> /// Generate XML Format RSA private key. /// </summary> /// <param name="keySize"></param> /// <returns></returns> public static RsaKey GeneratePrivateKeyInXml(RsaKeySize keySize) => Factory.GeneratePrivateKeyInXml(keySize);
/// <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); }
/// <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);
/// <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) { }
/// <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);
/// <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);
/// <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);