- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试使用 AES 密码来加密一些字节,但它返回一个无提示错误,这意味着我输入如下内容:
byte[] raw = new String("Test","UTF8").getBytes("UTF8");
并且它不会返回任何内容。我认为问题出在 ByteArrayInput
/OutputStreams
但我不知道如何以其他方式做到这一点..
这是有问题的代码。
public byte[] encrypt(byte[] in) {
byte[] encrypted = null;
try {
aesCipher.getInstance("AES/CBC/PKCS5Padding");
aesCipher.init(Cipher.ENCRYPT_MODE, aeskeySpec);
ByteArrayInputStream bais = new ByteArrayInputStream(in);
ByteArrayOutputStream baos = new ByteArrayOutputStream(bais.available());
CipherOutputStream os = new CipherOutputStream(baos, aesCipher);
copy(bais, os);
os.flush();
byte[] raw = baos.toByteArray();
os.close();
encrypted = Base64.encodeBase64(raw);
} catch (FileNotFoundException ex) {
Logger.getLogger(FileEncryption.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) {
Logger.getLogger(FileEncryption.class.getName()).log(Level.SEVERE, null, ex);
} catch (InvalidKeyException ex) {
Logger.getLogger(FileEncryption.class.getName()).log(Level.SEVERE, null, ex);
} catch (NoSuchAlgorithmException ex) {
Logger.getLogger(FileEncryption.class.getName()).log(Level.SEVERE, null, ex);
} catch (NoSuchPaddingException ex) {
Logger.getLogger(FileEncryption.class.getName()).log(Level.SEVERE, null, ex);
}
return encrypted;
}
这是同一类中的另一个有效的函数:
public void encrypt(File in, File out) {
try {
aesCipher.getInstance("AES/CBC/PKCS5Padding");
aesCipher.init(Cipher.ENCRYPT_MODE, aeskeySpec);
FileInputStream is;
is = new FileInputStream(in);
CipherOutputStream os = new CipherOutputStream(new FileOutputStream(out), aesCipher);
copy(is, os);
os.close();
} catch (FileNotFoundException ex) {
Logger.getLogger(FileEncryption.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) {
Logger.getLogger(FileEncryption.class.getName()).log(Level.SEVERE, null, ex);
} catch (InvalidKeyException ex) {
Logger.getLogger(FileEncryption.class.getName()).log(Level.SEVERE, null, ex);
} catch (NoSuchAlgorithmException ex) {
Logger.getLogger(FileEncryption.class.getName()).log(Level.SEVERE, null, ex);
} catch (NoSuchPaddingException ex) {
Logger.getLogger(FileEncryption.class.getName()).log(Level.SEVERE, null, ex);
}
}
private void copy(InputStream is, OutputStream os) throws IOException {
int i;
byte[] b = new byte[2048];
while ((i = is.read(b)) != -1) {
os.write(b, 0, i);
}
}
最佳答案
首先引起我注意的是这句话:
aesCipher.getInstance("AES/CBC/PKCS5Padding");
假设aesCipher
是Cipher
类型的变量,您在此处调用静态Cipher.getInstance
并丢弃结果(而不是分配它到任何变量)。 IE。此行根本没有任何效果,aesCipher
与此行之前相同。
如果之前是null
,那么它仍然是null
,并且下一行(调用非静态方法)会给你一个NullPointerException。如果您的代码默默地吞噬未知异常(这可能超出了显示的代码),那么这是一个普遍问题。
除此之外,我认为 CipherOutputStream 上的 flush
并不会真正刷新整个缓冲区,而只是刷新可以写入的 block ,而无需添加任何填充。此处使用 close()
而不是 flush()
(这似乎也适用于第二个示例)。
一般说明:A small self-contained complete compilable example将使我能够尝试并给你一个明确的答案,而不是仅仅猜测。例如,“不返回任何内容”并不是对方法行为的良好描述 - 该方法是否返回 null、空数组、抛出异常、永远阻塞?
关于java - 在 Java 中使用 CipherOutputStream,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7656293/
我是密码学新手,但我计划在以后的一些应用程序中使用它。 我想知道我制作的这个简短的演示程序中是否缺少某些组件。 我知道我正在用 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。如何将加密数据附加到已加密的文件中?这是因为下载可能会停止或连接可
我是一名优秀的程序员,十分优秀!