Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: Java
Hi,
 
I am doing project in concurrent Java. If anyone know RSA algorithm in concurrent Java or using threads.
Find my code for the sequential RSA algorithm. Can anyone change paralleize this code.
import java.security.KeyPairGenerator;
import java.security.KeyPair;
import java.security.PublicKey;
import java.security.PrivateKey;
import javax.crypto.Cipher;
public class rsa {
  private static byte[] encrypt(byte[] inpBytes, PublicKey key,
      String xform) throws Exception {
    Cipher cipher = Cipher.getInstance(xform);
    cipher.init(Cipher.ENCRYPT_MODE, key);
    return cipher.doFinal(inpBytes);
  }
  private static byte[] decrypt(byte[] inpBytes, PrivateKey key,
      String xform) throws Exception{
    Cipher cipher = Cipher.getInstance(xform);
    cipher.init(Cipher.DECRYPT_MODE, key);
    return cipher.doFinal(inpBytes);
  }
  public static void main(String[] unused) throws Exception {
   long starttime =System.currentTimeMillis();
   
   String xform = "RSA/NONE/PKCS1PADDING";
    // Generate a key-pair
    KeyPairGenerator kpg = KeyPairGenerator.getInstance("RSA");
    kpg.initialize(1024); // 1024 is the keysize.
    KeyPair kp = kpg.generateKeyPair();
    PublicKey pubk = kp.getPublic();
    PrivateKey prvk = kp.getPrivate();
    byte[] dataBytes =
        "J2EE Security for Servlets, EJBs and Web Services".getBytes();
    byte[] encBytes = encrypt(dataBytes, pubk, xform);
    System.out.println(encBytes);
    byte[] decBytes = decrypt(encBytes, prvk, xform);
    System.out.println(decBytes);
    String res=new String(decBytes);
    
    boolean expected = java.util.Arrays.equals(dataBytes, decBytes);
    System.out.println("Test " + (expected ? "SUCCEEDED!" : "FAILED!"));
    System.out.println(res);
    long endtime=System.currentTimeMillis();
System.out.println("Time required for sequential rsa algorithm:"+(endtime-starttime)+"millisecond");
  }
}
Posted 31-Mar-11 2:50am
Edited 31-Mar-11 3:03am
v2
Comments
Sandeep Mewara at 31-Mar-11 8:03am
   
What does: "Can anyone change paralleize this code" this mean?
Keith Barrow at 31-Mar-11 10:28am
   
There isn't any loop to parallelize. Even if there were, IIRC RSA needs to run sequentially to work.

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Dude, your code isn't doing the actual encoding. All you're doing is using the encryption classes built into the Java libraries. You're not implementing the algorithm at all!
 
Besides, RSA is a sequential algorithm. You cannot encode the next block of data without knowing the result of the previous block. That alone says you can't parallelize it.
 
About the only thing you can do is put each instance of your RSA class on a seperate thread so you can encrypt/decrypt different datasets at the same time.
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 6,569
1 Sergey Alexandrovich Kryukov 6,168
2 DamithSL 5,228
3 Manas Bhardwaj 4,717
4 Maciej Los 4,150


Advertise | Privacy | Mobile
Web04 | 2.8.1411022.1 | Last Updated 31 Mar 2011
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100