- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在使用 Java 的加密库并收到 IllegalBlockSizeException。
我目前正在尝试以 XML 文件格式提取数据库内容。在数据转储期间,我正在创建一个 list 文件,其中包含一个字符串,该字符串使用数据库中定义的 key 进行解密。
稍后,当 XML 文件的内容加载到另一个数据库中时,它会从该数据库获取 key 并使用它来解密 list 。如果解密后的 list 与原始内容不匹配,则意味着源数据库和目标数据库中的加密 key 不匹配,并且用户会收到此通知。
以下是代码。 EncryptionEngine 对象是一个单例,它使用 Java 加密库抽象出许多加密细节。假设它工作正常,因为它是相当古老且成熟的代码。
这都是我制作的类(class)中的内容。首先,我们有这些数据成员:
private final String encryptedManifestContents;
private final static String DECRYPTED_MANIFEST_CONTENTS = "This file contains the encrypted string for validating data in the dump and load process";
final static String ENCRYPTED_MANIFEST_FILENAME = "manifest.bin";
首先是加密过程。该字符串的加密方式如下:
final EncryptionEngine encryptionEngine = EncryptionEngine.getInstance();
encryptedManifestContents = encryptionEngine.symmetricEncrypt(DECRYPTED_MANIFEST_CONTENTS); // The contents get converted to bytes via getBytes("UTF-8")
然后写入 list 文件(目标只是一个以字符串形式保存文件路径的变量):
EncryptedManifestUtil encryptedManifestUtil = new EncryptedManifestUtil(); // The class I've created. The constructor is the code above, which just initialized the EncryptionEngine and encrypted the manifest string.
manifestOut = new FileOutputStream(destination + "/" + ENCRYPTED_MANIFEST_FILENAME);
manifestOut.write(encryptedManifestUtil.encryptedManifestContents.getBytes("UTF-8"));
至此,加密过程完成。我们获取了一个字符串,对其进行了加密,然后按顺序将内容写入到一个文件中。现在,当有人加载数据时,解密过程就会开始:
BufferedReader fileReader = new BufferedReader(new FileReader(filename)); // Filename is the manifest's file name and location
final EncryptionEngine encryptionEngine = EncryptionEngine.getInstance();
String decryptedManifest = encryptionEngine.decryptString(fileReader.readLine().getBytes("UTF-8")); // This is a symmetric decrypt
当解密发生时,它会抛出此异常:
Caused by: javax.crypto.IllegalBlockSizeException: last block incomplete in decryption
at org.bouncycastle.jcajce.provider.symmetric.util.BaseBlockCipher.engineDoFinal(Unknown Source)
at javax.crypto.Cipher.doFinal(DashoA13*..)
它似乎可以正确读取和写入文件,但内容对我来说是乱码。 fileReader.readLine() 的结果是:
9�Y�������䖷�߾��=Ă��� s7Cx�t�b��_-(�b��LFA���}�6�f����Ps�n�����ʢ�@�� �%��%�5P�p
感谢您的帮助。
编辑:所以我改变了写入文件的方式。
回想一下这一行:
encryptedManifestContents = encryptionEngine.symmetricEncrypt(DECRYPTED_MANIFEST_CONTENTS);
加密首先从输入的字符串中获取字节,然后解密,然后通过首先将其编码为基本 64 字节将字节更改回字符串。然后它将基本 64 字节数组转换回字符串。
考虑到这一点,我将文件编写器更改为 PrintWriter 而不是 FileOutputStream,并直接将字符串而不是字节写入文件。不幸的是我仍然收到错误。然而,读取行生成的字符串中的 � 似乎较少。
最佳答案
看起来问题出在你的 fileReader.readLine() 上 - 你正在将字节流写入文件,然后将其作为字符串读回。相反,您应该读取字节流,例如refer to this question ,或者使用Base64 Encoding要将字节数组转换为字符串,请将其写入文件,从文件中读取,然后将其转换回字节数组。
关于java - 加密/解密,获取 IllegalBlockSizeException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15886657/
我在使用以下代码时遇到了一些问题 - 我似乎收到了 IllegalBlocksizeException 并且不确定我在这里可能做错了什么?可以获得一些建议/指示吗? 谢谢 public cla
我在 JRE 1.6 上使用 AES 128 位加密。 我在尝试解密由 encrypt() 生成的字符串时不断收到此异常: 2014-11-19 14:40:10.831 28 javax.c
我编写了一个加密和解密函数。加密工作正常,但我总是在解密时遇到 IllegalBlockSizeException。 public static String aes_encrypt (String
我在处理解密方法时遇到了问题。加密产生了正确的输出,但是当我解密完全相同的加密字符串时(应该返回明文字符串),它不起作用。 import javax.crypto.Cipher; import jav
String key = "my_own_key_which_should_be_quite_long"; byte[] keyData = key.getBytes(); SecretKeySpec
我当前编写的解密算法如下, public String decrypt(String enc) throws Exception { Key key = k;
我的加密方法是: private static final String ALGORITHM = "AES/ECB/PKCS5Padding"; private static final byte[]
我在使用 Cipher 时观察到以下情况. 加密代码: Cipher aes = Cipher.getInstance("AES"); aes.init(Cipher.ENCRYPT_MODE, ge
我写了一些聊天内容,消息是类似的对象 {type="message",sender="userA",content="plaintextmessage",recipient="userB"} 发送到服
我正在使用 Java 的加密库并收到 IllegalBlockSizeException。 我目前正在尝试以 XML 文件格式提取数据库内容。在数据转储期间,我正在创建一个 list 文件,其中包含一
我已经使用 RSA 的公钥加密成功地加密了我的 AES key 。在使用 RSA 的私钥解密时,我得到: javax.crypto.IllegalBlockSizeException 适用于我的加密逻
我有一个硬编码 key ,我想在将字符串存储在 SharedPreferences 中之前使用它对其进行加密。这是我到目前为止的代码: public class TokenEncryptor {
我有一个硬编码 key ,我想在将字符串存储到 SharedPreferences 之前用它加密字符串。这是我到目前为止的代码: public class TokenEncryptor { p
我在java中使用AES/CBC/PKCS5Padding填充标准,我的 friend 在c#.NET中使用PKCS7标准如果我的 friend 使用 AES 加密数据并向我发送 key ,那么我就可
我目前正在开展一个夏季项目,该项目使用 AES 加密或解密文件。但是,当使用与加密对象相同的 key 时,我在尝试解密时收到非法 block 大小异常。我对密码学很陌生,但也是一位经验丰富的 Java
我正在尝试加密/解密文件中的文本,但收到以下错误: Exception in thread "main" javax.crypto.IllegalBlockSizeException: Input l
我正在尝试使用 jks Keystore 文件加密和解密字符串。但是在解密时出现以下错误... 这是我的加密和解密类(class): package com.Encrypt; import java.
我使用DES 算法来加密/解密我的文本。它与拉丁文本完美搭配。 但是当我开始加密/解密西里尔文本时,解密的文本显示为 ?????? ???????? 在我的 TextField 表单和控制台中。我该如
RSA key 是使用以下代码生成的: RSAKeyGenParameterSpec rsaSpec = new RSAKeyGenParameterSpec(2048, RSAKeyGenParam
我使用 aes-256-cbc 进行数据加密/解密。 当使用 php 代码解密数据时,我没有收到错误。 PHP 代码如下: $key = 'd7df7d66bc110ba2e03a3a647ecd8c
我是一名优秀的程序员,十分优秀!