2

I have this method which takes a BigInteger, makes another BigInteger (via rsa algorithm) then thats converted to binary, then thats broken up into blocks of 8 where i get the ascii value for that binary string.

ALL OF THAT WORKS

but im having trouble getting the ascii chars that i get from each binary string and making a new string out of them. Im trying to use the concat method built in but it doesnt seem to be working!

 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; } 

as you can see in the comments thats what i am talking about, I mean by looking at the API for concat it seems what I am doing is right, but it just wont work!

Thanks if you could explain / show whats wrong!

5
  • First, note that String pt = null, so you are getting a null pointer Commented Apr 17, 2013 at 4:50
  • 1
    Second, you can just set String pt = ""; and have pt += c; at the place of interest. Also, you could skip c = new Character(...) and just do this: pt += "" + (char)charCode; Commented Apr 17, 2013 at 4:52
  • but wouldnt concat just take away nullity? Commented Apr 17, 2013 at 4:52
  • thanks man. i would vote that the answer if ya made it one :D worked like a charm! Commented Apr 17, 2013 at 4:56
  • Your approach using a string representation of the binary notation of the number is all very fine. If you were going for performance, though, I'd suggest you use BigInteger.toByteArray(). Drop the first byte if it is zero. Then use the remaining bytes to construct a String from these. This will be a lot shorter and faster, but more difficult to debug in case anything goes wrong. Commented Apr 17, 2013 at 7:23

1 Answer 1

4

1 . You should use StringBuilder to append a char to.

2 . Your code creates a lot of strings: plaintext.substring(i*8, (i*8)+8))

Below is the code that fixed both issues.

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); } 

Output

plaintext = 011000110110001001100001 output = abc 

UPD

I also changed for cycle to the view that looks more natural to me - so that i points to the "base" index.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.