java - 如何解决“索引越界错误”?
问题描述
我使用这个密钥作为 3DES 密钥与 RSA 交换---> private static final byte[] MY_KEY = "5oquil2oo2vb63e8ionujny6l9k8".getBytes();
public byte[] encrypt_des(String message) throws Exception {
byte[] encrypted = RSA.this.encrypt(MY_KEY);
final MessageDigest md = MessageDigest.getInstance("md5");
final byte[] digestOfPassword = md.digest(RSA.this.encrypt(MY_KEY));
final byte[] keyBytes = Arrays.copyOf(digestOfPassword, 24);
for (int j = 0, k = 1; k < encrypted.length;) {
keyBytes[k++]=keyBytes[j++];
}
final SecretKey key = new SecretKeySpec(keyBytes, "DESede");
final IvParameterSpec iv = new IvParameterSpec(new byte[8]);
final Cipher cipher = Cipher.getInstance("DESede/CBC/PKCS5Padding");
cipher.init(Cipher.ENCRYPT_MODE, key, iv);
final byte[] plainTextBytes = message.getBytes("utf-8");
final byte[] cipherText = cipher.doFinal(plainTextBytes);
// final String encodedCipherText = new sun.misc.BASE64Encoder()
// .encode(cipherText);
return cipherText;
}
并且此错误出现在输出中---> 线程“main”中的异常 java.lang.ArrayIndexOutOfBoundsException: Index 24 out of bounds for length 24 at RSA.encrypt_des(RSA.java:100) at RSA.main(RSA.java :62)。我该如何解决这个错误?以及如何在 3DES 中使用来自 RSA 的加密/解密密钥(上述方法)?
解决方案
推荐阅读
- authentication - 创建配置文件问题 - Ubuntu 20.04.1 上的 Twilio CLI
- android - Android:在屏幕上定位文字。Google ML Kit 边界框有点偏离
- r - 将 by() 的回归输出合并到一个表中
- powershell - 将列表转换为字符串 Powershell
- apache-spark - 合并重叠日期的记录
- elixir - 如何更新另一个地图内的地图值(ELIXIR)
- flutter - 使用 data_connection_checker 包时使用不相关类型的引用调用相等运算符`==`
- pgp - Openpgp:使用 python 包装器 python-gnupg 'error - sign-encrypt 17' - 仅在签名时出错
- java - 问题自动更改菜单 Android Studio
- html - 用 .NET 中的占位符图像替换所有图像