【发布时间】:2016-05-14 03:41:43
【问题描述】:
我正在做一个小型的学校“项目”,它是一个小型聊天程序。您启动服务器,服务器将公钥发送给客户端,以便客户端可以发回加密数据。
问题是当我尝试在服务器端解密它时,我收到一个错误。
javax.crypto.BadPaddingException: Decryption error
at sun.security.rsa.RSAPadding.unpadV15(RSAPadding.java:380)
at sun.security.rsa.RSAPadding.unpad(RSAPadding.java:291)
我的服务器等待socket.accept(),当客户端连接时,它会向此套接字发送一个公钥。
(这不是全部代码)
private Client client;
private JSONObject json;
//Connector is used as a listener for messages.
public Connector(Socket socket, ServerBroadCast SBC) throws IOException {
DIS = new DataInputStream(socket.getInputStream());
this.SBC = SBC;
this.client = SBC.AddClient(socket);
//Sending public RSA key to client to use to encrypt their message
this.client.sendPublicKey(RSA.getPublicKey()); //This is where I am sending RSA public key to client
}
我只是将一条消息发送回服务器,以查看它是否已加密以及服务器是否可以解密它。
使用此类发送给 CLIENT
DataOutputStream DOS;
public Client(Socket s) throws IOException {
DOS = new DataOutputStream(s.getOutputStream());
}
public void sendPublicKey(PublicKey publicKey) throws IOException {
JSONObject json = new JSONObject();
json.put(JSONKeys.TYPE, JSONTypes.PUBLIC_KEY);
json.put(JSONKeys.MESSAGE, RSA.getEncodedPublicKey());
this.send(json);
}
public void send(JSONObject json) throws IOException{
this.DOS.writeUTF(json.toString());
System.out.println(json);
}
这是我用来加密和解密的 RSA 类
private static PrivateKey privateKey = null;
private static PublicKey publicKey = null;
public static void generateKeyPair() {
KeyPairGenerator keyPairGenerator = null;
KeyPair keyPair = null;
try {
keyPairGenerator = KeyPairGenerator.getInstance("RSA");
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
keyPairGenerator.initialize(1024);
keyPair = keyPairGenerator.generateKeyPair();
RSA.privateKey = keyPair.getPrivate();
RSA.publicKey = keyPair.getPublic();
}
public static String getEncodedPublicKey() {
return (new String(Base64.encode(RSA.getPublicKey().getEncoded())));
}
public static String encrypt(String string) {
byte[] encrypted = null;
try {
Cipher cipher = Cipher.getInstance("RSA");//174 bytes
cipher.init(Cipher.ENCRYPT_MODE, RSA.getPublicKey());
encrypted = cipher.doFinal(string.getBytes());
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (NoSuchPaddingException e) {
e.printStackTrace();
} catch (InvalidKeyException e) {
e.printStackTrace();
} catch (IllegalBlockSizeException e) {
e.printStackTrace();
} catch (BadPaddingException e) {
e.printStackTrace();
}
return (new String(Base64.encode(encrypted)));
}
public static String decrypt(String string) {
byte[] decrypted = null;
try {
Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");
cipher.init(Cipher.DECRYPT_MODE, RSA.getPrivateKey());
decrypted = cipher.doFinal(Base64.decode(string));
} catch (InvalidKeyException e) {
e.printStackTrace();
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (NoSuchPaddingException e) {
e.printStackTrace();
} catch (IllegalBlockSizeException e) {
e.printStackTrace();
} catch (BadPaddingException e) {
e.printStackTrace();
} catch (Base64DecodingException e) {
e.printStackTrace();
}
return (new String(decrypted));
}
我知道我的填充有一些“错误”,因为错误说明了这一点,但我不明白是什么。
我从客户端加密消息返回的长度为 174 字节长。我知道它很长,但是为什么它会从我的纯文本中创建如此巨大的加密消息。
private final String MESSAGE = "Hello from client side";
【问题讨论】:
-
我唯一能想到的是 JSON 可能忽略了一些字节(不适合字符串)..尝试使用 Base64 对字节进行编码和解码。 (Java 通过 Base64.getEncoder() 和 Base64.get decoder() 支持 Base64)。 (我的意思是发送密钥时)
-
@Meguy26 抱歉忘记添加我拥有的另一个功能。我已经在使用 Base64 进行编码和解码。仍然收到此错误
-
发送密钥的时候呢?
-
@Meguy26
public static String getEncodedPublicKey() { return (new String(Base64.encode(RSA.getPublicKey().getEncoded()))); }这就是我正在使用的 -
啊,好吧。我不确定,加密是否必须是 RSA,因为 java 的 SSLSocket 自己进行加密。不过,您可能不得不使用匿名 TSL 协议
标签: java sockets encryption cryptography rsa