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

Java解密加密字符串:解析给定encode函数的工作原理与逆向实现

解密Java加密函数的实现思路与步骤

Hey there! Let's break down this encryption function step by step, then work out how to reverse it properly.

加密函数的工作原理

First, let's unpack what the encode function actually does—it has two key steps:

1. 单个字符的转换逻辑

The line c += c+i is a shorthand for c = c + c + i, which simplifies to:

加密后字符的ASCII值 = 原字符ASCII值 × 2 + 当前索引i

For example, if the original character is 'a' (ASCII 97) at index i=0, the encrypted character becomes 97*2 + 0 = 194, which corresponds to the character Â.

2. 整体字符串反转

After processing every character in the input message, the entire sequence of transformed characters is reversed. So if the transformed sequence is [A, B, C], the final encrypted string becomes [C, B, A].

解密的思路与实现

To decrypt, we need to reverse the encryption steps in reverse order (since the last step of encryption was reversing, we start with that):

解密步骤

  1. Reverse the encrypted string first: This restores the sequence of transformed characters to the order they were in before the final encryption step.
  2. Calculate the original character for each position:
    From the encryption formula c_new = 2*c_old + i, we can rearrange to get the original character:

    原字符ASCII值 = (加密后字符的ASCII值 - 当前索引i) ÷ 2

    • Important: (c_new - i) must be even—if not, the input encrypted string is invalid (since encryption always produces a value where this subtraction results in an even number).

Java解密代码实现

public static String decode(String encryptedMessage) {
    // Step 1: Reverse the encrypted string to get back the transformed character sequence
    StringBuilder reversedTransformed = new StringBuilder(encryptedMessage).reverse();
    StringBuilder originalMessage = new StringBuilder();
    
    for (int i = 0; i < reversedTransformed.length(); i++) {
        char cNew = reversedTransformed.charAt(i);
        // Calculate original character using the reversed formula
        int originalCharCode = (cNew - i) / 2;
        
        // Optional: Validate the result to catch invalid input
        if (2 * originalCharCode + i != cNew) {
            throw new IllegalArgumentException("Invalid encrypted string at index " + i);
        }
        
        originalMessage.append((char) originalCharCode);
    }
    
    return originalMessage.toString();
}

Test Example

Let's test with the original string "hello":

  • Encrypted string (from the encode function) is "âÛÚËÐ"
  • Run decode("âÛÚËÐ"):
    1. Reverse to get "ÐËÚÛâ"
    2. For each index:
      • i=0: (208 - 0)/2 = 104 → 'h'
      • i=1: (203 - 1)/2 = 101 → 'e'
      • i=2: (218 - 2)/2 = 108 → 'l'
      • i=3: (219 - 3)/2 = 108 → 'l'
      • i=4: (226 - 4)/2 = 111 → 'o'
    3. Returns the original string "hello"

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.26 08:37:15