Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Problem with BigInteger and String ...

Status
Not open for further replies.

rahmanjan

Programmer
Apr 13, 2003
180
AU
Hi evreyone,

I am passing an encrypted message from my client application to my server application. (just for testing purposes) In the server i get the message as:

String ciphertext = input.readLine();

To Decrypt the message I need to pass it to decrypt function which gets arguments as BigInteger[] something like:


public static String Decrypt(BigInteger[] ciphertext)
{
String strRecoveredPlaintext = rsa.decrypt( ciphertext ) ;
return strRecoveredPlaintext;
}

My question is how can i covert my ciphertext which is string of numbers to BigInteger[] to be passable to Decrypt function?

best regards,

rahman
 
Just guessing : Do you have to cut your string in blocks of the size you used when encrypting? Convert these blocks to BigInteger and then pass them to Decrypt() and concatenate the resulting strings again ?
An example (I use another encryption / decryption)
I have taken blocks of 4 characters.
==============================
The main
==============================
Code:
import java.math.BigInteger;

public class CaeserCipherTest {

  private int key = 3;

  public CaeserCipherTest() {
  }

  public static void main(String[] args) {
    CaeserCipherTest caeserCipherTest1 = new CaeserCipherTest();
    caeserCipherTest1.doIt();
  }

  public void doIt() {
    String plainText = "Hi everyone, I am passing an encrypted message from my client application to my server applicationxy";
    System.out.println("=== First method ===");
    System.out.println("This is the plaintext : " + plainText);
    String cipherText = CaesarCipher.encrypt(plainText, key);
    System.out.println("This is the cipherText : " + cipherText);
    String deCipherText = CaesarCipher.decrypt(cipherText, key);
    System.out.println("This is the deCipherText : " + deCipherText);
    System.out.println("=== Second method ===");
    System.out.println("This is the plaintext : " + plainText);
    cipherText = encrypt(plainText, key);
    System.out.println("This is the cipherText : " + cipherText);
    deCipherText = decrypt(cipherText, key);
    System.out.println("This is the deCipherText : " + deCipherText);
  }

  public String encrypt(String plainText, int key) {
    String cipherText = "";
    for (int i= 0; i< plainText.length(); i=i+4) {
      String sub = plainText.substring(i, i+4);
      System.out.println(&quot;Sub=&quot;+sub);
      String cipherSub = CaesarCipher.encrypt(sub, key);
      System.out.println(&quot;CipherSub=&quot;+cipherSub);
      cipherText = cipherText + cipherSub;
    }
    return cipherText;
  }


  public String decrypt(String cipherText, int key) {
    String plainText = &quot;&quot;;
    for (int i= 0; i< cipherText.length(); i=i+4) {
      String sub = cipherText.substring(i, i+4);
      System.out.println(&quot;Sub=&quot;+sub);
      // rsa.decrypt( new BigInteger(sub) ) ;
      String plainSub = CaesarCipher.decrypt(sub, key);
      System.out.println(&quot;PlainSub=&quot;+plainSub);
      plainText = plainText + plainSub;
    }
    return plainText;
  }

}
==============================
The encrypt / decrypt stuff
==============================
Code:
//package com.jaworski.security.handbook;
// Provide a static implementation of the Caesar cipher.
public class CaesarCipher {
 // Encrypt a String
 public static String encrypt(String s, int key) {
  String returnValue = &quot;&quot;;
  for(int i=0;i<s.length();++i) {
   char c = s.charAt(i);
   c = encrypt(c,key);
   returnValue += String.valueOf(c);
  }
  return returnValue;
 }

 // Decrypt a String
 public static String decrypt(String s, int key) {
  return encrypt(s,-key);
 }
 // Encrypt a single character
 public static char encrypt(char c, int key) {
  if(c >= 'a' && c <= 'z') c=Character.toUpperCase(c);
  if(c >= 'A' && c <= 'Z') c=rotate(c,key);
  return c;
 }
 // Decrypt a single character
 public static char decrypt(char c, int key) {
  if(c<'A' || c>'Z') return c;
  else return rotate(c,-key);
 }
 // Perform the alphabetic rotation
 private static char rotate(char c, int key) {
  key = key % 26;
  int n = (c - 'A' + key) % 26;
  if(n < 0) n += 26;
  return (char) (n + 'A');
 }
}

 
how about ...

String ciphertext = input.readLine();
BigInteger[] bi = new BigInteger[1];
bi[0] = new BigInteger(cipertext);
Decrypt(bi);
 
sound like a very good idea SEDJ
thanks a lot!!!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top