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

    Talking Problem with rsa encryption

    Hello, i need help regarding rsa encryption. I've tried to encrypt file .txt, the file was successfully encrypted and decrypted but it can only encrypt about 2 lines inside the .txt file when using rsa key size 1024-bit. To encrypt more lines, I have to use larger key size (for example 2048, 3072 ,4096-bit) but this will slow down the process to generate the rsa keys.. Is there any other ways to encrypt files .txt with more lines in it? If possible, i want to encrypt more than 20 lines. Help needed please.

    im using block cipher 64-bit, auto generate asymmetric keys.

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

    Unhappy Problem with rsa encryption

    Hello, i need help regarding rsa encryption. I've tried to encrypt file .txt, the file was successfully encrypted and decrypted but it can only encrypt about 2 lines of words inside the .txt file when using rsa key size 1024-bit. If there are words more than two lines inside the text file, encryption will produce empty encrypted file, which automatically produce empty decrypted file.

    What seems to be the problem? can anyone explain this to me?
    Is there any way to encrypt .txt files without need to worry how much words are inside the .txt file itself?

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,457
    Rep Power
    20

    Default Re: Problem with rsa encryption

    Don't double post. I've merged the two threads.

    Go through Forum Rules -- especially the second paragraph.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

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

    Default Re: Problem with rsa encryption

    ok, my bad, sorry

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

    Default Re: Problem with rsa encryption

    can anyone give me some help, maybe some ideas please..

  6. #6
    chipp's Avatar
    chipp is offline Member
    Join Date
    Jul 2011
    Posts
    62
    Rep Power
    0

    Default Re: Problem with rsa encryption

    can you show us your code?

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

    Default Re: Problem with rsa encryption

    //this is the key generator code :

    Java Code:
    import javax.swing.*;
    import javax.swing.event.*;
    import java.awt.*;
    import java.awt.event.*;
    
    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 java.io.*;
    import java.security.*;
    import java.security.spec.*;
    
    public class KeyGenerator{
    
    PublicKey publicKey;
    PrivateKey privateKey;
    
        public void generate(String path) throws Exception {
    
    			KeyPairGenerator kpg = KeyPairGenerator.getInstance("RSA");
    			kpg.initialize(5120);
    			KeyPair kp = kpg.generateKeyPair();
    			System.out.println("Generated Key Pair");
    			SaveKeyPair(path, kp);
    
        }
    
    	private String getHexString(byte[] b) {
    	String result = "";
    	for (int i = 0; i < b.length; i++) {
    	result += Integer.toString((b[i] & 0xff) + 0x100, 16).substring(1);
    	}
    	return result;
    	}
    
    	public void SaveKeyPair(String path, KeyPair keyPair) throws IOException {
    
    	privateKey = keyPair.getPrivate();
    	publicKey = keyPair.getPublic();
    
    	// Store Public Key.
    	X509EncodedKeySpec x509EncodedKeySpec = new X509EncodedKeySpec(
    	publicKey.getEncoded());
    	FileOutputStream fos = new FileOutputStream(path + "/public.key");
    	fos.write(x509EncodedKeySpec.getEncoded());
    	fos.close();
    	System.out.println("Public Key: " + getHexString(publicKey.getEncoded()));
    
    	// Store Private Key.
    	PKCS8EncodedKeySpec pkcs8EncodedKeySpec = new PKCS8EncodedKeySpec(
    	privateKey.getEncoded());
    	fos = new FileOutputStream(path + "/private.key");
    	fos.write(pkcs8EncodedKeySpec.getEncoded());
    	fos.close();
    	System.out.println("Private Key: " + getHexString(privateKey.getEncoded()));
    
    	System.out.println("Key generated successfully");
    	JOptionPane.showMessageDialog(null, "Key generated successfully");
    	}
    
    }
    //this is the encryption code :

    Java Code:
    import javax.swing.*;
    import javax.swing.event.*;
    import java.awt.*;
    import java.awt.event.*;
    
    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 java.io.*;
    import java.security.*;
    import java.security.spec.*;
    
    public class EncryptPage extends KeyGenerator{
    
    byte[] block = new byte[64];
    int i;
    PublicKey publicKey;
    PrivateKey privateKey;
    
        public void Encryption(String a, String b, File path) throws Exception {
    
    			Cipher cipher = Cipher.getInstance("RSA");
    
    			// Encrypt
    
    			String plaintextFile = a;
    			String ciphertextFile = b;
    
    			KeyPair loadedKeyPair = LoadKeyPair(path, "RSA");
    			System.out.println("Loaded Key Pair");
    
    			cipher.init(Cipher.ENCRYPT_MODE, publicKey);
    
    			FileInputStream fis = new FileInputStream(plaintextFile);
    			FileOutputStream fos = new FileOutputStream(ciphertextFile);
    			CipherOutputStream cos = new CipherOutputStream(fos, cipher);
    			try{
    			while ((i = fis.read(block)) != -1) {
    			    cos.write(block, 0, i);
    			}
    			cos.close();
    		   	System.out.println("File encryption successful");
    		   	JOptionPane.showMessageDialog(null, "File encryption successful");
    			}
    			catch(Exception exception){
    
    				JOptionPane.showMessageDialog(null, "Error encrypting file");
    			}
    
        }
        public KeyPair LoadKeyPair(File path, String algorithm)throws IOException,NoSuchAlgorithmException,InvalidKeySpecException {
    	// Read Public Key.
    	File filePublicKey = path;
    	FileInputStream fis = new FileInputStream(path);
    	byte[] encodedPublicKey = new byte[(int) filePublicKey.length()];
    	fis.read(encodedPublicKey);
    	fis.close();
    
    	/*// Read Private Key.
    	File filePrivateKey = new File(path + "/private.key");
    	fis = new FileInputStream(path + "/private.key");
    	byte[] encodedPrivateKey = new byte[(int) filePrivateKey.length()];
    	fis.read(encodedPrivateKey);
    	fis.close();*/
    
    	// Generate KeyPair.
    	KeyFactory keyFactory = KeyFactory.getInstance(algorithm);
    	X509EncodedKeySpec publicKeySpec = new X509EncodedKeySpec(
    	encodedPublicKey);
    	publicKey = keyFactory.generatePublic(publicKeySpec);
    
    	/*PKCS8EncodedKeySpec privateKeySpec = new PKCS8EncodedKeySpec(
    	encodedPrivateKey);
    	privateKey = keyFactory.generatePrivate(privateKeySpec);*/
    
    	return new KeyPair(publicKey, privateKey);
    	}
    
    }

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

    Default Re: Problem with rsa encryption

    the initial key size was 1024 bit, but i have increased the size, so that encryption of file length longer than key length possible. But it causes the key generating process gets longer. Is there any other methods not involving increasing key size?

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

    Default Re: Problem with rsa encryption

    i've tried to compress data using deflater to reduce the file size then encrypt, after that decrypt then decompress using inflater. It works but not very efficient. If possible I wish not to use deflater and inflater.

Similar Threads

  1. Encryption Problem, Can someone do this for me?
    By funstein in forum New To Java
    Replies: 50
    Last Post: 08-18-2011, 03:34 PM
  2. problem with encryption in steganography
    By stegano in forum New To Java
    Replies: 2
    Last Post: 02-26-2011, 02:10 PM
  3. Java encryption problem
    By lqu in forum Advanced Java
    Replies: 0
    Last Post: 01-24-2011, 05:30 AM
  4. WebService encryption problem
    By falnik in forum Advanced Java
    Replies: 3
    Last Post: 07-26-2009, 05:21 AM
  5. Encryption-Decryption Problem
    By dinesh.1.sharma in forum Advanced Java
    Replies: 1
    Last Post: 07-17-2008, 11:03 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
  •