简体   繁体   English

轻量级的API可读取Java中的PKCS#1 RSA公钥?

[英]Light weight api to read PKCS#1 RSA public key in java?

I use bouncycastle to read the PKCS#1 format RSA public key, this key is begin with: 我使用Bouncycastle读取PKCS#1格式的RSA公钥,该密钥始于:

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

The code works well, but it will depends on a heavy bouncycastle jar. 该代码运行良好,但是将取决于沉重的bouncycastle jar。 It will cause the code can't be compiled on Android because of the java function's number is more than 65535. 由于Java函数的编号超过65535,这将导致代码无法在Android上编译。

I have changed the bouncycastle to spongycastle and decouple the prov.jar & pkix.jar. 我已将bouncycastle更改为spongycastle,并取消了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);

But the classes.dex will still be 2MB larger. 但是classes.dex仍然会大2MB。

So I'm looking up is there light weight api to do it? 所以我正在查找是否可以使用轻量级的API? Or the algorithm to read PKCS#1 RSA public key will be easy to write? 还是读取PKCS#1 RSA公钥的算法很容易编写?

PS Using Proguard won't resolve the problem at all , it will disable the debugger in IDE. PS使用Proguard根本无法解决问题,它将在IDE中禁用调试器。

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: 使用经过验证的库绝对可以使您更好,但是这个特定的ASN.1对象相对简单:

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]);
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM