Tech Blog

Coding, Programming, Software, Technology

Serializing RSAPublicKey in Java / Android

By Pranay Choudhary2 min read
← Back to blog

While serializing the Modulus component of RSAPublicKey in Java or Android, you might run into issues. Normally, you would expect getModulus from 2048 bit RSAKey converted to byte array to give you a 256 byte key.

        final KeyPairGenerator generator = KeyPairGenerator.getInstance("RSA");
        generator.initialize(2048);
        final KeyPair rsaKey = generator.generateKeyPair();
        final RSAPublicKey publicKey = (RSAPublicKey) rsaKey.getPublic();
        final byte[] modulus = publicKey.getModulus().toByteArray();
        System.out.println("Length of modulus = " + modulus.length); // Prints 257

The strange thing is that the length of modulus turns out to be 257 (instead of 256). We all know that all cryptographic keys are multiples of 8. Then why does Java generate a cryptographic key of 257 bytes? To troubleshoot this, you need to understand the logic of modulus and its selization.

The Modulus of an RSAKey is represented by a BigIntiger. 256-bit modulus is unsigned whereas BigInteger.toByteArray() returns the signed representation of the value. The most significant bit of BigInteger.toByteArray() is the sign bit. Since the BigInteger here represents a 256-bit modulus, which will always be positive, the leftmost bit always turns out to be zero.

To solve the problem, you can write a trim method that removes the most significant bit with value zero from the byte array.

    public static byte[] trim(byte[] array)
    {
        if (array[0] == 0) {
            array = Arrays.copyOfRange(array, 1, array.length);
        }
        return array;
    }

Overall, the final solution looks like this:

        final KeyPairGenerator generator = KeyPairGenerator.getInstance("RSA");
        generator.initialize(2048);
        final KeyPair rsaKey = generator.generateKeyPair();
        final RSAPublicKey publicKey = (RSAPublicKey) rsaKey.getPublic();
        final byte[] modulus = trim(publicKey.getModulus().toByteArray());
        System.out.println("Length of modulus = " + modulus.length); // Prints 256

Keep reading

You may be interested in

Browse all articles →
How Angular is Different from AngularJS
September 27, 20227 min read

How Angular is Different from AngularJS

What is Angular? Angular is a popular open-source Typescript framework and free to use. Considered to be a JavaScript-based framework,TypeScript.is its primary programming language. TypeScript is Developed by Microsoft and its superset of JavaScript, which simply means that TypeScript has all the capabilities that JS has and more additional features. Angular is majorly used for […]

By Sachin DevadigaRead More →
Angular constructor vs ngOnInit
September 15, 20223 min read

Angular constructor vs ngOnInit

Overview: If you are new to angular or have been working with it for a while, you will have questions like: Is it necessary to inject dependencies in the constructor and bind data? When should we use ngOnInit ? What is the difference between constructor and OnInit?. So, in this blog we will answer all […]

By Priyanka SRead More →
Angular – Difference between ng-container and ng-template
July 13, 20222 min read

Angular – Difference between ng-container and ng-template

Overview: ng-container and ng-template, both are angular elements which will work like containers with HTML elements, but with different purpose/use cases. Lets see what is the use of ng-container and ng-template below: ng-container: In some cases when you do not want to add an extra HTML element to the DOM , for example like div […]

By Priyanka SRead More →