我目前在我的服务器上解密rsa加密的数据时遇到了问题,该服务器使用node.js并使用node-rsa库进行加密/解密。
在我的Android客户端上,没有任何问题地接收到了公钥,但在尝试解密数据时,我得到了以下异常:
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)这是我在客户机上生成PublicKey的方式
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());
}以下是客户端的加密:
cipher = Cipher.getInstance("RSA");
cipher.init(Cipher.ENCRYPT_MODE, QtedEncryption.publicKey);
cipherData = cipher.doFinal(password.getBytes());
password = Base64.encodeToString(cipherData, Base64.DEFAULT);然后通过POST请求将密码发送到服务器。
var password = key.decrypt(requestData.password,'utf8');公钥和私钥是在使用以下代码启动服务器时生成的
var rsa = require('node-rsa');
//create RSA-key
var key = new rsa({b: 1024});
console.log(key.getPrivatePEM());
console.log(key.getPublicPEM());发布于 2015-01-03 08:25:29
请尝试使用Cipher.getInstance("RSA/ECB/OAEPWithSHA1AndMGF1Padding");。'node-rsa'似乎默认使用OAEP填充,目前您根本没有使用任何RSA填充方案。
https://stackoverflow.com/questions/27722177
复制相似问题