Results 1 to 9 of 9
Thread: Problem with rsa encryption
- 05-10-2012, 12:22 PM #1
Member
- Join Date
- Mar 2012
- Location
- Malaysia
- Posts
- 18
- Rep Power
- 0
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.
- 05-11-2012, 06:27 PM #2
Member
- Join Date
- Mar 2012
- Location
- Malaysia
- Posts
- 18
- Rep Power
- 0
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?
- 05-11-2012, 07:25 PM #3
Re: Problem with rsa encryption
Don't double post. I've merged the two threads.
Go through Forum Rules -- especially the second paragraph.
dbWhy do they call it rush hour when nothing moves? - Robin Williams
- 05-11-2012, 08:24 PM #4
Member
- Join Date
- Mar 2012
- Location
- Malaysia
- Posts
- 18
- Rep Power
- 0
Re: Problem with rsa encryption
ok, my bad, sorry
- 05-12-2012, 01:52 AM #5
Member
- Join Date
- Mar 2012
- Location
- Malaysia
- Posts
- 18
- Rep Power
- 0
Re: Problem with rsa encryption
can anyone give me some help, maybe some ideas please..
- 05-12-2012, 04:44 AM #6
Re: Problem with rsa encryption
can you show us your code?
- 05-12-2012, 09:57 PM #7
Member
- Join Date
- Mar 2012
- Location
- Malaysia
- Posts
- 18
- Rep Power
- 0
Re: Problem with rsa encryption
//this is the key generator code :
//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 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"); } }
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); } }
- 05-12-2012, 10:00 PM #8
Member
- Join Date
- Mar 2012
- Location
- Malaysia
- Posts
- 18
- Rep Power
- 0
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?
- 05-13-2012, 01:39 AM #9
Member
- Join Date
- Mar 2012
- Location
- Malaysia
- Posts
- 18
- Rep Power
- 0
Similar Threads
-
Encryption Problem, Can someone do this for me?
By funstein in forum New To JavaReplies: 50Last Post: 08-18-2011, 02:34 PM -
problem with encryption in steganography
By stegano in forum New To JavaReplies: 2Last Post: 02-26-2011, 01:10 PM -
Java encryption problem
By lqu in forum Advanced JavaReplies: 0Last Post: 01-24-2011, 04:30 AM -
WebService encryption problem
By falnik in forum Advanced JavaReplies: 3Last Post: 07-26-2009, 04:21 AM -
Encryption-Decryption Problem
By dinesh.1.sharma in forum Advanced JavaReplies: 1Last Post: 07-17-2008, 10:03 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks