Results 1 to 9 of 9
  1. #1
    brrrr is offline Member
    Join Date
    Mar 2012
    Location
    Malaysia
    Posts
    18
    Rep Power
    0

    Unhappy Need help with RSA encryption / decryption in different classes

    I have a problem using RSA encryption / decryption in different classes. Both encryption and decryption methods works fine if I put them in the same class~ I have used inheritance but still no luck~ can anyone help me solve this problem?

    Both class have same library~

    public class encrypt { //1st class

    String cleartextFile = "cleartext.txt";
    String ciphertextFile = "ciphertextRSA.txt";

    static PrivateKey privKey;
    static PublicKey pubKey;

    static Cipher cipher;
    static byte[] block = new byte[32];
    static int i;


    public static void main(String[] args) throws Exception {

    Security.addProvider(new FlexiCoreProvider());

    KeyPairGenerator kpg = KeyPairGenerator.getInstance("RSA", "FlexiCore");
    cipher = Cipher.getInstance("RSA", "FlexiCore");

    kpg.initialize(1024);
    KeyPair keyPair = kpg.generateKeyPair();
    privKey = keyPair.getPrivate();
    pubKey = keyPair.getPublic();

    // Encrypt

    cipher.init(Cipher.ENCRYPT_MODE, pubKey);

    String cleartextFile = "cleartext.txt";
    String ciphertextFile = "ciphertextRSA.txt";

    FileInputStream fis = new FileInputStream(cleartextFile);
    FileOutputStream fos = new FileOutputStream(ciphertextFile);
    CipherOutputStream cos = new CipherOutputStream(fos, cipher);
    byte[] block = new byte[32];
    int i;
    while ((i = fis.read(block)) != -1) {
    cos.write(block, 0, i);
    }
    cos.close();

    new decrypt();
    }

    }

    public class decrypt extends encrypt{ //2nd class

    public void decrypto() throws Exception{

    Security.addProvider(new FlexiCoreProvider());

    //Cipher cipher = Cipher.getInstance("RSA", "FlexiCore");
    // Decrypt

    String ciphertextFile = "ciphertextRSA.txt";
    String cleartextAgainFile = "cleartextAgainRSA.txt";

    cipher.init(Cipher.DECRYPT_MODE, privKey);

    FileInputStream fis = new FileInputStream(ciphertextFile);
    CipherInputStream cis = new CipherInputStream(fis, cipher);
    FileOutputStream fos = new FileOutputStream(cleartextAgainFile);
    while ((i = cis.read(block)) != -1) {
    fos.write(block, 0, i);
    }
    fos.close();
    }

    }

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,396
    Rep Power
    25

    Default Re: Need help with RSA encryption / decryption in different classes

    encryption and decryption methods works fine if I put them in the same class
    Can you also post the code that is working.
    Please edit your post and wrap the code in code tags:
    BB Code List - Java Programming Forum
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,396
    Rep Power
    25

    Default Re: Need help with RSA encryption / decryption in different classes

    The code you posted does not compile without errors.
    If you don't understand my response, don't ignore it, ask a question.

  4. #4
    brrrr is offline Member
    Join Date
    Mar 2012
    Location
    Malaysia
    Posts
    18
    Rep Power
    0

    Default Re: Need help with RSA encryption / decryption in different classes

    im so sorry, my mistake. wrong code given.

    here is the right one, working without errors :

    Java Code:
    import java.io.FileInputStream;
    import java.io.FileOutputStream;
    import java.security.KeyPair;
    import java.security.KeyPairGenerator;
    import java.security.PrivateKey;
    import java.security.PublicKey;
    import java.security.Security;
    
    import javax.crypto.Cipher;
    import javax.crypto.CipherInputStream;
    import javax.crypto.CipherOutputStream;
    
    import de.flexiprovider.core.FlexiCoreProvider;
    
    public class ExampleRSA {
    
        public static void main(String[] args) throws Exception {
    
    	Security.addProvider(new FlexiCoreProvider());
    
    	KeyPairGenerator kpg = KeyPairGenerator.getInstance("RSA", "FlexiCore");
    	Cipher cipher = Cipher.getInstance("RSA", "FlexiCore");
    
    	kpg.initialize(1024);
    	KeyPair keyPair = kpg.generateKeyPair();
    	PrivateKey privKey = keyPair.getPrivate();
    	PublicKey pubKey = keyPair.getPublic();
    
    	// Encrypt
    
    	cipher.init(Cipher.ENCRYPT_MODE, pubKey);
    
    	String cleartextFile = "C:/Users/Azmie/Desktop/PSM/test.txt";
    	String ciphertextFile = "C:/Users/Azmie/Desktop/PSM/ciphertextRSA.txt";
    
    	FileInputStream fis = new FileInputStream(cleartextFile);
    	FileOutputStream fos = new FileOutputStream(ciphertextFile);
    	CipherOutputStream cos = new CipherOutputStream(fos, cipher);
    
    	byte[] block = new byte[32];
    	int i;
    	try { while((i = fis.read(block)) != -1) {
    	    cos.write(block, 0, i);
    	}
    	System.out.println("asd");
    	}
    	catch(ArrayIndexOutOfBoundsException e){
    			//printed just to inform that we have entered the catch block
    			System.out.println("Oops, we went to far, better go back to 0!");
    		}
    	cos.close();
    
    	// Decrypt
    
    	String cleartextAgainFile = "C:/Users/Azmie/Desktop/PSM/cleartextAgainRSA.txt";
    
    	cipher.init(Cipher.DECRYPT_MODE, privKey);
    
    	fis = new FileInputStream(ciphertextFile);
    	CipherInputStream cis = new CipherInputStream(fis, cipher);
    	fos = new FileOutputStream(cleartextAgainFile);
    
    	try { while((i = cis.read(block)) != -1) {
    	    fos.write(block, 0, i);
    	}
    	System.out.println("asd");
    	}
    	catch(ArrayIndexOutOfBoundsException e){
    			//printed just to inform that we have entered the catch block
    			System.out.println("Oops, we went to far, better go back to 0!");
    		}
    	fos.close();
        }
    
    }
    Last edited by brrrr; 04-25-2012 at 03:41 AM.

  5. #5
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,396
    Rep Power
    25

    Default Re: Need help with RSA encryption / decryption in different classes

    There is a missing package. Can you remove that from the code?
    If you don't understand my response, don't ignore it, ask a question.

  6. #6
    brrrr is offline Member
    Join Date
    Mar 2012
    Location
    Malaysia
    Posts
    18
    Rep Power
    0

    Default Re: Need help with RSA encryption / decryption in different classes

    you mean the flexicoreprovider?

    done.
    Java Code:
    import java.io.FileInputStream;
    import java.io.FileOutputStream;
    import java.security.KeyPair;
    import java.security.KeyPairGenerator;
    import java.security.PrivateKey;
    import java.security.PublicKey;
    import java.security.Security;
    
    import javax.crypto.Cipher;
    import javax.crypto.CipherInputStream;
    import javax.crypto.CipherOutputStream;
    
    //import de.flexiprovider.core.FlexiCoreProvider;
    
    class ExampleRSA {
    
        public static void main(String[] args) throws Exception {
    
    //	Security.addProvider(new FlexiCoreProvider());
    
    	KeyPairGenerator kpg = KeyPairGenerator.getInstance("RSA");
    	Cipher cipher = Cipher.getInstance("RSA");
    
    	kpg.initialize(1024);
    	KeyPair keyPair = kpg.generateKeyPair();
    	PrivateKey privKey = keyPair.getPrivate();
    	PublicKey pubKey = keyPair.getPublic();
    
    	// Encrypt
    
    	cipher.init(Cipher.ENCRYPT_MODE, pubKey);
    
    	String cleartextFile = "cleartext.txt";
    	String ciphertextFile = "ciphertextRSA.txt";
    
    	FileInputStream fis = new FileInputStream(cleartextFile);
    	FileOutputStream fos = new FileOutputStream(ciphertextFile);
    	CipherOutputStream cos = new CipherOutputStream(fos, cipher);
    
    	byte[] block = new byte[32];
    	int i;
    	while ((i = fis.read(block)) != -1) {
    	    cos.write(block, 0, i);
    	}
    	cos.close();
    
    	// Decrypt
    
    	String cleartextAgainFile = "cleartextAgainRSA.txt";
    
    	cipher.init(Cipher.DECRYPT_MODE, privKey);
    
    	fis = new FileInputStream(ciphertextFile);
    	CipherInputStream cis = new CipherInputStream(fis, cipher);
    	fos = new FileOutputStream(cleartextAgainFile);
    
    	while ((i = cis.read(block)) != -1) {
    	    fos.write(block, 0, i);
    	}
    	fos.close();
        }
    
    }
    Last edited by brrrr; 04-25-2012 at 03:46 AM.

  7. #7
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,396
    Rep Power
    25

    Default Re: Need help with RSA encryption / decryption in different classes

    A suggestion for testing: Use ByteArray streams so you can keep everything used by the program in one place and that can be examined by the program and not have to use and look at disk files.

    That's it for tonight. See you tomorrow.
    Last edited by Norm; 04-25-2012 at 03:55 AM.
    If you don't understand my response, don't ignore it, ask a question.

  8. #8
    brrrr is offline Member
    Join Date
    Mar 2012
    Location
    Malaysia
    Posts
    18
    Rep Power
    0

    Default Re: Need help with RSA encryption / decryption in different classes

    okay, I'll try using ByteArray. thank you for your suggestion

  9. #9
    brrrr is offline Member
    Join Date
    Mar 2012
    Location
    Malaysia
    Posts
    18
    Rep Power
    0

    Default Re: Need help with RSA encryption / decryption in different classes

    I have solved the problem, after removed the import de.flexiprovider.core.FlexiCoreProvider; everything seems to be working perfectly. Thank you very much

Similar Threads

  1. Encryption & Decryption
    By raj.mscking@gmail.com in forum New To Java
    Replies: 1
    Last Post: 02-01-2012, 02:21 PM
  2. Encryption and Decryption
    By jatinkansagara in forum Advanced Java
    Replies: 7
    Last Post: 06-27-2011, 03:08 PM
  3. PGP Encryption&Decryption
    By Deepa in forum New To Java
    Replies: 2
    Last Post: 07-07-2009, 06:22 AM
  4. Encryption/Decryption
    By Echilon in forum New To Java
    Replies: 2
    Last Post: 03-24-2009, 11:58 AM
  5. Encryption/Decryption Through AOP
    By SirRawlins in forum Advanced Java
    Replies: 0
    Last Post: 12-19-2007, 03:22 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •