aes密钥扩展java实现_需要使用bouncycastle将AES 256密钥“扩展”为...

AES-128使用10轮,AES-192使用12轮,AES-256使用14轮.

使用BouncyCastle,可以在名为org.bouncycastle.crypto.engines.AESEngine的类中的私有方法generateWorkingKey()中实现它.这不是通用的实现:

该方法开始于:

[...]

int KC = keyLen >>> 2;

ROUNDS = KC + 6; // This is not always true for the generalized Rijndael that allows larger block sizes

int[][] W = new int[ROUNDS+1][4]; // 4 words in a block

switch (KC) {

case 4:

[...]

for (int i = 1; i <= 10; ++i)

阅读此代码,很容易看到,如果密钥长度为128位,则keyLen值为16(字节),因此KC为4,则ROUNDS为10(这是循环的上限).但这是硬编码的.对于其他密钥长度,在包含循环(上限为轮数)的开关情况下,也以相同的方式对其进行硬编码.

因此,对于您的Cryptonight实现,其密钥长度为256位,但经过10次回合,BouncyCastle实现中的硬编码值不符合您的需求.

因此,您需要派生BouncyCastle并修改此代码以实现您的目标.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Java 中可以使用 Bouncy Castle 库来实现 AES 解密。下面是一个简单的示例代码: ``` import org.bouncycastle.crypto.BufferedBlockCipher; import org.bouncycastle.crypto.CipherKeyGenerator; import org.bouncycastle.crypto.engines.AESFastEngine; import org.bouncycastle.crypto.generators.PKCS5S2ParametersGenerator; import org.bouncycastle.crypto.modes.CBCBlockCipher; import org.bouncycastle.crypto.params.KeyParameter; import org.bouncycastle.crypto.params.ParametersWithIV; import javax.crypto.Cipher; import javax.crypto.spec.IvParameterSpec; import javax.crypto.spec.SecretKeySpec; import java.security.NoSuchAlgorithmException; import java.security.SecureRandom; import java.security.spec.InvalidKeySpecException; import java.util.Base64; public class AesUtil { private static final int KEY_SIZE = 256; private static final int IV_SIZE = 128; private static final int SALT_SIZE = 8; private static final int ITERATION_COUNT = 65536; public static String decrypt(String encodeData, String passphrase) throws Exception { byte[] encryptedBytes = Base64.getDecoder().decode(encodeData); byte[] salt = new byte[SALT_SIZE]; System.arraycopy(encryptedBytes, 0, salt, 0, SALT_SIZE); byte[] iv = new byte[IV_SIZE / 8]; System.arraycopy(encryptedBytes, SALT_SIZE, iv, 0, iv.length); byte[] encrypted = new byte[encryptedBytes.length - SALT_SIZE - iv.length]; System.arraycopy(encryptedBytes, SALT_SIZE + iv.length, encrypted, 0, encrypted.length); PKCS5S2ParametersGenerator generator = new PKCS5S2ParametersGenerator(); generator.init(passphrase.getBytes(), salt, ITERATION_COUNT); KeyParameter keyParam = (KeyParameter) generator.generateDerivedParameters(KEY_SIZE); BufferedBlockCipher cipher = new BufferedBlockCipher(new CBCBlockCipher(new AESFastEngine())); cipher.init(false, new ParametersWithIV(keyParam, iv)); byte[] decrypted = new byte[cipher.getOutputSize(encrypted.length)]; int len = cipher.processBytes(encrypted, 0, encrypted.length, decrypted, 0); len += cipher.doFinal(decrypted, len); return new String(decrypted, 0, len); } } ``` 这个示例代码中使用了 Bouncy Castle 库来生成加密密钥和 IV,并使用 CBC 模式进行解密。同时也利用了 Java 内置的 AES 解密功能。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值