2

我有这种方法,它需要一个 BigInteger,生成另一个 BigInteger(通过 rsa 算法),然后将其转换为二进制,然后将其分解为 8 个块,在其中我得到该二进制字符串的 ascii 值。

所有这些都有效

但是我无法从每个二进制字符串中获取 ascii 字符并从中创建一个新字符串。我试图使用内置的 concat 方法,但它似乎不起作用!

    public static String Decrypt( BigInteger ct, BigInteger d, BigInteger mod ){

    String pt = null;
    BigInteger message = ct.modPow(d, mod);     //the decrypted message M but still in BigInteger form
    String plaintext = message.toString(2);

    if( plaintext.length() % 8 != 0 ){
        plaintext = "00000000".substring( plaintext.length() % 8 ) + plaintext;
    }

    String c;
    int charCode = 0;
    for( int i = (plaintext.length()/8) - 1 ; i >= 0; i--){

        charCode = Integer.parseInt((plaintext.substring(i*8, (i*8)+8)) , 2) ;
        c = new Character( (char) charCode).toString();
        System.out.print(c);        // here is where i need something like pt.concat(c) or something like that, I dont really want it printed
    }                               // i just want all of these chars to be put into the string pt 
    System.out.println();
    return pt;
}

正如您在评论中看到的那样,这就是我所说的,我的意思是通过查看 concat 的 API,我所做的似乎是正确的,但它就是行不通!

谢谢你能解释/显示什么是错的!

4

1 回答 1

4

1. 您应该使用 StringBuilder 附加一个字符。

2. 您的代码创建了很多字符串:plaintext.substring(i*8, (i*8)+8))

下面是解决这两个问题的代码。

public static void main(String... args) {

    String plaintext = "";

    { // preparing for test
        String input = "abc";
        for (char ch : input.toCharArray()) {
            String charAs8bits = String.format("%8s",
                    String.valueOf(Integer.toBinaryString(ch))).replace(
                    ' ', '0');
            plaintext = charAs8bits + plaintext;
        }
        System.out.println("plaintext = " + plaintext);
    }

    StringBuilder output = new StringBuilder();
    for (int i = plaintext.length() - 8; i >= 0; i -= 8) {

        boolean isDigit = Character.isDigit(plaintext.charAt(i)) //
                && Character.isDigit(plaintext.charAt(i + 1)) //
                && Character.isDigit(plaintext.charAt(i + 2)) //
                && Character.isDigit(plaintext.charAt(i + 3)) //
                && Character.isDigit(plaintext.charAt(i + 4)) //
                && Character.isDigit(plaintext.charAt(i + 5)) //
                && Character.isDigit(plaintext.charAt(i + 6)) //
                && Character.isDigit(plaintext.charAt(i + 7)) //
        ;

        if (isDigit) {
            int num = (Character.digit(plaintext.charAt(i), 2) << 7) //
                    + (Character.digit(plaintext.charAt(i + 1), 2) << 6) //
                    + (Character.digit(plaintext.charAt(i + 2), 2) << 5) //
                    + (Character.digit(plaintext.charAt(i + 3), 2) << 4) //
                    + (Character.digit(plaintext.charAt(i + 4), 2) << 3) //
                    + (Character.digit(plaintext.charAt(i + 5), 2) << 2) //
                    + (Character.digit(plaintext.charAt(i + 6), 2) << 1) //
                    + (Character.digit(plaintext.charAt(i + 7), 2)) //
            ;
            output.append((char) num);
        }
    }
    System.out.print("output = " + output);
}

输出

plaintext = 011000110110001001100001
output = abc

UPD

我还将for循环更改为对我来说看起来更自然的视图-i指向“基本”索引。

于 2013-04-17T05:53:47.180 回答