首页 > 解决方案 > 如何解决“索引越界错误”?

问题描述

我使用这个密钥作为 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 的加密/解密密钥(上述方法)?

标签: java

解决方案


推荐阅读