RSA Encryption not working appropiatly with key taken from XML file

Viewed 111

I've been trying to crypt a AES key using RSA with a given exponent and modulus. I'm getting the key XML file from a C# restful webservice. The XML key looks somewhat like this:

<RSAKeyValue><Modulus>vJaqEtwrfG...LFF7XACWCb6lQ==</Modulus><Exponent>AQAB</Exponent></RSAKeyValue>

I'm parsing the XML, and saving it in private variables using this function:

public void initPublicKeyFromServer(Context context) {
    String strModulusBytes = "";
    String strExpBytes = "";

    Log.i("rsaEncryptSecretKey", "pos1");
    try {
        InputStream inputStream = context.openFileInput("puk.txt");
        InputStreamReader inputStreamReader = new InputStreamReader(inputStream, StandardCharsets.UTF_16);
        BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
        String receiveString = bufferedReader.readLine();

        XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
        factory.setNamespaceAware(true);
        XmlPullParser xpp = factory.newPullParser();
        xpp.setInput(new StringReader(receiveString));
        int eventType = xpp.getEventType();
        xpp.next();
        String xmlTag = xpp.getName();



        while (eventType != XmlPullParser.END_DOCUMENT) {
            eventType = xpp.next();
            xmlTag = xpp.getName();
            if(eventType == XmlPullParser.START_TAG) {
                if (xmlTag.equals("RSAKeyValue")) {
                    continue;
                }
            }
            if(eventType == XmlPullParser.START_TAG) {
                if (xmlTag.equals("Modulus")) {
                    eventType = xpp.next();
                    xmlTag = xpp.getName();
                    strModulusBytes = xpp.getText();
                    eventType = xpp.next();
                    xmlTag = xpp.getName();
                }
            }
            if(eventType == XmlPullParser.START_TAG) {
                if (xmlTag.equals("Exponent")) {
                    eventType = xpp.next();
                    xmlTag = xpp.getName();
                    strExpBytes = xpp.getText();
                    eventType = xpp.next();
                    xmlTag = xpp.getName();
                }
            }

        }
    } catch (Exception e) {
        e.printStackTrace();
    }

    byte[] modBytes = StandardCharsets.UTF_16.encode(strModulusBytes).array();
    byte[] expBytes = StandardCharsets.UTF_16.encode(strExpBytes).array();

    BigInteger modules = new BigInteger(1, modBytes);
    BigInteger exponent = new BigInteger(1, expBytes);
    try {
        KeyFactory factory = KeyFactory.getInstance("RSA");
        RSAPublicKeySpec pubSpec = new RSAPublicKeySpec(modules, exponent);
        publicKey = factory.generatePublic(pubSpec);
        rsaCipher = Cipher.getInstance(KeyPairInstanceType);
        rsaCipher.init(Cipher.ENCRYPT_MODE, publicKey);
    } catch (Exception e) {
        e.printStackTrace();
    }
}

public byte[] rsaEncrypt(final byte[] plain) throws Exception {
    rsaCipher.init(Cipher.ENCRYPT_MODE, publicKey);
    byte[] encryptedBytes = rsaCipher.doFinal(plain);
    return encryptedBytes;
}

public byte[] rsaEncrypt(final String plain) throws Exception {
    rsaCipher.init(Cipher.ENCRYPT_MODE, publicKey);
    byte[] encryptedBytes = rsaCipher.doFinal(plain.getBytes());
    return encryptedBytes;
}

I'm getting the following exception and I've been stuck with it for the past week. Any clue how I could solve it?

W/System.err: java.lang.RuntimeException: error:04000065:RSA routines:OPENSSL_internal:BAD_E_VALUE

Could it have anything to do with the UTF-16 encoding? StandardCharsets.UTF_16.encode(strModulusBytes).array(); Thanks alot in advance.

0 Answers
Related