java BadPaddingException和某些文件停留在99%
我试图从这里收集所有可能的加密/解密信息。修修补补,有成功也有失败
但是现在我已经应用了这个代码,它的成功和失败也是如此。某些文件(exe或msi)正在工作,但它们仍然提供有关BadPaddingException的错误。此外,一些其他媒体文件(如mp4、mkv等)的存储容量为99%,尽管它们已完全接收(只有一些小的字节差异,但磁盘上的大小始终匹配),但不会超过99%
我只是想得到一些帮助来解决这两个问题。通过socket编程将文件从一台PC传输到另一台PC
服务器:(已编辑)
DataInputStream dis = new DataInputStream(msock.getInputStream());
DataOutputStream dos = new DataOutputStream(msock.getOutputStream());
String file2dl = dis.readLine(); //2
File file = new File(sharedDirectory.toString() + "\\" + file2dl);
dos.writeLong(file.length()); //3+
//Get file name without extension.
String fileName = Files.getNameWithoutExtension(file2dl);
//AES-128 bit key initialization.
byte[] keyvalue = "AES128BitPasswd".getBytes();
SecretKey key = new SecretKeySpec(keyvalue, "AES");
//Initialize the Cipher.
Cipher encCipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
encCipher.init(Cipher.ENCRYPT_MODE, key);
//Get the IV from cipher.
IvParameterSpec spec = null;
try {
spec = encCipher.getParameters().getParameterSpec(IvParameterSpec.class);
} catch (InvalidParameterSpecException ex) {
Logger.getLogger(PeersController.class.getName()).log(Level.SEVERE, null, ex);
}
byte[] iv = spec.getIV();
dos.write(iv, 0, iv.length);
File tempDir = new File(tempDirectory.toString());
//Encryption Mechanism.
try (FileInputStream fis = new FileInputStream(file)) {
try (CipherOutputStream cos = new CipherOutputStream(dos, encCipher);
FileInputStream stream = new FileInputStream(tempDir + "\\" + fileName + ".encr")) {
int read, r;
byte[] buffer = new byte[1024 * 1024];
while ((read = fis.read(buffer)) != -1) {
cos.write(buffer, 0, read);
}
}
}
}
客户端:
long len;
int count = 0;
int dflag = 0;
String size;
dos.writeBytes("Download\r\n"); //1+
dos.writeBytes(filename + "\r\n"); //2+
System.out.println("File to fetch: -> " + filename);
len = dis.readLong(); //3
System.out.println("Size of file: -> " + len);
//Get file name without Extension.
String fileName = Files.getNameWithoutExtension(filename);
//Get Initialization Vector from Encryption Cypher.
byte[] iv = new byte[16];
int j = dis.read(iv, 0, iv.length);
final File encrypted = new File(sharedDirectory.toString() + "\\" + fileName + ".encr");
final File decrypted = new File(sharedDirectory.toString() + "\\" + filename);
try (FileOutputStream fos = new FileOutputStream(encrypted)) {
byte[] b = new byte[1024 * 1024];
while (fetching) {
int r = dis.read(b, 0, b.length); //4
count = count + r;
double p = (double) count / len;
double per = new BigDecimal(p).setScale(4, BigDecimal.ROUND_HALF_UP).doubleValue();
fos.write(b, 0, r);
System.out.println("Size Appending: -> " + count);
System.out.println("Percentage: ->" + per);
Platform.runLater(() -> {
pBar.setProgress(per);
});
if (count >= len) {
dflag = 1;
break;
}
}
}
如果完全接收到加密数据
if(dflag == 1) {
//AES-128 bit key initialization.
System.out.println("File completely received");
byte[] keyvalue = "AES128PeerBuLLet".getBytes();
Key key = new SecretKeySpec(keyvalue, "AES");
//Initialization Vector initialized
IvParameterSpec ivParameterSpec = null;
//Cipher Initialization.
Cipher decCipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
try {
decCipher.init(Cipher.DECRYPT_MODE, key, new IvParameterSpec(iv));
} catch (InvalidAlgorithmParameterException ex) {
Logger.getLogger(PeersController.class.getName()).log(Level.SEVERE, null, ex);
}
System.out.println(decCipher.getProvider().getInfo());
//Decryption Mechanism.
try (FileOutputStream stream = new FileOutputStream(decrypted)) {
try (FileInputStream fis = new FileInputStream(encrypted)) {
try (CipherInputStream cis = new CipherInputStream(fis, decCipher)) {
int read, i = 0;
byte[] buffer = new byte[(1024 * 1024) + 16];
while ((read = cis.read(buffer)) != -1) {
stream.write(buffer, 0, read);
i = i + read;
double d = (double) i / len;
double progress = new BigDecimal(d).setScale(3, BigDecimal.ROUND_HALF_UP).doubleValue();
Platform.runLater(() -> {
pBar.setProgress(progress);
progressText.setText("Decrypting..");
});
}
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
非常感谢您的任何意见。多谢各位
编辑1:添加了通过流接收的加密和解密文件大小的链接Dropbox Link
编辑2:因此,在三位成员的帮助下,问题最终得以解决,他们参与了对我最大程度的帮助。我正在回顾我的问题的其他解决方案,我遇到了this解决方案,这有助于我深入思考背景中发生的实际情况。感谢Artjom B.的推荐解决方案和@zaph&@jtahlborn用于清除我关于填充和输入/输出流的错误假设
# 1 楼答案
当使用填充、PKCS#5或PKCS#7时,加密输出将更大,最多可包含一个块大小。见PKCS#7。解密后将删除填充
加密数据将更长,因此必须对其进行说明。如何处理取决于如何处理输出。如果要进入预先分配的区域,如内存缓冲区,则必须为缓冲区分配一个更大的块大小(AES为16字节)。如果流式传输通常只是确保发送所有加密的字节,则n=它只是输入的长度。所有这些都取决于实现和系统/语言
填充字节由加密方法动态创建,因此不需要更改输入。这假设加密方法正在添加填充,而解密方法正在删除填充
示例1:如果您有1024字节的数据,则加密输出将为1040字节。解密时,输入数据将为1040字节,输出解密数据将为1024字节
示例2:如果您有1020字节的数据,则加密输出将为1024字节。解密时,输入数据将为1024字节,输出解密数据将为1020字节
# 2 楼答案
无法使用FileInputStream读取当前正在写入的文件。它不是为读取正在进行的文件而构建的
看起来您正试图将加密流写入
dos
(服务器代码中没有包含它的定义)。如果是这样,您应该将其用作CipherOutputStream的底层流同样,在客户机中,您也尝试同样的事情。如果要先将文件写入磁盘,然后将整个文件写入磁盘,然后解密。如果您想要流解密,那么将CipherInputStream包裹在套接字InputStream周围(大概是
dis
?)此外,您没有显示从客户机上获取
len
的位置,但我假设它是原始数据的长度?如果是这样,那么您的进度计算是不正确的,因为加密数据的长度通常与原始数据的长度不同