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.
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?
Re: Problem with rsa encryption
Don't double post. I've merged the two threads.
Go through Forum Rules -- especially the second paragraph.
db
Re: Problem with rsa encryption
Re: Problem with rsa encryption
can anyone give me some help, maybe some ideas please..
Re: Problem with rsa encryption
can you show us your code?
Re: Problem with rsa encryption
//this is the key generator code :
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 :
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);
}
}
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?
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.