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

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

将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();}


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5103512.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-11-17
下一篇 2022-11-17

发表评论

登录后才能评论

评论列表(0条)

保存