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

将openSSH rsa密钥转换为javax.crypto.Cipher兼容格式

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

将openSSH rsa密钥转换为javax.crypto.Cipher兼容格式

static KeyPair demo(InputStream pub, InputStream pvt) throws IOException, GeneralSecurityException {
KeyFactory f = KeyFactory.getInstance(“RSA”);

    RSAPublicKeySpec pubspec = depreRSAPublicSSH(readAllbase64Bytes(pub));    RSAPrivateCrtKeySpec pvtspec = depreRSAPrivatePKCS1(readAllbase64Bytes(pvt));    return new KeyPair(f.generatePublic(pubspec), f.generatePrivate(pvtspec));}static RSAPublicKeySpec depreOpenSSH(byte[] input) {    String[] fields = new String(input, StandardCharsets.US_ASCII).split(" ");    if ((fields.length < 2) || (!fields[0].equals("ssh-rsa"))) throw new IllegalArgumentException("Unsupported type");    byte[] std = base64.getDeprer().depre(fields[1]);    return depreRSAPublicSSH(std);}static RSAPublicKeySpec depreRSAPublicSSH(byte[] enpred) {    ByteBuffer input = ByteBuffer.wrap(enpred);    String type = string(input);    if (!"ssh-rsa".equals(type)) throw new IllegalArgumentException("Unsupported type");    BigInteger exp = sshint(input);    BigInteger mod = sshint(input);    if (input.hasRemaining()) throw new IllegalArgumentException("Excess data");    return new RSAPublicKeySpec(mod, exp);}static RSAPrivateCrtKeySpec depreRSAPrivatePKCS1(byte[] enpred) {    ByteBuffer input = ByteBuffer.wrap(enpred);    if (der(input, 0x30) != input.remaining()) throw new IllegalArgumentException("Excess data");    if (!BigInteger.ZERO.equals(derint(input))) throw new IllegalArgumentException("Unsupported version");    BigInteger n = derint(input);    BigInteger e = derint(input);    BigInteger d = derint(input);    BigInteger p = derint(input);    BigInteger q = derint(input);    BigInteger ep = derint(input);    BigInteger eq = derint(input);    BigInteger c = derint(input);    return new RSAPrivateCrtKeySpec(n, e, d, p, q, ep, eq, c);}private static String string(ByteBuffer buf) {    return new String(lenval(buf), Charset.forName("US-ASCII"));}private static BigInteger sshint(ByteBuffer buf) {    return new BigInteger(+1, lenval(buf));}private static byte[] lenval(ByteBuffer buf) {    byte[] copy = new byte[buf.getInt()];    buf.get(copy);    return copy;}private static BigInteger derint(ByteBuffer input) {    int len = der(input, 0x02);    byte[] value = new byte[len];    input.get(value);    return new BigInteger(+1, value);}private static int der(ByteBuffer input, int exp) {    int tag = input.get() & 0xFF;    if (tag != exp) throw new IllegalArgumentException("Unexpected tag");    int n = input.get() & 0xFF;    if (n < 128) return n;    n &= 0x7F;    if ((n < 1) || (n > 2)) throw new IllegalArgumentException("Invalid length");    int len = 0;    while (n-- > 0) {        len <<= 8;        len |= input.get() & 0xFF;    }    return len;}private static byte[] readAllbase64Bytes(InputStream input) throws IOException {    ByteArrayOutputStream output = new ByteArrayOutputStream();    BufferedReader r = new BufferedReader(new InputStreamReader(input, StandardCharsets.US_ASCII));    Deprer deprer = base64.getDeprer();    while (true) {        String line = r.readLine();        if (line == null) break;        if (line.startsWith("-----")) continue;        output.write(deprer.depre(line));    }    return output.toByteArray();}


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

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

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