示例#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);
            }
        }
示例#2
0
        /// <summary>
        /// Generates and Registers a card for specified identity.
        /// </summary>
        private static async Task <ChatMember> Register(string email)
        {
            Console.WriteLine("\nGenerating and Publishing the keys...\n");

            // generate a new public/private key pair.

            var keyPair = VirgilKeyPair.Generate();

            // The app is registering a Virgil Card which includes a
            // public key and an email address identifier. The card will
            // be used for the public key identification and searching
            // for it in the Public Keys Service.

            var emailVerifier = await serviceHub.Identity.VerifyEmail(email);

            Console.WriteLine("\nThe email with confirmation code has been sent to your email address. Please check it!\n");

            var confirmationCode =
                Param <string> .Mandatory("Enter Code: ").WaitInput();

            var identity = await emailVerifier.Confirm(confirmationCode);

            var card = await serviceHub.Cards.Create(identity, keyPair.PublicKey(), keyPair.PrivateKey());

            // Private key can be added to Virgil Security storage if you want to
            // easily synchronise yout private key between devices.

            await serviceHub.PrivateKeys.Stash(card.Id, keyPair.PrivateKey());

            return(new ChatMember(card, keyPair.PrivateKey()));
        }
示例#3
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);
            }
        }
示例#4
0
        public static void GenerateKeys()
        {
            var keyPair = VirgilKeyPair
                          .Generate(VirgilKeyPair.Type.FAST_EC_ED25519);
            //Will use generator but it is used for symetric crypto
            var secret = VirgilKeyPair
                         .Generate(VirgilKeyPair.Type.FAST_EC_X25519)
                         .PrivateKey();

            File.WriteAllBytes(GetFileName(PUBLIC), keyPair.PublicKey());
            File.WriteAllBytes(GetFileName(PRIVATE), keyPair.PrivateKey());
            File.WriteAllBytes(GetFileName(SECRET), secret);
        }
        static public string[] GetPublicPrivateKeys()
        {
            VirgilKeyPair pair = VirgilKeyPair.Generate(VirgilKeyPair.Type.EC_SECP256K1);
            var           keys = new string[2];

            keys[0] = Encoding.UTF8.GetString(pair.PublicKey())
                      .Replace("-----BEGIN PUBLIC KEY-----", "")
                      .Replace("-----END PUBLIC KEY-----", "")
                      .Replace("\n", "");
            keys[1] = Encoding.UTF8.GetString(pair.PrivateKey())
                      .Replace("-----BEGIN EC PRIVATE KEY-----", "")
                      .Replace("-----END EC PRIVATE KEY-----", "")
                      .Replace("\n", "");

            return(keys);
        }
        public override void Execute()
        {
            var password = Param <string> .Optional("Enter password").WaitInput();

            var type = Param <VirgilKeyPair.Type> .Optional("Enter keys type").WaitInput();

            var isBase64 = Param <bool> .Optional("Output in Base64 format yes/no?").WaitInput();

            Console.WriteLine();

            this.StartWatch();

            var keyPair = string.IsNullOrWhiteSpace(password)
                ? VirgilKeyPair.Generate(type)
                : VirgilKeyPair.Generate(type, Encoding.UTF8.GetBytes(password));

            this.StopWatch();

            // Output

            var publicKey  = keyPair.PublicKey();
            var privateKey = keyPair.PrivateKey();

            if (isBase64)
            {
                Console.WriteLine("Public Key\n\n" + Convert.ToBase64String(publicKey) + "\n");
                Console.WriteLine("Private Key\n\n" + Convert.ToBase64String(privateKey) + "\n");
            }
            else
            {
                Console.WriteLine(Encoding.UTF8.GetString(publicKey));
                Console.WriteLine(Encoding.UTF8.GetString(privateKey));
            }

            this.DisplayElapsedTime();
        }