Decryption problem (continue from "problem with encryption")
Hello again.I am posting on a new topic because i could not change the name of the previous topic i posted. I cannot decrypt an encrypted message which i load from a file.
File encryptf =new File("encrypt.txt");
int enclen = (int)encryptf.length();
DataInputStream dis2 =new DataInputStream(new FileInputStream(encryptf));
byte[] encrBytes =newbyte[enclen];
dis2.readFully(encrBytes);
dis2.close();
// get an RSA cipher object and print the provider
Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");
// decrypt the ciphertext using the private key
System.out.println("\nStart decryption" );
cipher.init(Cipher.DECRYPT_MODE, privateKey);
byte[] newPlainText = cipher.doFinal(encrBytes);
System.out.println("Finish decryption: " );
System.out.println(new String(newPlainText,"UTF8") );
The error i receive is avax.crypto.BadPaddingException: unknown block type
at org.bouncycastle.jce.provider.JCERSACipher.engineDoFinal(Unknown Source)
at javax.crypto.Cipher.doFinal(DashoA6275)
at receiverpak.receiver.main(receiver.java:94)
What is wrong with the padding?In both encryption and decryption i use PKCS1 padding.I tried to put Nopadding and i didnt receive any error but the plaintext was not decrypted
# 1
You need to post the whole of your code because we can't get a coherent view of what you are doing. You should present the code as a stand alone application that we can work with.
# 2
The encryption and decryption need to be done in two separate applications.
The code for the encryption is
byte[] plainText = "This is a message".getBytes("UTF8");
FileOutputStream plainfile = new FileOutputStream("plaintext.txt");
plainfile.write(plainText);
plainfile.close();
File f = new File("public.dat");
int len = (int)f.length();
DataInputStream dis = new DataInputStream(new FileInputStream(f));
byte[] pubKeyBytes = new byte[len];
dis.readFully(pubKeyBytes);
dis.close();
EncodedKeySpec publicKeySpec = new X509EncodedKeySpec(pubKeyBytes);
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
Key publicKey = keyFactory.generatePublic(publicKeySpec);
// get an RSA cipher object and print the provider
Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");
// encrypt the plaintext using the public key
System.out.println( "\nStart encryption" );
cipher.init(Cipher.ENCRYPT_MODE, publicKey);
byte[] cipherText = cipher.doFinal(plainText);
FileOutputStream encrfile = new FileOutputStream("encrypt.txt");
encrfile.write(cipherText);
encrfile.close();
The code for the decryption is
File privatef = new File("private.dat");
int len = (int)privatef.length();
DataInputStream dis = new DataInputStream(new FileInputStream(privatef));
byte[] prKeyBytes = new byte[len];
dis.readFully(prKeyBytes);
dis.close();
PKCS8EncodedKeySpec priKeySpec = new PKCS8EncodedKeySpec(prKeyBytes);
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
Key privateKey = keyFactory.generatePrivate(priKeySpec);
File encryptf = new File("encrypt.txt");
int enclen = (int)encryptf.length();
DataInputStream dis2 = new DataInputStream(new FileInputStream(encryptf));
byte[] encrBytes = new byte[enclen];
dis2.readFully(encrBytes);
dis2.close();
// get an RSA cipher object and print the provider
Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");
// decrypt the ciphertext using the private key
System.out.println( "\nStart decryption" );
cipher.init(Cipher.DECRYPT_MODE, privateKey);
byte[] newPlainText = cipher.doFinal(encrBytes);
System.out.println( "Finish decryption: " );
System.out.println( new String(newPlainText, "UTF8") );
Message was edited by:
flightcaptain
# 3
Your encryption and decryption code works for me using my own key generation so it must be a problem with your key generation.As I said in my last post - publish a stand alone application that does everything without the need for us to create our own test harness.
# 4
Thank you for your reply Sabre.Here is the code.
public class receiver {
public receiver() {
try {
jbInit();
}
catch (Exception ex) {
ex.printStackTrace();
}
}
public static void main (String[] args) throws Exception {
System.out.println( "\nStart generating RSA key" );
KeyPairGenerator keyGen = KeyPairGenerator.getInstance("RSA");
keyGen.initialize(1024);
KeyPair key = keyGen.generateKeyPair();
PrivateKey priv = key.getPrivate();
PublicKey pub = key.getPublic();
System.out.println( "Finish generating RSA key" );
FileOutputStream pukey = new FileOutputStream("public.dat");
pukey.write(pub.getEncoded());
pukey.close();
FileOutputStream prkey = new FileOutputStream("private.dat");
prkey.write(priv.getEncoded());
prkey.close();
Generate the Plaintext to be encryptred
[code] byte[] plainText = "This is a message".getBytes("UTF8");
FileOutputStream plainfile = new FileOutputStream("plaintext.txt");
plainfile.write(plainText);
plainfile.close();
File f = new File("public.dat");
int len = (int)f.length();
DataInputStream dis = new DataInputStream(new FileInputStream(f));
byte[] pubKeyBytes = new byte[len];
dis.readFully(pubKeyBytes);
dis.close();
EncodedKeySpec publicKeySpec = new X509EncodedKeySpec(pubKeyBytes);
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
Key publicKey = keyFactory.generatePublic(publicKeySpec);
// get an RSA cipher object and print the provider
Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");
System.out.println( "\n" + cipher.getProvider().getInfo() );
// encrypt the plaintext using the public key
System.out.println( "\nStart encryption" );
cipher.init(Cipher.ENCRYPT_MODE, publicKey);
byte[] cipherText = cipher.doFinal(plainText);
FileOutputStream encrfile = new FileOutputStream("encrypt.txt");
encrfile.write(cipherText);
encrfile.close();
File privatef = new File("private.dat");
int len = (int)privatef.length();
DataInputStream dis = new DataInputStream(new FileInputStream(privatef));
byte[] prKeyBytes = new byte[len];
dis.readFully(prKeyBytes);
dis.close();
PKCS8EncodedKeySpec priKeySpec = new PKCS8EncodedKeySpec(prKeyBytes);
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
Key privateKey = keyFactory.generatePrivate(priKeySpec);
File encryptf = new File("encrypt.txt");
int enclen = (int)encryptf.length();
FileInputStream fis = new FileInputStream(encryptf);
byte[] encr = new byte[fis.available()];
fis.read(encr);
fis.close();
// get an RSA cipher object and print the provider
Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");
System.out.println( "\n" + cipher.getProvider().getInfo() );
// decrypt the ciphertext using the private key
System.out.println( "\nStart decryption" );
cipher.init(Cipher.DECRYPT_MODE, privateKey );
byte[] newPlainText = cipher.doFinal(encr);
System.out.print(newPlainText);
System.out.println( "Finish decryption: " );
System.out.println(new String(newPlainText, "UTF8"));
}
private void jbInit() throws Exception {
}
}
[/code]
Message was edited by:
flightcaptain
# 5
<deleted>Message was edited by: sabre150
# 6
Sorry Sabre.I will post it write now
# 7
import java.io.*;
import java.security.*;
import java.security.spec.*;
import javax.crypto.*;
import javax.crypto.spec.*;
import org.bouncycastle.jce.spec.ECKeySpec;
import com.sun.crypto.provider.JceKeyStore;
public class cryptography {
public cryptography() {
try {
jbInit();
}
catch (Exception ex) {
ex.printStackTrace();
}
}
public static void main (String[] args) throws Exception {
// generate an RSA key
System.out.println( "\nStart generating RSA key" );
KeyPairGenerator keyGen = KeyPairGenerator.getInstance("RSA");
keyGen.initialize(1024);
KeyPair key = keyGen.generateKeyPair();
PrivateKey priv = key.getPrivate();
PublicKey pub = key.getPublic();
System.out.println( "Finish generating RSA key" );
FileOutputStream pukey = new FileOutputStream("public.dat");
pukey.write(pub.getEncoded());
pukey.close();
FileOutputStream prkey = new FileOutputStream("private.dat");
prkey.write(priv.getEncoded());
prkey.close();
byte[] plainText = "This is a message".getBytes("UTF8");
FileOutputStream plainfile = new FileOutputStream("plaintext.txt");
plainfile.write(plainText);
plainfile.close();
File f = new File("public.dat");
int len = (int)f.length();
DataInputStream dis = new DataInputStream(new FileInputStream(f));
byte[] pubKeyBytes = new byte[len];
dis.readFully(pubKeyBytes);
dis.close();
EncodedKeySpec publicKeySpec = new X509EncodedKeySpec(pubKeyBytes);
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
Key publicKey = keyFactory.generatePublic(publicKeySpec);
// get an RSA cipher object and print the provider
Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");
System.out.println( "\n" + cipher.getProvider().getInfo() );
// encrypt the plaintext using the public key
System.out.println( "\nStart encryption" );
cipher.init(Cipher.ENCRYPT_MODE, publicKey);
byte[] cipherText = cipher.doFinal(plainText);
FileOutputStream encrfile = new FileOutputStream("encrypt.txt");
encrfile.write(cipherText);
encrfile.close();
File privatef = new File("private.dat");
int len = (int)privatef.length();
DataInputStream dis = new DataInputStream(new FileInputStream(privatef));
byte[] prKeyBytes = new byte[len];
dis.readFully(prKeyBytes);
dis.close();
PKCS8EncodedKeySpec priKeySpec = new PKCS8EncodedKeySpec(prKeyBytes);
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
Key privateKey = keyFactory.generatePrivate(priKeySpec);
File encryptf = new File("encrypt.txt");
int enclen = (int)encryptf.length();
FileInputStream fis = new FileInputStream(encryptf);
byte[] encr = new byte[fis.available()];
fis.read(encr);
fis.close();
// get an RSA cipher object and print the provider
Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");
System.out.println( "\n" + cipher.getProvider().getInfo() );
// decrypt the ciphertext using the private key
System.out.println( "\nStart decryption" );
cipher.init(Cipher.DECRYPT_MODE, priv);
byte[] newPlainText = cipher.doFinal(encr);
System.out.print(newPlainText);
System.out.println( "Finish decryption: " );
System.out.println(new String(newPlainText, "UTF8"));
}
private void jbInit() throws Exception {
}
}
# 8
It doesn't even compile!
# 9
That is because the code i posted here is in two separate applications.
Application 1
import java.io.*;
import java.security.*;
import java.security.spec.*;
import javax.crypto.*;
import javax.crypto.spec.*;
import org.bouncycastle.jce.spec.ECKeySpec;
import com.sun.crypto.provider.JceKeyStore;
public class receiver {
public receiver() {
try {
jbInit();
}
catch (Exception ex) {
ex.printStackTrace();
}
}
public static void main (String[] args) throws Exception {
// generate an RSA key
System.out.println( "\nStart generating RSA key" );
KeyPairGenerator keyGen = KeyPairGenerator.getInstance("RSA");
keyGen.initialize(1024);
KeyPair key = keyGen.generateKeyPair();
PrivateKey priv = key.getPrivate();
PublicKey pub = key.getPublic();
System.out.println( "Finish generating RSA key" );
FileOutputStream pukey = new FileOutputStream("public.dat");
pukey.write(pub.getEncoded());
pukey.close();
FileOutputStream prkey = new FileOutputStream("private.dat");
prkey.write(priv.getEncoded());
prkey.close();
File privatef = new File("private.dat");
int len = (int)privatef.length();
DataInputStream dis = new DataInputStream(new FileInputStream(privatef));
byte[] prKeyBytes = new byte[len];
dis.readFully(prKeyBytes);
dis.close();
PKCS8EncodedKeySpec priKeySpec = new PKCS8EncodedKeySpec(prKeyBytes);
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
Key privateKey = keyFactory.generatePrivate(priKeySpec);
File encryptf = new File("encrypt.txt");
int enclen = (int)encryptf.length();
FileInputStream fis = new FileInputStream(encryptf);
byte[] encr = new byte[fis.available()];
fis.read(encr);
fis.close();
// get an RSA cipher object and print the provider
Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");
System.out.println( "\n" + cipher.getProvider().getInfo() );
// decrypt the ciphertext using the private key
System.out.println( "\nStart decryption" );
cipher.init(Cipher.DECRYPT_MODE, priv);
byte[] newPlainText = cipher.doFinal(encr);
System.out.print(newPlainText);
System.out.println( "Finish decryption: " );
System.out.println(new String(newPlainText, "UTF8"));
}
private void jbInit() throws Exception {
}
}
Application2
import java.io.*;
import java.security.*;
import java.security.spec.*;
import javax.crypto.*;
import javax.crypto.spec.*;
public class sender {
public sender() {
try {
jbInit();
}
catch (Exception ex) {
ex.printStackTrace();
}
}
public static void main (String[] args) throws Exception {
byte[] plainText = "This is a message".getBytes("UTF8");
FileOutputStream plainfile = new FileOutputStream("plaintext.txt");
plainfile.write(plainText);
plainfile.close();
File f = new File("public.dat");
int len = (int)f.length();
DataInputStream dis = new DataInputStream(new FileInputStream(f));
byte[] pubKeyBytes = new byte[len];
dis.readFully(pubKeyBytes);
dis.close();
EncodedKeySpec publicKeySpec = new X509EncodedKeySpec(pubKeyBytes);
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
Key publicKey = keyFactory.generatePublic(publicKeySpec);
// get an RSA cipher object and print the provider
Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");
System.out.println( "\n" + cipher.getProvider().getInfo() );
// encrypt the plaintext using the public key
System.out.println( "\nStart encryption" );
cipher.init(Cipher.ENCRYPT_MODE, publicKey);
byte[] cipherText = cipher.doFinal(plainText);
FileOutputStream encrfile = new FileOutputStream("encrypt.txt");
encrfile.write(cipherText);
encrfile.close();
System.out.println( "Finish encryption: " );
}
private void jbInit() throws Exception {
}
}
# 10
Is it compiling now Sabre?
# 11
> Is it compiling now Sabre?You encrypt the file using an existing public key then you try to decrypt the file using the private key of a newly generated public and private key!Come on - use a few of your brain cells!
# 12
<deleted>Message was edited by: sabre150
# 13
I solve the problem Sabre.You see i am not experienced in Java and in programming in general and my brain cells deny to help me for these issues. Thank you again for your support and help.Now i have to work for the exchange of the files between the two applications.