简体   繁体   中英

Android RSA and node.js RSA Encryption/Decryption

I'm currently having a problem with decrypting RSA-encrypted data on my server, which is working with node.js and using the node-rsa library for Encryption/Decryption.

The public key is received without any problems on my Android client, but when trying to decrypt the data I get the following Exception:

TypeError: Cannot call method 'toString' of null
    at NodeRSA.module.exports.NodeRSA.$getDecryptedData (C:\src\qteddev\node\nod
e_modules\node-rsa\src\NodeRSA.js:283:27)
    at NodeRSA.module.exports.NodeRSA.decrypt (C:\src\qteddev\node\node_modules\
node-rsa\src\NodeRSA.js:170:21)
    at IncomingMessage.<anonymous> (C:\src\qteddev\node\main.js:187:36)
    at IncomingMessage.emit (events.js:92:17)
    at _stream_readable.js:929:16
    at process._tickCallback (node.js:419:13)

This is how i generate the PublicKey on the client

public static void getPublicKeyFromPemFormat(String PEMString,
                                                       boolean isFilePath) throws IOException, NoSuchAlgorithmException,
            InvalidKeySpecException {

        BufferedReader pemReader = null;
        if (isFilePath) {
            pemReader = new BufferedReader(new InputStreamReader(
                    new FileInputStream(PEMString)));
        } else {
            pemReader = new BufferedReader(new InputStreamReader(
                    new ByteArrayInputStream(PEMString.getBytes("UTF-8"))));
        }
        StringBuffer content = new StringBuffer();
        String line = null;
        while ((line = pemReader.readLine()) != null) {
            if (line.indexOf("-----BEGIN PUBLIC KEY-----") != -1) {
                while ((line = pemReader.readLine()) != null) {
                    if (line.indexOf("-----END PUBLIC KEY") != -1) {
                        break;
                    }
                    content.append(line.trim());
                }
                break;
            }
        }
        if (line == null) {
            throw new IOException("PUBLIC KEY" + " not found");
        }
        Log.i("PUBLIC KEY: ", "PEM content = : " + content.toString());

        KeyFactory keyFactory = KeyFactory.getInstance("RSA");

        publicKey = keyFactory.generatePublic(new X509EncodedKeySpec(Base64.decode(content.toString(), Base64.DEFAULT)));
        //Log.i("GENERATED EXPONENT AND MODULUS = ", publicKey.toString());
    }

Here's the encryption on the client side:

cipher = Cipher.getInstance("RSA");
cipher.init(Cipher.ENCRYPT_MODE, QtedEncryption.publicKey);
cipherData = cipher.doFinal(password.getBytes());
password = Base64.encodeToString(cipherData, Base64.DEFAULT);

The password is then sent to the server via POST request.

var password = key.decrypt(requestData.password,'utf8');

Public and private key are generated when the server is started using this code

var rsa = require('node-rsa');

//create RSA-key
var key = new rsa({b: 1024}); 
console.log(key.getPrivatePEM());
console.log(key.getPublicPEM());

Try Cipher.getInstance("RSA/ECB/OAEPWithSHA1AndMGF1Padding"); instead. 'node-rsa' seems to default to OAEP padding, currently you are using no RSA padding scheme at all.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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