0
현재 node.js와 함께 작동하고 암호화/해독을 위해 node-rsa 라이브러리를 사용하는 서버에서 RSA 암호화 데이터의 해독에 문제가 있습니다.안드로이드 RSA 및 node.js RSA 암호화/해독
공개 키는 내 안드로이드 클라이언트에서 아무 문제없이 수신하지만, 나는 다음과 같은 예외가 데이터를 해독 할 때 :
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를 생성하는 방법이다
cipher = Cipher.getInstance("RSA");
cipher.init(Cipher.ENCRYPT_MODE, QtedEncryption.publicKey);
cipherData = cipher.doFinal(password.getBytes());
password = Base64.encodeToString(cipherData, Base64.DEFAULT);
: 여기
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());
}
는 클라이언트 측에서 암호화입니다 그런 다음 POST 요청을 통해 암호가 서버로 전송됩니다. 서버가 대신
Cipher.getInstance("RSA/ECB/OAEPWithSHA1AndMGF1Padding");
을보십시오이 코드
var rsa = require('node-rsa');
//create RSA-key
var key = new rsa({b: 1024});
console.log(key.getPrivatePEM());
console.log(key.getPublicPEM());
이 정보가 있습니까? –