我有一个RSA加密系统,用于保护服务器(Python)和客户端(Java)之间的通信。目前,我使用服务器的密钥对从客户端到服务器的信息进行了正确的加密和解密。我不能做的是在客户端(Java)上解密来自服务器(Python)的消息
Python
中的加密片段如下所示:
publicKeyStr = None
with open(PATH_KEY, 'r') as file:
publicKeyStr = file.read()
publicKeyClient = RSA.importKey(publicKeyStr)
encryptor = PKCS1_v1_5.new(key)
message = encryptor.encrypt(plainMessage)
# Message's length is always 128 bytes on the server side
#Send message to the client (RabbitMQ)
带有加密内容的变量message
在客户端以字符串格式接收,问题来了:Java中的解密方法要求byte[]
输入和最大块大小为128字节,但当将字符串message
转换为byte[]
数组时,这些字符串超过128字节。这是我在客户端(Java)的方法:
public String call(String queue, byte[] message) throws IOException,
InterruptedException {
final String corrId = UUID.randomUUID().toString();
String replyQueueName = channel.queueDeclare().getQueue();
AMQP.BasicProperties props = new AMQP.BasicProperties.Builder()
.correlationId(corrId).replyTo(replyQueueName).build();
channel.basicPublish("", queue, props, message);
final BlockingQueue<String> response = new ArrayBlockingQueue<String>(1);
String ctag = channel.basicConsume(replyQueueName, true,
new DefaultConsumer(channel) {
@Override
public void handleDelivery(String consumerTag,
Envelope envelope, AMQP.BasicProperties properties,
byte[] body) throws IOException {
if (properties.getCorrelationId().equals(corrId)) {
response.offer(new String(body, "UTF-8"));
}
}
});
String result = response.take();
channel.basicCancel(ctag);
return result;
}
public static String sendElement(String topic, byte[] message) {
SendRabbitMQ sendRabbitMQ = null;
String response = "";
try {
sendRabbitMQ = new SendRabbitMQ();
response = sendRabbitMQ.call(topic, message);
} catch (IOException | TimeoutException | InterruptedException e) {
e.printStackTrace();
} finally {
if (sendRabbitMQ != null) {
try {
sendRabbitMQ.close();
} catch (IOException _ignore) {
}
}
}
return response;
}
String response = SendRabbitMQ.sendElement("topic", encryptedInfo);
byte [] responseByte = response.getBytes();
Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");
cipher.init(Cipher.DECRYPT_MODE, privateKey);
System.out.println(new String(cipher.doFinal(message)));
分配responseByte
时会出现错误,因为它的大小超过128个字符
因此,我的问题是,如何将从服务器端发送的消息字符串转换为客户端大小,以便将该字符串正确转换为byte[]数组
编辑:我相信这与客户端的RabbitMQ代码无关,但为了以防万一,我已经介绍了使用的方法
目前没有回答
相关问题 更多 >
编程相关推荐