Exemplo n.º 1
0
        /// <summary>
        /// Generates asymmetric key pair that is comprised of both public and private keys by specified type.
        /// </summary>
        public KeyPair GenerateKeys(KeyPairType keyPairType)
        {
            try
            {
                using (var keyPair = VirgilKeyPair.Generate(keyPairType.ToVirgilKeyPairType()))
                {
                    var keyPairId  = this.ComputePublicKeyHash(keyPair.PublicKey());
                    var privateKey = new PrivateKey
                    {
                        ReceiverId = keyPairId,
                        Value      = VirgilKeyPair.PrivateKeyToDER(keyPair.PrivateKey()),
                    };

                    var publicKey = new PublicKey
                    {
                        ReceiverId = keyPairId,
                        Value      = VirgilKeyPair.PublicKeyToDER(keyPair.PublicKey())
                    };

                    return(new KeyPair(publicKey, privateKey));
                }
            }
            catch (Exception ex)
            {
                throw new CryptoException(ex.Message);
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// Generate a key pair.
        /// </summary>
        /// <param name="keyPairType">keyPairType Key pair type to generate</param>
        /// <param name="iKeySize">iKeySize Key size of key pair</param>
        /// <returns>A key pair</returns>
        public static AsymmetricCipherKeyPair GenerateKeyPair(KeyPairType keyPairType, int iKeySize)
        {
            IAsymmetricCipherKeyPairGenerator keyPairGen = null;

            try
            {
                switch (keyPairType)
                {
                case KeyPairType.Ec:
                    keyPairGen = new ECKeyPairGenerator();
                    break;

                case KeyPairType.Dsa:
                    keyPairGen = new DsaKeyPairGenerator();
                    break;

                //case KeyPairType.Rsa:
                default:
                    keyPairGen = new RsaKeyPairGenerator();
                    break;
                }


                // Initialize key pair generator with key strength and a randomness
                keyPairGen.Init(new KeyGenerationParameters(Repository.Srand, iKeySize));

                // Generate and return the key pair
            }
            catch (InvalidParameterException ex)
            {
            }
            return(keyPairGen.GenerateKeyPair());
        }
Exemplo n.º 3
0
        /// <summary>
        /// Generate a key pair.
        /// </summary>
        /// <param name="keyPairType">keyPairType Key pair type to generate</param>
        /// <param name="iKeySize">iKeySize Key size of key pair</param>
        /// <returns>A key pair</returns>
        public static AsymmetricCipherKeyPair GenerateKeyPair(KeyPairType keyPairType, int iKeySize)
        {
            IAsymmetricCipherKeyPairGenerator keyPairGen = null;
            try
            {
                switch (keyPairType)
                {
                    case KeyPairType.Ec:
                        keyPairGen = new ECKeyPairGenerator();
                        break;
                    case KeyPairType.Dsa:
                        keyPairGen = new DsaKeyPairGenerator();
                        break;
                    //case KeyPairType.Rsa:
                    default:
                        keyPairGen = new RsaKeyPairGenerator();
                        break;
                }

                // Initialize key pair generator with key strength and a randomness
                keyPairGen.Init(new KeyGenerationParameters(Repository.Srand, iKeySize));

                // Generate and return the key pair
            }
            catch (InvalidParameterException ex)
            {

            }
            return keyPairGen.GenerateKeyPair();
        }
Exemplo n.º 4
0
        /// <summary>
        /// Generates asymmetric key pair that is comprised of both public and private keys by specified type.
        /// </summary>
        /// <param name="keyPairType">type of the generated keys.
        ///   The possible values can be found in <see cref="KeyPairType"/>.</param>
        /// <param name="keyMaterial">the only data to be used for key generation,
        /// length must be more than 31.</param>
        /// <returns>Generated key pair with the specified type.</returns>
        /// <example>
        /// Generated key pair with type EC_SECP256R1.
        ///     <code>
        ///         var crypto = new VirgilCrypto();
        ///         var keyPair = crypto.GenerateKeys(KeyPairType.EC_SECP256R1);
        ///     </code>
        /// </example>
        public KeyPair GenerateKeys(KeyPairType keyPairType, byte[] keyMaterial = null)
        {
            try
            {
                VirgilKeyPair keyPair;
                if (keyMaterial == null || keyMaterial.Length == 0)
                {
                    keyPair = VirgilKeyPair.Generate(VirgilCryptoExtentions.ToVirgilKeyPairType(keyPairType));
                }
                else
                {
                    keyPair = VirgilKeyPair.GenerateFromKeyMaterial(
                        VirgilCryptoExtentions.ToVirgilKeyPairType(keyPairType),
                        keyMaterial);
                }

                byte[]     keyPairId  = this.ComputePublicKeyHash(keyPair.PublicKey());
                PrivateKey privateKey = new PrivateKey();
                privateKey.Id     = keyPairId;
                privateKey.RawKey = VirgilKeyPair.PrivateKeyToDER(keyPair.PrivateKey());

                PublicKey publicKey = new PublicKey();
                publicKey.Id     = keyPairId;
                publicKey.RawKey = VirgilKeyPair.PublicKeyToDER(keyPair.PublicKey());

                return(new KeyPair(publicKey, privateKey));
            }
            catch (Exception ex)
            {
                throw new VirgilCryptoException(ex.Message);
            }
        }
Exemplo n.º 5
0
 /// <summary />
 public virtual Task<KeyPair> CreateKeyPairAsync(string name, KeyPairType? type = null, CancellationToken cancellationToken = default(CancellationToken))
 {
     var keyPair = new KeyPairDefinition(name)
     {
         Type = type
     };
     return _computeApi.CreateKeyPairAsync<KeyPair>(keyPair, cancellationToken);
 }
        public static VirgilKeyPair.Type ToVirgilKeyPairType(this KeyPairType keyPairType)
        {
            VirgilKeyPair.Type type;

            switch (keyPairType)
            {
            case KeyPairType.Default:         type = VirgilKeyPair.Type.FAST_EC_ED25519; break;

            case KeyPairType.RSA_2048:        type = VirgilKeyPair.Type.RSA_2048; break;

            case KeyPairType.RSA_3072:        type = VirgilKeyPair.Type.RSA_3072; break;

            case KeyPairType.RSA_4096:        type = VirgilKeyPair.Type.RSA_4096; break;

            case KeyPairType.RSA_8192:        type = VirgilKeyPair.Type.RSA_8192; break;

            case KeyPairType.EC_SECP256R1:    type = VirgilKeyPair.Type.EC_SECP256R1; break;

            case KeyPairType.EC_SECP384R1:    type = VirgilKeyPair.Type.EC_SECP384R1; break;

            case KeyPairType.EC_SECP521R1:    type = VirgilKeyPair.Type.EC_SECP521R1; break;

            case KeyPairType.EC_BP256R1:      type = VirgilKeyPair.Type.EC_BP256R1; break;

            case KeyPairType.EC_BP384R1:      type = VirgilKeyPair.Type.EC_BP384R1; break;

            case KeyPairType.EC_BP512R1:      type = VirgilKeyPair.Type.EC_BP512R1; break;

            case KeyPairType.EC_SECP256K1:    type = VirgilKeyPair.Type.EC_SECP256K1; break;

            case KeyPairType.EC_CURVE25519:   type = VirgilKeyPair.Type.EC_CURVE25519; break;

            case KeyPairType.FAST_EC_X25519:  type = VirgilKeyPair.Type.FAST_EC_X25519; break;

            case KeyPairType.FAST_EC_ED25519: type = VirgilKeyPair.Type.FAST_EC_ED25519; break;

            default:
                throw new ArgumentOutOfRangeException(nameof(keyPairType), keyPairType, null);
            }

            return(type);
        }
Exemplo n.º 7
0
 /// <summary>
 /// Initializes a new instance of the <see cref="VirgilCrypto" /> class.
 /// </summary>
 public VirgilCrypto()
 {
     this.defaultKeyPairType = KeyPairType.Default;
 }
Exemplo n.º 8
0
 /// <summary>
 /// Initializes a new instance of the <see cref="VirgilCrypto" /> class.
 /// </summary>
 /// <param name="defaultKeyPairType">Default type of the key pair.</param>
 public VirgilCrypto(KeyPairType defaultKeyPairType)
 {
     this.defaultKeyPairType = defaultKeyPairType;
 }
        /// <summary>
        /// Gets Signature Algorithms for give KeyPairType
        /// </summary>
        /// <param name="keyPairType"></param>
        /// <returns></returns>
        public static IEnumerable <String> ValuesFor(KeyPairType keyPairType)
        {
            IEnumerable <String> values = KeypairMap[keyPairType];

            return(values ?? Enumerable.Empty <String>());
        }
 /// <summary>
 /// Gets Signature Algorithms for give KeyPairType
 /// </summary>
 /// <param name="keyPairType"></param>
 /// <returns></returns>
 public static IEnumerable<String> ValuesFor(KeyPairType keyPairType)
 {
     IEnumerable<String> values = KeypairMap[keyPairType];
     return values ?? Enumerable.Empty<String>();
 }