- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我在将 FileInputStream 和 FileOutputStream 转换为 3DES 加密算法的 CipherInputStream 和 CipherOutputStream 时遇到一些问题。有人可以指导我走上正确的道路吗?我最终收到错误:“CipherInputStream 无法解析为类型”。抱歉,如果这是显而易见的。
// author Alexander Matheakis
public static void main(String[] args) throws Exception {
// file to be encrypted
FileInputStream inputFile = new FileInputStream("plainfile.txt");
// encrypted file
FileOutputStream outputFile = new FileOutputStream("C:\\Users\\islan\\OneDrive\\Documents\\Encryptor\\plainfile.des");
// password to encrypt the file
String passKey = "tkfhkggovubm";
byte[] salt = new byte[8];
SecureRandom r = new SecureRandom();
r.nextBytes(salt);
PBEKeySpec pbeKeySpec = new PBEKeySpec(passKey.toCharArray());
SecretKeyFactory secretKeyFactory = SecretKeyFactory
.getInstance("PBEWithSHA1AndDESede");
SecretKey secretKey = secretKeyFactory.generateSecret(pbeKeySpec);
PBEParameterSpec pbeParameterSpec = new PBEParameterSpec(salt, 99999);
Cipher cipher = Cipher.getInstance("PBEWithSHA1AndDESede");
cipher.init(Cipher.ENCRYPT_MODE, secretKey, pbeParameterSpec);
outputFile.write(salt);
byte[] input = new byte[64];
int bytesRead;
while ((bytesRead = inputFile.read(input)) != -1) {
byte[] output = cipher.update(input, 0, bytesRead);
if (output != null)
outputFile.write(output);
}
byte[] output = cipher.doFinal();
if (output != null)
outputFile.write(output);
inputFile.close();
outputFile.flush();
outputFile.close();
// author Alexander Matheakis
最佳答案
这对我有用:
public class Cypher {
// password to encrypt the file
private static final String passKey = "tkfhkggovubm";
public static void main(String[] args) throws Exception {
try (FileInputStream inputFile = new FileInputStream("plainfile.txt");
FileOutputStream outputFile = new FileOutputStream("plainfile.des");) {
byte[] salt = new byte[8];
SecureRandom r = new SecureRandom();
r.nextBytes(salt);
outputFile.write(salt);
PBEKeySpec pbeKeySpec = new PBEKeySpec(passKey.toCharArray());
SecretKeyFactory secretKeyFactory = SecretKeyFactory
.getInstance("PBEWithSHA1AndDESede");
SecretKey secretKey = secretKeyFactory.generateSecret(pbeKeySpec);
PBEParameterSpec pbeParameterSpec = new PBEParameterSpec(salt, 99999);
Cipher cipher = Cipher.getInstance("PBEWithSHA1AndDESede");
cipher.init(Cipher.ENCRYPT_MODE, secretKey, pbeParameterSpec);
try (CipherOutputStream cis = new CipherOutputStream(outputFile, cipher);) {
IOUtils.copy(inputFile, cis);
}
}
try (FileInputStream fis = new FileInputStream("plainfile.des");
FileOutputStream outputFile = new FileOutputStream("plainfile.txt2");) {
byte[] salt = new byte[8];
int saltBytes = fis.read(salt);
if (saltBytes!=salt.length)
throw new Exception("Huh???");
PBEKeySpec pbeKeySpec = new PBEKeySpec(passKey.toCharArray());
SecretKeyFactory secretKeyFactory = SecretKeyFactory
.getInstance("PBEWithSHA1AndDESede");
SecretKey secretKey = secretKeyFactory.generateSecret(pbeKeySpec);
PBEParameterSpec pbeParameterSpec = new PBEParameterSpec(salt, 99999);
Cipher cipher = Cipher.getInstance("PBEWithSHA1AndDESede");
cipher.init(Cipher.DECRYPT_MODE, secretKey, pbeParameterSpec);
try (CipherInputStream cis = new CipherInputStream(fis, cipher);) {
IOUtils.copy(cis, outputFile);
}
}
}
}
执行main后,plainfile.txt
和plainfile.txt2
是相等的。 plainfile.des
已加密。
IOUtils.copy
是 apache.commons-io 中的一个方法,它在内部保留一个缓冲区并将所有字节从一个流写入另一个流。它只是将 byte[] input = ...
提取到方法中,并使用 try-with-resources 。这使代码变得简单并具有更好的可读性。
关于java - 需要帮助实现 CipherInputStream 和 CipherOutputStream,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55867506/
我正在尝试加密/解密一些文件,我将使用通过 CipherIn/OutputStream 进行管道传输的 FileIn/OutputStream 来读取/写入这些文件。概念相当简单,我已经使用原始字节数
我正在编写一个使用 RSA 密码和 AES 密码的公钥和私钥加密算法的实现。在此方法中,AES key 应该使用 RSA CipherInputStream 解密。 public void loadK
我正在测试 Java 中的文本加密。问题是我在行的开头得到一些奇怪的字符,但我不明白为什么。当我删除加密时,一切都会顺利进行。 复制到 Notepad++ 中时,输出如下所示: Hello dear
我有一个类似的方法。它工作正常,但问题是当我尝试解密填充错误甚至根本未加密的文件时。通常我相信 cipher.doFinal(..) 通常会抛出一些与 IllegalBlockSizeExceptio
我有以下函数来加密文件。我打印了两个文件的结果,一切似乎都正常工作。 加密文件已更改,并且与输入文件的长度相同。 public void encrypt(String password, String
我正在尝试读取一个文件,其中部分数据已加密,部分数据未加密。 每条消息的开头都有一个未加密的 header ,其中包含后续加密消息的字节大小。 我要读的类(class)刚刚延长FileInputStr
我正在使用以下代码来解密从 Android 设备加密的文件。 private void mDecrypt_File(FileInputStream fin, String outFile) throw
有没有人知道如何在不将解密文件重写为原始文件的情况下读取解密文件? 下载文件后,文件自动加密。当我想打开文件时,文件会先被解密,但问题是如何从 CipherInputStream 读取文件,这样就不需
我遇到了麻烦,因为我需要关闭 de CIS(否则我不会得到最后 16 个字节),但我不能,因为我通过套接字使用它: cis = new CipherInputStream(new ObjectInpu
我遇到了需要缓冲 CipherInputStream 的场景。确切地说,在将结果返回给 InputStream.read(byte[], int, int) 的调用者之前,我需要确保缓冲区已填充到 3
这个问题已经有答案了: Initial bytes incorrect after Java AES/CBC decryption (10 个回答) 已关闭 4 年前。 我在将 FileInputSt
我有以下代码。但是,文件 b.xlsx 和 c.xlsx 的大小为 0 字节。为什么CipherOuputSteam不工作? public static void main(String[] args
我加密,然后解密一个图像,然后将其传递给我的图像 util 以调整大小,(从某处慷慨地借用代码)像这样: public static Bitmap loadResizedBitmap(InputStr
我有点被这个异常(exception)困住了: java.lang.RuntimeException: error:0407806d:RSA routines:decrypt:DATA_LEN_NOT
我遇到了一个问题,如果使用 CipherInputStream,则针对 FileInputStream 支持的 InputStream 的代码不起作用。 示例如下: // skipCount is
我想将一个加密流添加到我的多方实体中,以将其上传到我的 servlet,但我不知道该怎么做... 嗯,那么 MultipartEntity 对于 addPart(...,...) 来说是相当有限的方法
我的文件包含开头包含未加密的 header 数据,然后其余部分包含加密的 header 数据。我希望能够使用 BufferedInputStream/FileInputSteam 读取 header
我一直在尝试用 AES 编写一个加密文件,然后使用 JCA 中提供的密码流对其进行解密。但是,我在读取文件时遇到了问题,因为解密正在失控。 public class CipherStreams { p
我一直在研究 Java 中的加密,并且遇到了奇怪的行为。使用 加密 byte[] 数据时 InputStream fin = new ByteArrayInputStream(data); Ciphe
我正在编写基于客户端-服务器的 Java 应用程序,但遇到了一个问题,因为在客户端和服务器中构造 ObjectInputStream 时它会挂起。 客户: Socket socket = new So
我是一名优秀的程序员,十分优秀!