- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我一直在尝试用 AES 编写一个加密文件,然后使用 JCA 中提供的密码流对其进行解密。但是,我在读取文件时遇到了问题,因为解密正在失控。
public class CipherStreams {
public static void main(String[] args) {
try {
KeyGenerator keygen = KeyGenerator.getInstance("AES");
Key k = keygen.generateKey();
Cipher aes = Cipher.getInstance("AES/ECB/PKCS5Padding");
aes.init(Cipher.ENCRYPT_MODE, k);
FileOutputStream fs = new FileOutputStream("Encrypyed.txt");
CipherOutputStream out = new CipherOutputStream(fs, aes);
out.write("[Hello:Okay]\nOkay".getBytes());
out.close();
Cipher aes2 = Cipher.getInstance("AES/ECB/PKCS5Padding");
aes2.init(Cipher.DECRYPT_MODE, k);
FileInputStream fis = new FileInputStream("Encrypyed.txt");
CipherInputStream in = new CipherInputStream(fis,aes2);
byte[] b = new byte[8];
int i = in.read(b);
while(i!=-1) {
System.out.print((char)i);
i = in.read(b);
}
} catch (NoSuchAlgorithmException | NoSuchPaddingException | InvalidKeyException | IOException ex) {
Logger.getLogger(CipherStreams.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
最佳答案
您不是在写入读取的字节数,而是写入正在读取的字节数。
您还假设默认平台编码只是将每个字符转换为一个字节。
只需执行与写入时相反的操作:读取所有内容,并将读取的字节数组转换为字符串,然后打印该字符串:
public class CipherStreams {
public static void main(String[] args) {
try {
KeyGenerator keygen = KeyGenerator.getInstance("AES");
Key k = keygen.generateKey();
Cipher aes = Cipher.getInstance("AES/ECB/PKCS5Padding");
aes.init(Cipher.ENCRYPT_MODE, k);
String fileName = "Encrypted.txt";
FileOutputStream fs = new FileOutputStream(fileName);
CipherOutputStream out = new CipherOutputStream(fs, aes);
out.write("[Hello:Okay]\nOkay".getBytes());
out.flush();
out.close();
Cipher aes2 = Cipher.getInstance("AES/ECB/PKCS5Padding");
aes2.init(Cipher.DECRYPT_MODE, k);
FileInputStream fis = new FileInputStream(fileName);
CipherInputStream in = new CipherInputStream(fis, aes2);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
byte[] b = new byte[1024];
int numberOfBytedRead;
while ((numberOfBytedRead = in.read(b)) >= 0) {
baos.write(b, 0, numberOfBytedRead);
}
System.out.println(new String(baos.toByteArray()));
}
catch (NoSuchAlgorithmException | NoSuchPaddingException | InvalidKeyException | IOException ex) {
ex.printStackTrace();
;
}
}
}
关于java - 使用 CipherInputStream 和 CipherOutputStream 加密和解密文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41413439/
我是密码学新手,但我计划在以后的一些应用程序中使用它。 我想知道我制作的这个简短的演示程序中是否缺少某些组件。 我知道我正在用 300 字节进行假设,如果有办法绕过我想知道的猜测数组大小, impor
我正在尝试加密/解密一个文件,但我遇到了 ByteArrayOutputStream 和 CipherOutputStream 的问题。我可以加密一个文件,但是不能解密这个文件。我尝试在 Cipher
我想多次加密一个字符串。但我不知道为什么我以空字节数组结束。一个公钥可以,但添加另一个公钥会返回空结果。有谁知道为什么? private static byte[] encrypt(LinkedLis
我有一个由另一个 OutputStream 支持的 CipherOutputStream。在我将所有需要加密的数据写入 CipherOutputStream 之后,我需要附加一些未加密的数据。 The
我正在尝试使用公钥加密 Java 中的一些二进制数据,如这个有用页面中所述: http://www.junkheap.net/content/public_key_encryption_java 按照
这个问题已经有答案了: Initial bytes incorrect after Java AES/CBC decryption (10 个回答) 已关闭 4 年前。 我在将 FileInputSt
这是我的代码: import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream;
我有以下代码。但是,文件 b.xlsx 和 c.xlsx 的大小为 0 字节。为什么CipherOuputSteam不工作? public static void main(String[] args
我正在寻找一种方法,在给定任意大小的密码的情况下,使用 AES 加密我通过流接收的文件。为了让事情更容易开始,我试图将一些加密消息写入文件。但我遇到了麻烦: 我希望能够定义任意大小的密码。是否可以以“
尝试编写客户端/服务器程序,其中客户端读取文本文件并使用 CipherOutputStream 将其发送到服务器套接字。预期的文本文件已创建但为空,并且出现以下错误 Read Length-1 EOF
我有这个代码可以通过网络发送加密数据: s = new Socket(serverAddress, serverPort); is = s.getInputStream(); os = s.getOu
这个问题已经有答案了: CipherInputStream and CipherOutputStream are not generating files (1 个回答) 已关闭 6 年前。 我正在尝
我正在尝试使用 AES 密码来加密一些字节,但它返回一个无提示错误,这意味着我输入如下内容: byte[] raw = new String("Test","UTF8").getBytes("UTF8
我正在尝试使用 this example 对字符串进行 3DES 加密并将其存储在属性文件中.我遇到的问题是我不想直接从方法中将 encrypt() 和 decrypt() 的内容写入文件。我想将它存
我正在用 Java 破坏一个非常简单的 RSA 加密。但输出文件似乎是空的。如果我在没有 CipherOutputStream 的情况下尝试,同样的事情会起作用。我可以看到每一个写循环中的循环无效。任
我正在尝试使用 CipherOutputStream 加密字符串并将加密的字节存储在原始字节数组中,它由 ByteArrayOutputStream 支持,但 ByteArrayOutputStrea
我一直在尝试用 AES 编写一个加密文件,然后使用 JCA 中提供的密码流对其进行解密。但是,我在读取文件时遇到了问题,因为解密正在失控。 public class CipherStreams { p
我正在使用一个简单的 CipherInput/OutputStream 来尝试在 android 中加密/解密文件。 我遇到的问题是它似乎损坏了文件的前几个字节,但没有损坏其余部分。下面是一个简单文本
我想创建一个 OutputStream来自另一个 OutputStream其中新OutputStream将自动加密我写入的内容 OutputStream .我想使用 Bouncy CaSTLe,因为我
因此,我的应用程序从互联网下载视频,但在下载时对其进行即时加密,并将加密数据写入存储。我正在使用 AES/CTR/NoPadding。如何将加密数据附加到已加密的文件中?这是因为下载可能会停止或连接可
我是一名优秀的程序员,十分优秀!