重量轻API来读取在Java PKCS#1 RSA公钥? [英] Light weight api to read PKCS#1 RSA public key in java?

查看:313
本文介绍了重量轻API来读取在Java PKCS#1 RSA公钥?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用BouncyCastle的读取PKCS#1格式的RSA公共密钥,该密钥是开头:

I use bouncycastle to read the PKCS#1 format RSA public key, this key is begin with:

-----BEGIN RSA PUBLIC KEY---- 

在code效果很好,但它会依赖于一个沉重的BouncyCastle的罐子。这将导致code不能上编译的Andr​​oid,因为Java函数的数量超过65535。

我已经改变了BouncyCastle的为spongycastle和解耦prov.jar&放大器; pkix.jar。也只使用一个类来减少code参考:

I have changed the bouncycastle to spongycastle and decouple the prov.jar & pkix.jar. Also only use one class to reduce the code reference:

org.spongycastle.asn1.pkcs.RSAPublicKey rsaPublicKey = org.spongycastle.asn1.pkcs.RSAPublicKey.getInstance(keyBytes);

但classes.dex仍然会超过2 MB更大。

But the classes.dex will still be 2MB larger.

于是我找了有重量轻的API来做到这一点?或算法来读取PKCS#1 RSA公钥将很容易写?

P.S。使用ProGuard解决不了问题的,它会在IDE中禁用调试器。

P.S. Using Proguard won't resolve the problem at all , it will disable the debugger in IDE.

推荐答案

下面是一些code我掀起了演示了如何容易,这是。你肯定是最好使用久经考验的库,但这个特殊的ASN.1对象是相对简单的:

Here is some code I whipped out that shows how easy this is. You are definitely better off using a tried and tested library but this particular ASN.1 object is relatively simple:

import java.io.IOException;
import java.math.BigInteger;
import java.nio.ByteBuffer;
import java.nio.file.Files;
import java.nio.file.Paths;


public class ParseRSAPublicKey {
    private static final int SEQUENCE = 0x30;
    private static final int INTEGER = 0x02;
    private final ByteBuffer derBuf;

    public ParseRSAPublicKey(byte[] der) {
        derBuf = ByteBuffer.wrap(der);
    }

    public byte get() {
        return derBuf.get();
    }

    /**
     * @return the next byte of the buffer as an int
     */
    public int getAsInt() {
        return get() & 0xff;
    }

    public byte[] getArray(int len) {
        byte [] arr = new byte[len];
        derBuf.get(arr);
        return arr;
    }

    public int parseId() {
        // Only the low-tag form is legal.
        int idOctect = getAsInt();
        if (idOctect >= 0x31) {
            throw new RuntimeException("Invalid identifier octets");
        }
        return idOctect;        
    }

    public long parseLength() {
        int octet1 = getAsInt();
        if (octet1 < 128) {
            // short form of length
            return octet1;
        } else {
            // long form of length
            int lengthOfLength = octet1 & 0x7f;
            BigInteger bigLen = new BigInteger(1, getArray(lengthOfLength));

            if (bigLen.compareTo(BigInteger.valueOf(Long.MAX_VALUE)) > 0){
                throw new RuntimeException("Length is too long");
            }
            return bigLen.longValue();
        }
    }

    public BigInteger parseInteger() {
        if (parseId() != INTEGER) {
            throw new RuntimeException("expected SEQUENCE tag");
        }

        long length = parseLength();
        if (length > Integer.MAX_VALUE){
            throw new RuntimeException("Length is too long");
        }
        return new BigInteger(1, getArray((int) length));
    }
    public BigInteger[] parse() {
        // Parse SEQUENCE header
        if (parseId() != SEQUENCE) {
            throw new RuntimeException("expected SEQUENCE tag");
        }

        @SuppressWarnings("unused")
        long seqLength = parseLength(); // We ignore this

        // Parse INTEGER modulus
        BigInteger n = parseInteger();
        BigInteger e = parseInteger();
        return new BigInteger[] {n, e};

    }

    public static void main(String [] args) throws Exception {
        byte [] der = Files.readAllBytes(Paths.get("rsapub.p1"));
        ParseRSAPublicKey parser = new ParseRSAPublicKey(der);
        BigInteger [] results = parser.parse();
        System.out.printf("%d%n", results[0]);
        System.out.printf("%d%n", results[1]);
    }
}

这篇关于重量轻API来读取在Java PKCS#1 RSA公钥?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆