Skip to Main Content

Java Security

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

RSA ENCRYPT / DECRYPT FILES

911074Jan 14 2012 — edited Jan 16 2012
Hello everyone ..
I'm new to the forum and to the java.security programming..

I have a problem that I can't fix:

I'm trying to make code for encrypt / decrypt files with RSA algorithm, using small keys.

I have an example working fine with a string.

In this exemple, with the minimum modulus size 512, input data must not be longer than 53 bytes.

So, to use it with files you need to split the file into many subarrays with maximum size of 53 bytes.

I realized another example in which to apply the encryption in parts and then try to decipher by following the same pattern.

Unfortunately in the process of decryption I get an error that I cannot understand.

Has anybody any idea?

Below the error stack and the two examples..

Thanks to all

Exception in thread "main" javax.crypto.BadPaddingException: Data must start with zero
at sun.security.rsa.RSAPadding.unpadV15(RSAPadding.java:325)
at sun.security.rsa.RSAPadding.unpad(RSAPadding.java:272)
at com.sun.crypto.provider.RSACipher.doFinal(RSACipher.java:357)
at com.sun.crypto.provider.RSACipher.engineDoFinal(RSACipher.java:383)
at javax.crypto.Cipher.doFinal(Cipher.java:1813)
at algoritmi.ProvaKey.main(RSA_FILE.java:94)

WORKING EXAMPLE (INPUT IS A STRING):

import java.security.*;

import javax.crypto.*;

public class RSA_Example{

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

KeyPairGenerator kpg=null;
kpg = KeyPairGenerator.getInstance("RSA");
kpg.initialize(512);
KeyPair kp = kpg.generateKeyPair();
PublicKey publickey = kp.getPublic();
PrivateKey privatekey = kp.getPrivate();

String s=new String("Input string!");
byte[] b=s.getBytes();

//ENCRYPT
Cipher c=null;
byte[] encodeFile=null;

c = Cipher.getInstance("RSA/ECB/PKCS1Padding");
c.init(Cipher.ENCRYPT_MODE, publickey);

encodeFile = c.doFinal(b);

//DECRYPT
Cipher cc=null;
byte[] plainFile=null;

cc = Cipher.getInstance("RSA/ECB/PKCS1Padding");
cc.init(Cipher.DECRYPT_MODE, privatekey);

plainFile = cc.doFinal(encodeFile);

String dec=new String(plainFile);
System.out.println("Output: "+dec);
}
}



NON WORKING EXAMPLE (INPUT IS A FILE):

public class RSA_FILE{

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

KeyPairGenerator kpg=null;
kpg = KeyPairGenerator.getInstance("RSA");
kpg.initialize(512);
KeyPair kp = kpg.generateKeyPair();
PublicKey publickey = kp.getPublic();
PrivateKey privatekey = kp.getPrivate();


String path = "/home/user/example.bmp";
byte[] file = File2Byte(path);
byte[] FILE_CIF = new byte[file.length];
byte[] FILE_DEC = new byte[file.length];

//ENCRYPT
Cipher c=null;
byte[] encodedChunk=null;

c = Cipher.getInstance("RSA/ECB/PKCS1Padding");
c.init(Cipher.ENCRYPT_MODE, publickey);


int length = file.length;
int mod = file.length%53;
int offset = 0;
byte[] chunk = new byte[53];
byte[] lastChunk = new byte[mod];

int cycles = (length - mod)/53;

for(int i = 0; i<cycles; i++)
{
System.arraycopy(file, offset, chunk, 0, 53);
encodedChunk = c.doFinal(chunk);
System.arraycopy(encodedChunk, 0, FILE_CIF, offset, 53);
offset = offset+53;
}


System.arraycopy(file, file.length-mod, lastChunk, 0, mod);
encodedChunk = c.doFinal(lastChunk);
System.arraycopy(encodedChunk, 0, FILE_CIF, file.length-mod, mod);
Byte2File("/home/user/exampleENC.bmp", FILE_CIF);

//DECRYPT
Cipher cc=null;
byte[] plainChunk=null;

length = FILE_CIF.length;
mod = file.length%53;
offset = 0;
chunk = new byte[53];
lastChunk = new byte[mod];

cycles = (length - mod)/53;

cc = Cipher.getInstance("RSA/ECB/PKCS1Padding");
cc.init(Cipher.DECRYPT_MODE, privatekey);

offset =0;

for(int i = 0; i<cycles; i++)
{
System.arraycopy(FILE_CIF, offset, chunk, 0, 53);
plainChunk = cc.doFinal(chunk); // ERROR IS HERE !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! <-----
System.arraycopy(plainChunk, 0, FILE_DEC, offset, 53);
offset = offset+53;
}

System.arraycopy(FILE_CIF, FILE_CIF.length-mod, lastChunk, 0, mod);
plainChunk = cc.doFinal(lastChunk);
System.arraycopy(plainChunk, 0, FILE_DEC, FILE_DEC.length-mod, mod);
Byte2File("/home/user/exampleDEC.bmp",FILE_DEC);
}

public static byte[] File2Byte(String path)
{
//create file object
File file = new File(path);
byte fileContent[] = new byte[(int)file.length()];

try
{
FileInputStream fin = new FileInputStream(file);
fin.read(fileContent);
}

catch(FileNotFoundException e) { System.out.println("File not found" + e);}
catch(IOException ioe) { System.out.println("Exception while reading the file " + ioe);}

return fileContent;
}

public static void Byte2File(String path, byte[] content)
{
try
{
FileOutputStream fos = new FileOutputStream(path);
fos.write(content);
fos.close();
}
catch(FileNotFoundException ex)
{
System.out.println("FileNotFoundException : " + ex);
}
catch(IOException ioe)
{
System.out.println("IOException : " + ioe);
}
}
}

Edited by: 908071 on 14-gen-2012 0.30

Edited by: 908071 on Jan 14, 2012 9:33 AM
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Feb 13 2012
Added on Jan 14 2012
6 comments
3,300 views