我正在尝试创建一个zip文件,其中包含加密文件和该文件的加密密钥。这是我的代码:
zip
when (params) {
is RecyclerView.LayoutParams -> { /* smart cast here */ }
else -> { /* optional other cases */ }
}
// casts to RecyclerView.LayoutParams? and then invokes the body of let if not null (when params is a RecyclerView.LayoutParams)
(params as? RecyclerView.LayoutParams)?.let {
/* smart cast here */
}
连接的流
ZipOutputStream zout = new ZipOutputStream(new FileOutputStream("zip.zip"));
获取密钥
PipedInputStream pis = new PipedInputStream();
PipedOutputStream pos = new PipedOutputStream(pis);
获取密码
KeyGenerator keygen=KeyGenerator.getInstance(storageSettings.getFileEncryptAlgorithm());
SecretKey key = keygen.generateKey();
现在我将密码输出输出到pipedOutputStream
Сipher cipher = Cipher.getInstance("DESede");
cipher.init(Cipher.ENCRYPT_MODE, key);
添加到zip及其那里,但下一个文件为空
CipherOutputStream cos = new CipherOutputStream(pos,cipher);
byte[] m = new byte[1024];
int position;
while ((position = someInputData.read(m, 0, 1024)) >= 0)
{
cos.write(m, 0, position);
}
现在我想通过rsa加密SecretKey密钥,并且我有文件中的密钥对
addFileToZip(zout, pis, backup.getFilename());
cos.flush();
现在我想用加密密钥创建第二个文件,但是我的zip中有空文件,除了第一个文件外,所有文件都为空
String publicKeyContent = new String(Files.readAllBytes(Paths.get(ClassLoader.getSystemResource(storageSettings.getPublicKeyFile()).toURI())));
publicKeyContent = publicKeyContent.replaceAll("\\n", "").replace("-----BEGIN PUBLIC KEY-----", "").replace("-----END PUBLIC KEY-----", "");
KeyFactory keyfactory = KeyFactory.getInstance(storageSettings.getKeyEnryptAlgorithm());
X509EncodedKeySpec keySpecX509 = new X509EncodedKeySpec(Base64.getDecoder().decode(publicKeyContent));
RSAPublicKey publicKey = (RSAPublicKey) keyfactory.generatePublic(keySpecX509);
cipher = Cipher.getInstance(storageSettings.getKeyEnryptAlgorithm());
cipher.init(Cipher.ENCRYPT_MODE, publicKey);
这是addFileToZip()
CipherOutputStream cos1 = new CipherOutputStream(pos,cipher);
byte[] tmp = key.getEncoded();
cos1.write(tmp);
addFileToZip(zout, pis,"key.txt");