- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在将 Java 应用程序移植到 OS X (10.8)。我们的一个单元测试在进行加密时失败(它适用于 Windows)。两者都运行 Java 7 Update 21,但 Windows 版本使用 32 位 JDK,Mac 版本使用 64 位 JDK。
在 Mac 上运行它时,尝试解密加密数据时出现以下异常:
Caused by: javax.crypto.BadPaddingException: Given final block not properly padded at com.sun.crypto.provider.CipherCore.doFinal(CipherCore.java:811) at com.sun.crypto.provider.CipherCore.doFinal(CipherCore.java:676) at com.sun.crypto.provider.AESCipher.engineDoFinal(AESCipher.java:313) at javax.crypto.Cipher.doFinal(Cipher.java:2087) at com.degoo.backend.security.Crypto.processCipher(Crypto.java:56) ... 25 more
这是加密类。
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
public final class Crypto {
private final static String CIPHER_ALGORITHM = "AES";
private final static String CIPHER_TRANSFORMATION = "AES/CBC/PKCS5Padding";
public final static int CRYPTO_KEY_SIZE = 16;
public static byte[] encryptByteArray(byte[] blockToEncrypt, int maxLengthToEncrypt, byte[] encryptionKey, byte[] ivBytes) {
return processCipher(blockToEncrypt, maxLengthToEncrypt, Cipher.ENCRYPT_MODE, ivBytes, encryptionKey);
}
public static byte[] decryptByteArray(byte[] encryptedData, byte[] encryptionKey, byte[] ivBytes) {
return processCipher(encryptedData, encryptedData.length, Cipher.DECRYPT_MODE, ivBytes, encryptionKey);
}
private static byte[] processCipher(byte[] blockToEncrypt, int maxLength, int cryptionMode, byte[] ivBytes, byte[] encryptionKey) {
try {
IvParameterSpec iv = new IvParameterSpec(ivBytes);
final Cipher cipher = initCipher(cryptionMode, iv, encryptionKey);
return cipher.doFinal(blockToEncrypt, 0, maxLength);
} catch (Exception e) {
throw new RuntimeException("Failure", e);
}
}
private static Cipher initCipher(int cryptionMode, IvParameterSpec iv, byte[] encryptionKey) {
KeyGenerator keyGen;
try {
keyGen = KeyGenerator.getInstance(CIPHER_ALGORITHM);
final SecureRandom randomSeed = new SecureRandom();
randomSeed.setSeed(encryptionKey);
keyGen.init(CRYPTO_KEY_SIZE * 8, randomSeed);
// Generate the secret key specs.
final SecretKey secretKey = keyGen.generateKey();
final SecretKeySpec secretKeySpec = new SecretKeySpec(secretKey.getEncoded(), CIPHER_ALGORITHM);
// Instantiate the cipher
final Cipher cipher = Cipher.getInstance(CIPHER_TRANSFORMATION);
cipher.init(cryptionMode, secretKeySpec, iv);
return cipher;
} catch (Exception e) {
throw new RuntimeException("Failure", e);
}
}
}
测试代码如下所示:
public void testEncryption() throws Exception {
int dataLength = TestUtil.nextInt(applicationParameters.getDataBlockMinSize());
byte[] dataToEncrypt = new byte[dataLength];
TestUtil.nextBytes(dataToEncrypt);
int keyLength = 16;
byte[] key = new byte[keyLength];
TestUtil.nextBytes(key);
byte[] ivBytes = new byte[16];
TestUtil.nextBytes(key);
long startTime = System.nanoTime();
byte[] encryptedBlock = Crypto.encryptByteArray(dataToEncrypt, dataToEncrypt.length, key, ivBytes);
long endTime = System.nanoTime();
System.out.println("Encryption-speed: " + getMBPerSecond(dataLength, startTime, endTime));
startTime = System.nanoTime();
byte[] decryptedData = Crypto.decryptByteArray(encryptedBlock, key, ivBytes);
endTime = System.nanoTime();
System.out.println("Decryption-speed: " + getMBPerSecond(dataLength, startTime, endTime));
if (encryptedBlock.length == decryptedData.length) {
boolean isEqual = true;
//Test that the encrypted data is not equal to the decrypted data.
for (int i = 0; i < encryptedBlock.length; i++) {
if (encryptedBlock[i] != decryptedData[i]) {
isEqual = false;
break;
}
}
if (isEqual) {
throw new RuntimeException("Encrypted data is equal to decrypted data!");
}
}
Assert.assertArrayEquals(dataToEncrypt, decryptedData);
}
最佳答案
我想我已经找到了。由于某种原因,上面的代码通过使用现有加密 key 播种 SecureRandom 实例来获取新的 byte[] 来派生加密 key (不要问我为什么,这是很久以前编写的)。然后将其提供给 SecretKeySpec 构造函数。如果我跳过所有这些,只向 SecretKeySpec 构造函数提供我们已有的加密 key ,则单元测试将通过。执行加密的代码现在如下所示:
final SecretKeySpec secretKeySpec = new SecretKeySpec(encryptionKey, CIPHER_ALGORITHM);
// Instantiate the cipher
final Cipher cipher = Cipher.getInstance(CIPHER_TRANSFORMATION);
cipher.init(cryptionMode, secretKeySpec, iv);
return cipher;
奇怪的是它可以在 Windows 上运行。看起来 SecureRandom 实现在 OS X 和 Windows 上的行为有所不同。在 OS X 上调用 setSeed 会附加到种子,而 Windows 则会替换它。
更新:发现了有关 SecureRandom 实现差异的更多详细信息:http://www.cigital.com/justice-league-blog/2009/08/14/proper-use-of-javas-securerandom/
关于java - 将 Java 应用程序移植到 OS X 后发生 BadPaddingException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18742733/
我是 Mercurial 的新手,并且不知何故仍处于评估过程中,所以这四个概念对我来说有点困惑。有些被提到等同于 Git 的 Staging/Index 概念,有些甚至比 Git 的 Staging
如何将在某些网站 (www.example1.com) 上用某种语言即 (java) 制作的 session 传送到用其他语言制作的网站,即在某些其他网站上的 (php),即 (www.example
我有以下代码行我想移植到 Torch Matmul rotMat = xmat @ ymat @ zmat 我能知道这是不是正确的顺序: rotMat = torch.matmul(xmat, tor
我正在尝试移植一个内部有一个联合的 C 结构。 Winapi.Winsock2.pas 中的默认结构记录中缺少某些字段。 但这是正确的方法吗?谢谢。 typedef struct _WSACOMPLE
我想将基于 webkit 的浏览器移植到我的堆栈中。谁能介绍一下 webkit 浏览器引擎的组织结构?目前我所知道的是它具有用于呈现 html 和解析 javascript 的核心。我想了解更多,比如
我目前有一个 ActiveX 控件,它链接到许多 c/c++ dll。问题是我们现在需要此控件在 IE 以外的浏览器(最重要的是 Firefox)上运行。 在我看来,我有以下选择: 将控件编写为 fi
我正在尝试在 Objective-C 中重写 Java 库。我想将其重写为 API,以便需要实现某些方法。我已经开始尝试重写代码,但遇到了一些问题。 Objective-C 是否支持抽象类? 如果没有
我已经有一段时间没有接触 SQL 了,所以我需要重新学习一下。我的计算机上运行着一个 SQL 数据库,我的服务器是 localhost。我在 VB.Net 中制作了一个连接到该数据库的应用程序。一切都
按照目前的情况,这个问题不适合我们的问答形式。我们希望答案得到事实、引用或专业知识的支持,但这个问题可能会引发辩论、争论、投票或扩展讨论。如果您觉得这个问题可以改进并可能重新打开,visit th
运行命令时出现错误 [root@himanshi busybox-1.20.2]# make ARCH=arm CROSS_COMPILE=arm-unknown-linux-gnueabi- CON
我需要将为 iPhone 编写的现有游戏移植到 Flash。 iPhone 游戏主要是用纯 C 而不是 Objective C 编写的。 我想知道是否有任何好的工具可以将 C 代码直接转换为 Acti
我将要在 Smalltalk (Pharo) 中构建一个项目。还有一个 python 库,我打算将其用于相同的目的。现在,有 3 个选项: 那些 python 库的 Smalltalk 包装器 将 p
我必须在 GPU 上移植一个广泛使用随机数的结构。一切都可以毫无问题地移植,但随机生成器函数是唯一在该类的所有函数中被广泛调用的函数。我虽然可以简单地将它重新实现为类本身的内部设备函数。下面我放了一个
我对整个移植问题有点陌生,因为 Android SDK 提供的模拟器速度很慢,所以我解决了这个问题。 我下载了 android-x86-3.2-RC2-eeepc 和 android-x86-3.2-
我们的数据库 (PostgreSQL 9.x) 中有一些 PL/pgSQL 存储过程。 这些是严格顺序的,在某些情况下,可能会非常慢。 我们正在考虑将它们移植到 PL/Java、PL/Python 或
我有一个 Android 应用程序可以处理圆顶图像。出于性能原因,我想用 C++ 编写应用程序的某些部分,然后通过 NDK 调用这些方法。我是否需要一个特定的 C++ 编译器(例如用于嵌入式系统)或者
我正在从事一个将一大堆 OS-9(微软件)代码移植到 linux 的项目。 OS-9 中的信号处理功能允许您创建自己的信号,或者至少它是如何实现的(intercept() 函数)。我对 linux 信
目前我有这个 gtk2 代码: GList *input_devices = gdk_devices_list(); while(input_devices) { GdkDevice *devic
我正在尝试移植 Aether.Physics2D从 C# 到 Xojo 的库。这本质上是 Farseer 物理引擎的调整版本。大部分已经完成,但有一部分源代码我无法解决(可能是因为 C# 不是我的主要
我们正在开发采用 RISCV 架构的多核处理器。 我们已经为单核 RISCV 处理器移植了 Linux,它正在我们自己的基于 FPGA 的主板上使用 busybox rootfs。 我现在想为多核 R
我是一名优秀的程序员,十分优秀!