Package com.sshtools.j2ssh.io

Examples of com.sshtools.j2ssh.io.ByteArrayReader


     */
    public byte[] decryptKeyblob(byte[] formattedKey, String passphrase)
        throws InvalidSshKeyException {
        try {
            byte[] keyblob = getKeyBlob(formattedKey);
            ByteArrayReader bar = new ByteArrayReader(keyblob);
            String type = bar.readString();

            if (type.equalsIgnoreCase("3des-cbc")) {
                // Decrypt the key
                byte[] keydata = makePassphraseKey(passphrase);
                byte[] iv = new byte[8];

                if (type.equals("3DES-CBC")) {
                    bar.read(iv);
                }

                keyblob = bar.readBinaryString();

                Cipher cipher = Cipher.getInstance("DESede/CBC/PKCS5Padding");
                KeySpec keyspec = new DESedeKeySpec(keydata);
                Key key = SecretKeyFactory.getInstance("DESede").generateSecret(keyspec);
                cipher.init(Cipher.DECRYPT_MODE, key,
                    new IvParameterSpec(iv, 0, cipher.getBlockSize()));

                ByteArrayReader data = new ByteArrayReader(cipher.doFinal(
                            keyblob));

                if (data.readInt() == cookie) {
                    keyblob = data.readBinaryString();
                } else {
                    throw new InvalidSshKeyException(
                        "The host key is invalid, check the passphrase supplied");
                }
            } else {
View Full Code Here


    public SshDssPublicKey(byte[] key) throws InvalidSshKeyException {
        try {
            DSAPublicKeySpec dsaKey;

            // Extract the key information
            ByteArrayReader bar = new ByteArrayReader(key);
            String header = bar.readString();

            if (!header.equals(getAlgorithmName())) {
                throw new InvalidSshKeyException();
            }

            BigInteger p = bar.readBigInteger();
            BigInteger q = bar.readBigInteger();
            BigInteger g = bar.readBigInteger();
            BigInteger y = bar.readBigInteger();
            dsaKey = new DSAPublicKeySpec(y, p, q, g);

            KeyFactory kf = KeyFactory.getInstance("DSA");
            pubkey = (DSAPublicKey) kf.generatePublic(dsaKey);
        } catch (Exception e) {
View Full Code Here

    public boolean verifySignature(byte[] signature, byte[] data)
        throws InvalidSshKeySignatureException {
        try {
            // Check for differing version of the transport protocol
            if (signature.length != 40) {
                ByteArrayReader bar = new ByteArrayReader(signature);
                byte[] sig = bar.readBinaryString();

                //log.debug("Signature blob is " + new String(sig));
                String header = new String(sig);
                log.debug("Header is " + header);

                if (!header.equals("ssh-dss")) {
                    throw new InvalidSshKeySignatureException();
                }

                signature = bar.readBinaryString();

                //log.debug("Read signature from blob: " + new String(signature));
            }

            // Using a SimpleASNWriter
View Full Code Here

    public SshDssPrivateKey(byte[] key) throws InvalidSshKeyException {
        try {
            DSAPrivateKeySpec dsaKey;

            // Extract the key information
            ByteArrayReader bar = new ByteArrayReader(key);
            String header = bar.readString();

            if (!header.equals(getAlgorithmName())) {
                throw new InvalidSshKeyException();
            }

            BigInteger p = bar.readBigInteger();
            BigInteger q = bar.readBigInteger();
            BigInteger g = bar.readBigInteger();
            BigInteger x = bar.readBigInteger();
            dsaKey = new DSAPrivateKeySpec(x, p, q, g);

            KeyFactory kf = KeyFactory.getInstance("DSA");
            prvkey = (DSAPrivateKey) kf.generatePrivate(dsaKey);
        } catch (Exception e) {
View Full Code Here

     * @return
     *
     * @throws IOException
     */
    public SshPublicKey toPublicKey() throws IOException {
        ByteArrayReader bar = new ByteArrayReader(keyblob);
        String type = bar.readString();
        SshKeyPair pair = SshKeyPairFactory.newInstance(type);

        return pair.decodePublicKey(keyblob);
    }
View Full Code Here

     *
     * @throws InvalidMessageException
     */
    public void fromByteArray(byte[] data) throws InvalidMessageException {
        try {
            ByteArrayReader bar = new ByteArrayReader(data);

            if (bar.available() > 0) {
                type = bar.read();
                constructMessage(bar);
            } else {
                throw new InvalidMessageException(
                    "Not enough message data to complete the message");
            }
View Full Code Here

        if (requestType.equals("exit-status")) {
            exitCode = new Integer((int) ByteArrayReader.readInt(requestData, 0));
            log.debug("Exit code of " + exitCode.toString() + " received");
        } else if (requestType.equals("exit-signal")) {
            ByteArrayReader bar = new ByteArrayReader(requestData);
            String signal = bar.readString();
            boolean coredump = bar.read() != 0;
            String message = bar.readString();
            String language = bar.readString();
            log.debug("Exit signal " + signal + " received");
            log.debug("Signal message: " + message);
            log.debug("Core dumped: " + String.valueOf(coredump));

            if (signalListener != null) {
View Full Code Here

                "The client can only request the " +
                "opening of a local forwarding channel");
        }

        try {
            ByteArrayReader bar = new ByteArrayReader(requestData);
            String hostToConnect = bar.readString();
            int portToConnect = (int) bar.readInt();
            String originatingHost = bar.readString();
            int originatingPort = (int) bar.readInt();

            // Get a configuration item for the forwarding
            ForwardingConfiguration config = getLocalForwardingByAddress(originatingHost,
                    originatingPort);
            Socket socket = new Socket(hostToConnect, portToConnect);
View Full Code Here

        String addressToBind = null;
        int portToBind = -1;
        log.debug("Processing " + requestName + " global request");

        try {
            ByteArrayReader bar = new ByteArrayReader(requestData);
            addressToBind = bar.readString();
            portToBind = (int) bar.readInt();

            if (requestName.equals(ForwardingClient.REMOTE_FORWARD_REQUEST)) {
                addRemoteForwardingConfiguration(addressToBind, portToBind);
                response = new GlobalRequestResponse(true);
            }
View Full Code Here

     * @throws InvalidSshKeyException
     */
    public byte[] encryptKeyblob(byte[] keyblob, String passphrase)
        throws InvalidSshKeyException {
        try {
            ByteArrayReader bar = new ByteArrayReader(keyblob);
            String algorithm = bar.readString(); // dsa or rsa
            byte[] payload;
            PEMWriter pem = new PEMWriter();

            if ("ssh-dss".equals(algorithm)) {
                BigInteger p = bar.readBigInteger();
                BigInteger q = bar.readBigInteger();
                BigInteger g = bar.readBigInteger();
                BigInteger x = bar.readBigInteger();
                DSAKeyInfo keyInfo = new DSAKeyInfo(p, q, g, x, BigInteger.ZERO);
                SimpleASNWriter asn = new SimpleASNWriter();
                DSAKeyInfo.writeDSAKeyInfo(asn, keyInfo);
                payload = asn.toByteArray();
                pem.setType(PEM.DSA_PRIVATE_KEY);
            } else if ("ssh-rsa".equals(algorithm)) {
                BigInteger e = bar.readBigInteger();
                BigInteger n = bar.readBigInteger();
                BigInteger p = bar.readBigInteger();
                RSAKeyInfo keyInfo = new RSAKeyInfo(n, p, e, BigInteger.ZERO,
                        BigInteger.ZERO, BigInteger.ZERO, BigInteger.ZERO,
                        BigInteger.ZERO);
                SimpleASNWriter asn = new SimpleASNWriter();
                RSAKeyInfo.writeRSAKeyInfo(asn, keyInfo);
View Full Code Here

TOP

Related Classes of com.sshtools.j2ssh.io.ByteArrayReader

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.