所以我有一个 android 到 PC 的通信( android = client, PC = server ),当客户端尝试连接到服务器时,这会在服务器上抛出:
javax.crypto.BadPaddingException: Blocktype mismatch: 0
at sun.security.rsa.RSAPadding.unpadV15(Unknown Source)
at sun.security.rsa.RSAPadding.unpad(Unknown Source)
at com.sun.crypto.provider.RSACipher.doFinal(RSACipher.java:356)
at com.sun.crypto.provider.RSACipher.engineDoFinal(RSACipher.java:382)
at javax.crypto.Cipher.doFinal(Cipher.java:2087)
但是PC客户端的相同代码成功连接到PC服务器(PC <-> PC Works,Android <-> PC没有)
可能导致这种情况的 android 加密有什么不同吗?我没有发布任何代码,因为代码很大,并且 PC 客户端上的 1:1 副本完美无缺。
服务器解密数据包的方法:
public static Packet decompile(PacketWrapper wrapper, PrivateKey privateKey)
throws Exception {
for (Provider provider : Security.getProviders()) {
System.out.println(provider.getName());
System.out.println(provider.getInfo());
System.out.println(System.lineSeparator());
}
Cipher cipher = Cipher.getInstance("RSA");
cipher.init(Cipher.DECRYPT_MODE, privateKey);
byte[] data = cipher.doFinal(wrapper.data);
return (Packet) bytesToObj(data);
}
发送数据包时的客户端方法:
try {
KeyGenerator kg = KeyGenerator.getInstance("AES");
kg.init(AESsize, new SecureRandom());
aesKey = (SecretKeySpec) kg.generateKey();
new SecureRandom().nextBytes(ivKey);
out.writeObject(Packet.compile(new ClientKeyPacket(aesKey, ivKey),
publicKey));
} catch (Exception e) {
e.printStackTrace();
print("Could not connect to the server");
closeStreams("");
return;
}
哪里Packet.compile()
是:
public static PacketWrapper compile(Packet packet, PublicKey publicKey)
throws Exception {
byte[] bytes = objToBytes(packet);
System.out.println("Size > " + bytes.length);
Cipher cipher = Cipher.getInstance("RSA");
cipher.init(Cipher.ENCRYPT_MODE, publicKey);
byte[] data = cipher.doFinal(bytes);
return new PacketWrapper(data);
}
Crypt
静态字符串是:
public class Crypt {
public static String saltMethod = "PBKDF2WithHmacSHA1";
public static String encryptMethod = "AES/CBC/NoPadding";
public static String shortEncrypt = "AES";
public static String encoding = "UTF-8";
public static int saltIterations = 5000;
public static int saltLength = 8;
}