コード例 #1
0
        /// <summary>
        /// Converts an SSH key between .pem and opensshv2 formats
        /// </summary>
        /// <returns>conversion succeeded</returns>
        public bool Convert()
        {
            // get the file contents
            string fileContent = string.Empty;
            using (var reader = new StreamReader(_privateKeyFile))
            {
                fileContent = reader.ReadToEnd();
            }
            // check to see whether this has worked
            var key = new SshKey {Password = _password};
            // get the ssh key
            bool converted = key.FromOpenSshPrivateKey(fileContent);
            // if this hasn't worked then just drop out of this method
            if (!converted)
                return false;
            // update the ssh key
            string content = key.ToOpenSshPrivateKey(false);
            // get the filename without the key extension
            string openSsh2Filename = Path.GetFileNameWithoutExtension(_privateKeyFile) + ".pvk";
            // get the path
            string openSsh2Directory = Path.GetDirectoryName(_privateKeyFile);

            // get the full path
            KeyFilePath = Path.Combine(openSsh2Directory, openSsh2Filename);
            // create the file
            using(var writer = new StreamWriter(File.Create(KeyFilePath)))
            {
                writer.Write(content);
            }
            // if that worked return out of this
            return true;
        }
コード例 #2
0
        public static SshKeyPair Generate(int bits)
        {
            SshKey key        = new SshKey();
            int    numBits    = bits;
            int    exponent   = 65537;
            bool   success    = key.GenerateRsaKey(numBits, exponent);
            var    sshKeyPair = new SshKeyPair(key.ToOpenSshPublicKey(), key.ToOpenSshPrivateKey(false));

            if (!success)
            {
                Generate(bits);
            }
            return(sshKeyPair);
        }
コード例 #3
0
        /// <summary>
        /// Converts an SSH key between .pem and opensshv2 formats
        /// </summary>
        /// <returns>conversion succeeded</returns>
        public bool Convert()
        {
            // get the file contents
            string fileContent = string.Empty;

            using (var reader = new StreamReader(_privateKeyFile))
            {
                fileContent = reader.ReadToEnd();
            }
            // check to see whether this has worked
            var key = new SshKey {
                Password = _password
            };
            // get the ssh key
            bool converted = key.FromOpenSshPrivateKey(fileContent);

            // if this hasn't worked then just drop out of this method
            if (!converted)
            {
                return(false);
            }
            // update the ssh key
            string content = key.ToOpenSshPrivateKey(false);
            // get the filename without the key extension
            string openSsh2Filename = Path.GetFileNameWithoutExtension(_privateKeyFile) + ".pvk";
            // get the path
            string openSsh2Directory = Path.GetDirectoryName(_privateKeyFile);

            // get the full path
            KeyFilePath = Path.Combine(openSsh2Directory, openSsh2Filename);
            // create the file
            using (var writer = new StreamWriter(File.Create(KeyFilePath)))
            {
                writer.Write(content);
            }
            // if that worked return out of this
            return(_converted = true);
        }
コード例 #4
0
ファイル: SSHGen.cs プロジェクト: papunov/SSHKeysGeneRator
        public static string Generate(string name, string folder, out string password, out bool error)
        {
            error = false;

            SshKey key = new SshKey();

            bool success;

            int numBits;
            int exponent;

            string exportedKey;
            bool   exportEncrypted;

            password = GenPassword();

            string fullPath = $"{folder}\\{name}";

            //  numBits may range from 384 to 4096.  Typical values are
            //  1024 or 2048.  (must be a multiple of 64)
            //  A good choice for the exponent is 65537.  Chilkat recommends
            //  always using this value.
            numBits  = 2048;
            exponent = 65537;
            success  = key.GenerateRsaKey(numBits, exponent);
            if (success != true)
            {
                error = true;
                return(string.Empty);
            }

            //  Note: Generating a public/private key pair is CPU intensive
            //  and may take a short amount of time (more than few seconds,
            //  but less than a minute).

            string exportFile = string.Empty;

            string extension = ".pem";
            int    i         = 0;

            do
            {
                if (i == 0)
                {
                    exportFile = fullPath + extension;
                }
                else
                {
                    exportFile = fullPath + $"-{i}{extension}";
                }

                i++;
            }while (FoldersInit.CheckIfFileExist(exportFile));

            //  Export with encryption to OpenSSH private key format:
            key.Password    = password;
            exportEncrypted = true;
            exportedKey     = key.ToOpenSshPrivateKey(exportEncrypted);
            success         = key.SaveText(exportedKey, exportFile);

            extension = ".ppk";
            i         = 0;
            do
            {
                if (i == 0)
                {
                    exportFile = fullPath + extension;
                }
                else
                {
                    exportFile = fullPath + $"-{i}{extension}";
                }

                i++;
            }while (FoldersInit.CheckIfFileExist(exportFile));

            //  Export the RSA private key to encrypted PuTTY format:
            key.Password    = password;
            exportEncrypted = true;
            exportedKey     = key.ToPuttyPrivateKey(exportEncrypted);
            success         = key.SaveText(exportedKey, exportFile);


            //  ----------------------------------------------------
            //  Now for the public key....
            //  ----------------------------------------------------

            //  The Secure Shell (SSH) Public Key File Format
            //  is documented in RFC 4716.
            extension = "_pubkey_rfc4716.pub";
            i         = 0;
            do
            {
                if (i == 0)
                {
                    exportFile = fullPath + extension;
                }
                else
                {
                    exportFile = fullPath + $"-{i}{extension}";
                }

                i++;
            }while (FoldersInit.CheckIfFileExist(exportFile));

            exportedKey = key.ToRfc4716PublicKey();
            success     = key.SaveText(exportedKey, exportFile);

            //  OpenSSH has a separate public-key file format, which
            //  is also supported by Chilkat SshKey:

            extension = "_pubkey_openSsh.pub";
            i         = 0;
            do
            {
                if (i == 0)
                {
                    exportFile = fullPath + extension;
                }
                else
                {
                    exportFile = fullPath + $"-{i}{extension}";
                }

                i++;
            }while (FoldersInit.CheckIfFileExist(exportFile));

            exportedKey = key.ToOpenSshPublicKey();
            success     = key.SaveText(exportedKey, exportFile);

            return(exportedKey);
        }