栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

在Java安全性中使用authorized_keys中的公钥

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

在Java安全性中使用authorized_keys中的公钥

令我惊讶的是,那里没有明显的东西。我很好奇,并实现了一种解码

authorized_keys
文件的方法。这取决于用于base64解码的Apache
Commons Codec。

import java.io.File;import java.math.BigInteger;import java.security.KeyFactory;import java.security.PublicKey;import java.security.spec.DSAPublicKeySpec;import java.security.spec.RSAPublicKeySpec;import java.util.Scanner;import org.apache.commons.prec.binary.base64;public class AuthorizedKeysDeprer {    private byte[] bytes;    private int pos;    public PublicKey deprePublicKey(String keyLine) throws Exception {        bytes = null;        pos = 0;        // look for the base64 enpred part of the line to depre        // both ssh-rsa and ssh-dss begin with "AAAA" due to the length bytes        for (String part : keyLine.split(" ")) { if (part.startsWith("AAAA")) {     bytes = base64.deprebase64(part);     break; }        }        if (bytes == null) { throw new IllegalArgumentException("no base64 part to depre");        }        String type = depreType();        if (type.equals("ssh-rsa")) { BigInteger e = depreBigInt(); BigInteger m = depreBigInt(); RSAPublicKeySpec spec = new RSAPublicKeySpec(m, e); return KeyFactory.getInstance("RSA").generatePublic(spec);        } else if (type.equals("ssh-dss")) { BigInteger p = depreBigInt(); BigInteger q = depreBigInt(); BigInteger g = depreBigInt(); BigInteger y = depreBigInt(); DSAPublicKeySpec spec = new DSAPublicKeySpec(y, p, q, g); return KeyFactory.getInstance("DSA").generatePublic(spec);        } else { throw new IllegalArgumentException("unknown type " + type);        }    }    private String depreType() {        int len = depreInt();        String type = new String(bytes, pos, len);        pos += len;        return type;    }    private int depreInt() {        return ((bytes[pos++] & 0xFF) << 24) | ((bytes[pos++] & 0xFF) << 16)     | ((bytes[pos++] & 0xFF) << 8) | (bytes[pos++] & 0xFF);    }    private BigInteger depreBigInt() {        int len = depreInt();        byte[] bigIntBytes = new byte[len];        System.arraycopy(bytes, pos, bigIntBytes, 0, len);        pos += len;        return new BigInteger(bigIntBytes);    }    public static void main(String[] args) throws Exception {        AuthorizedKeysDeprer deprer = new AuthorizedKeysDeprer();        File file = new File("authorized_keys");        Scanner scanner = new Scanner(file).useDelimiter("n");        while (scanner.hasNext()) { System.out.println(deprer.deprePublicKey(scanner.next()));        }        scanner.close();    }}


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/449970.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号