⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 sshprivatekeyfile.java

📁 这是linux下ssl vpn的实现程序
💻 JAVA
字号:
/*
 *  SSL-Explorer
 *
 *  Copyright (C) 2003-2006 3SP LTD. All Rights Reserved
 *
 *  This program is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU General Public License
 *  as published by the Free Software Foundation; either version 2 of
 *  the License, or (at your option) any later version.
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public
 *  License along with this program; if not, write to the Free Software
 *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */
			
package com.sslexplorer.security.pki;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Iterator;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.maverick.crypto.io.ByteArrayReader;


/**
 *
 *
 * @author $author$
 * @version $Revision: 1.2 $
 */
public class SshPrivateKeyFile {
    private static Log log = LogFactory.getLog(SshPrivateKeyFile.class);
    private SshPrivateKeyFormat format;
    private byte[] keyblob;

    /**
     * Creates a new SshPrivateKeyFile object.
     *
     * @param keyblob
     * @param format
     */
    protected SshPrivateKeyFile(byte[] keyblob, SshPrivateKeyFormat format) {
        this.keyblob = keyblob;
        this.format = format;
    }

    /**
     *
     *
     * @return
     */
    public byte[] getBytes() {
        return keyblob;
    }

    /**
     *
     *
     * @param passphrase
     *
     * @return
     *
     * @throws InvalidSshKeyException
     */
    public byte[] getKeyBlob(String passphrase) throws InvalidKeyException {
        return format.decryptKeyblob(keyblob, passphrase);
    }

    /**
     *
     *
     * @param oldPassphrase
     * @param newPassphrase
     *
     * @throws InvalidSshKeyException
     */
    public void changePassphrase(String oldPassphrase, String newPassphrase)
        throws InvalidKeyException {
        byte[] raw = format.decryptKeyblob(keyblob, oldPassphrase);
        keyblob = format.encryptKeyblob(raw, newPassphrase);
    }

    /**
     *
     *
     * @param formattedKey
     *
     * @return
     *
     * @throws InvalidSshKeyException
     */
    public static SshPrivateKeyFile parse(byte[] formattedKey)
        throws InvalidKeyException {
        if (formattedKey == null) {
            throw new InvalidKeyException("Key data is null");
        }

        if (log.isInfoEnabled())
        	log.info("Parsing private key file");

        // Try the default private key format
        SshPrivateKeyFormat format;
        format = SshPrivateKeyFormatFactory.newInstance(SshPrivateKeyFormatFactory.getDefaultFormatType());

        boolean valid = format.isFormatted(formattedKey);

        if (valid) {
            return new SshPrivateKeyFile(formattedKey, format);
        } else {
            throw new InvalidKeyException(
                "The key format is not a supported format");
        }
    }

    /**
     *
     *
     * @param keyfile
     *
     * @return
     *
     * @throws InvalidSshKeyException
     * @throws IOException
     */
    public static SshPrivateKeyFile parse(File keyfile)
        throws InvalidKeyException, IOException {

    	return parse(new FileInputStream(keyfile));
    }
    
    public static SshPrivateKeyFile parse(InputStream in)
        throws InvalidKeyException, IOException {
    
        byte[] data = null;

        try {
            data = new byte[in.available()];
            in.read(data);
        } finally {
            try {
                if (in != null) {
                    in.close();
                }
            } catch (IOException ex) {
            }
        }

        return parse(data);
    }

    /**
     *
     *
     * @return
     */
    public boolean isPassphraseProtected() {
        return format.isPassphraseProtected(keyblob);
    }

    /*public void changePassphrase(String oldPassphrase, String newPassphrase)
     throws InvalidSshKeyException {
     keyblob = format.changePassphrase(keyblob, oldPassphrase, newPassphrase);
      }*/
    public static SshPrivateKeyFile create(SshPrivateKey key,
        String passphrase, SshPrivateKeyFormat format)
        throws InvalidKeyException {
        byte[] keyblob = format.encryptKeyblob(key.getEncoded(), passphrase);

        return new SshPrivateKeyFile(keyblob, format);
    }

    /**
     *
     *
     * @param newFormat
     * @param passphrase
     *
     * @throws InvalidSshKeyException
     */
    public void setFormat(SshPrivateKeyFormat newFormat, String passphrase)
        throws InvalidKeyException {
        byte[] raw = this.format.decryptKeyblob(keyblob, passphrase);
        format = newFormat;
        keyblob = format.encryptKeyblob(raw, passphrase);
    }

    /**
     *
     *
     * @return
     */
    public SshPrivateKeyFormat getFormat() {
        return format;
    }

    /**
     *
     *
     * @param passphrase
     *
     * @return
     *
     * @throws InvalidSshKeyException
     */
    public SshPrivateKey toPrivateKey(String passphrase)
        throws InvalidKeyException {
            byte[] raw = format.decryptKeyblob(keyblob, passphrase);
            SshKeyPair pair = SshKeyPairFactory.newInstance(getAlgorithm(raw));

            return pair.decodePrivateKey(raw);

    }

    /**
     *
     *
     * @return
     */
    public String toString() {
        return new String(keyblob);
    }

    private String getAlgorithm(byte[] raw) {
        try {
			ByteArrayReader r = new ByteArrayReader(raw);
			return r.readString();
		} catch (IOException e) {
			return null;
		}
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -