You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用XOR位运算符实现消息加解密时遇数组越界异常求助

Fixing StringIndexOutOfBoundsException in XOR Encryption/Decryption Java Code

Let's break down why you're hitting that StringIndexOutOfBoundsException and fix it step by step:

The Root Cause

Your encryption loop is trying to read characters from the empty encmsg string instead of the original message msg. When the loop starts, encmsg has a length of 0—so calling encmsg.charAt(0) on the first iteration immediately throws an index out-of-bounds error. The decryption loop has the exact same issue: you're trying to read from the empty decmsg instead of the encrypted encmsg.

The Fix

We need to adjust which string we're reading characters from in each loop:

  • For encryption: Use characters from the original msg to perform the XOR operation
  • For decryption: Use characters from the encrypted encmsg (since XOR is reversible, applying the same key again will restore the original text)

Here's the corrected code:

class Search {
    public static void main(String[] args) {
        String msg = "This is a test";
        String encmsg = "";
        String decmsg = "";
        int key = 88;
        System.out.print("Original message: ");
        System.out.println(msg);
        
        // Encode using original message characters
        for(int i=0; i < msg.length(); i++)
            encmsg = encmsg + (char) (msg.charAt(i) ^ key);
        System.out.print("Encoded message: ");
        System.out.println(encmsg);
        
        // Decode using encrypted message characters
        for(int i=0; i < encmsg.length(); i++)
            decmsg = decmsg + (char) (encmsg.charAt(i) ^ key);
        System.out.print("Decoded message: ");
        System.out.println(decmsg);
    }
}

Optional Optimization

String concatenation in loops (encmsg = encmsg + ...) creates a new String object every iteration, which is inefficient for longer messages. For better performance, use StringBuilder instead:

class Search {
    public static void main(String[] args) {
        String msg = "This is a test";
        int key = 88;
        System.out.print("Original message: ");
        System.out.println(msg);
        
        // Encode with StringBuilder
        StringBuilder encBuilder = new StringBuilder();
        for(int i=0; i < msg.length(); i++) {
            encBuilder.append((char) (msg.charAt(i) ^ key));
        }
        String encmsg = encBuilder.toString();
        System.out.print("Encoded message: ");
        System.out.println(encmsg);
        
        // Decode with StringBuilder
        StringBuilder decBuilder = new StringBuilder();
        for(int i=0; i < encmsg.length(); i++) {
            decBuilder.append((char) (encmsg.charAt(i) ^ key));
        }
        String decmsg = decBuilder.toString();
        System.out.print("Decoded message: ");
        System.out.println(decmsg);
    }
}

Run either version, and you'll get your expected output:

Original message: This is a test
Encoded message: 01+x1+x9x,=+,
Decoded message: This is a test

内容的提问来源于stack exchange,提问作者Edoardo Fiorini

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.09 09:22:27