Skip to Main Content

Java Programming

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!

IllegalBlockSizeException: Data must not be longer than 64 bytes

807606Apr 15 2007 — edited Apr 15 2007
hi guys

Am trying to decrypt a byte array and am getting the following error message. i have tried adding a loop that encryptes 64 bytes at a time but i then get a message about it not begging with a zero!




Exception: javax.crypto.IllegalBlockSizeException: Data must not be longer than 64 bytes
javax.crypto.IllegalBlockSizeException: Data must not be longer than 64 bytes
at com.sun.crypto.provider.RSACipher.a(DashoA12275)
at com.sun.crypto.provider.RSACipher.engineDoFinal(DashoA12275)
at javax.crypto.Cipher.doFinal(DashoA12275)
at Security.decrypt(Security.java:178)
at Security.byteConvert(Security.java:123)
at Security.main(Security.java:305)
BUILD SUCCESSFUL (total time: 6 seconds)


this is my code, any help would be great!

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.PrintStream;
import java.security.interfaces.RSAPrivateKey;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.RSAPrivateKeySpec;
import javax.crypto.spec.SecretKeySpec;
import javax.imageio.stream.FileImageInputStream;
import java.security.*;
import java.io.*;
import javax.crypto.*;
import sun.misc.*;
import java.lang.*;
 
 
public class Security{
 
public static void imageConvert( ) throws Exception {
try{
System.out.println("please select the file to encrypt");
BufferedReader fileInput = new BufferedReader(new InputStreamReader(System.in));
String filetoread = null;
filetoread = fileInput.readLine();
 
System.out.println("please select the file to save to");
BufferedReader fileOutput = new BufferedReader(new InputStreamReader(System.in));
String filetostore = null;
filetostore = fileOutput.readLine();
 
    
    
FileImageInputStream input = new FileImageInputStream(new File(filetoread));
ByteArrayOutputStream output = new ByteArrayOutputStream();
PrintStream stream = new PrintStream(output);
byte[] buffer = new byte[1024];
byte[] data = null;
 
 
 
int numberBytesRead = 0;
while ((numberBytesRead = input.read(buffer)) != -1)
{
output.write(buffer, 0, numberBytesRead);
}
 
data = output.toByteArray();
 
 
System.out.println("encrytped" + buffer);
System.out.println("going to encrypt");
byte[] imageOutput = encrypt(data);
System.out.println("back to imageconvert");
System.out.println("encrytped" + buffer);
 
 
 
 
//FileOutputStream imageOut = new FileOutputStream(new File(filetostore));
//imageOut.write(imageOutput,0,imageOutput.length);
//imageOut.close();
 
BASE64Encoder encoder = new BASE64Encoder();
String enc = encoder.encodeBuffer(imageOutput);
FileWriter fw = new FileWriter(filetostore);
fw.write(enc);
fw.close();
 
 
 
 
 
 
System.out.println("sent to file");
output.close();
input.close();
 
} catch(Exception ex) {
System.out.println("Exception: " + ex);
ex.printStackTrace();
}
}
 
 
public static void byteConvert()throws Exception{
    try{
 
System.out.println("please select the file to decrypt");
BufferedReader fileInput = new BufferedReader(new InputStreamReader(System.in));
String filetoread = null;
filetoread = fileInput.readLine();
 
FileInputStream fis = new FileInputStream(filetoread);
byte[] decrypt = new byte[fis.available()];
fis.read(decrypt);
fis.close();
 
String dec = new String(decrypt);
 
BASE64Decoder decoder = new BASE64Decoder();
byte[] byteSeed = decoder.decodeBuffer(dec);
 
 
// FileInputStream input = new FileInputStream(new File(filetoread));
// ByteArrayOutputStream inStream = new ByteArrayOutputStream();
// PrintStream stream = new PrintStream(inStream);
// PrintStream stream = new PrintStream(input);
 
// BASE64Decoder decoder = new BASE64Decoder();
// String dec = decoder.decodeBuffer(stream);
 
// String dec = new String(stream);
 
// byte[] decryptionBytes = inStream;
// byte[] s = decrypt(dec);
// decryptionBytes =(byte [])stream.readStream();
// stream.close();
 
// for(int i=0; i < decryptionBytes.length; i++)
    // System.out.print(decryptionBytes);
 
 
byte[] imageOutput = decrypt(byteSeed);
 
    }catch(Exception ex) {
System.out.println("Exception: " + ex);
ex.printStackTrace();
}
}
 
 
 
 
 
 
public static byte[] encrypt(byte[] imageBytes) throws Exception {
 
System.out.println("in encrypt");
Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");
PublicKey pubk = keyGenerator();
System.out.println("read out" + pubk);
cipher.init(Cipher.ENCRYPT_MODE, pubk);
 
byte[] encrypted = new byte[imageBytes.length];
System.out.println("Imagebyte length: " + imageBytes.length);
byte[] temp = new byte[53];
byte[] temp2 = null;
int x, y, z = 0;
int repeats = imageBytes.length/53;
System.out.println("Iterations: " + repeats);
	
for (z=0; z<repeats; z++) {
System.out.println("Iteration number: " + z);
int offset = z*53;
for (x=0; x><53; x++) {
temp[x] = (byte) imageBytes[offset+x];
}
temp2 = cipher.doFinal(temp);
for (y=0; y<53; y++) {
encrypted[offset+y] = (byte) temp2[y];
}
temp2 = null;
}
return encrypted;
	}
//return cipher.doFinal(imageBytes); 
 
 
 
public static byte[] decrypt(byte[] bytesIn) throws Exception{
 
System.out.println("in decrypt");
Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");
PrivateKey prvk = loadPrvkey();
System.out.println("read out" + prvk);
cipher.init(Cipher.DECRYPT_MODE, prvk);
 
byte[] decodeData= cipher.doFinal(bytesIn);
 
 
 
 
 
/*byte[] decrypted = new byte[bytesIn.length];
System.out.println("Imagebyte length: " + bytesIn.length);
byte[] temp = new byte[64];
byte[] temp2 = null;
int x, y, z = 0;
int repeats = bytesIn.length/64;
System.out.println("Iterations: " + repeats);
	
for (z=0; z<repeats; z++) {
System.out.println("Iteration number: " + z);
int offset = z*64;
for (x=0; x><64; x++) {
temp[x] = (byte) bytesIn[offset+x];
}
temp2 = cipher.doFinal(temp);
for (y=0; y<64; y++) {
decrypted[offset+y] = (byte) temp2[y];
}
temp2 = null;
}
return decrypted;*/
return decodeData;
}
 
 
/*the keyGenerator method is the method that is used to create the private and 
*publickeys that are used for the encryption and decryption.
*/        
public static PublicKey keyGenerator()throws Exception {
 
//try{
KeyPairGenerator keyGen = KeyPairGenerator.getInstance("rsa");
keyGen.initialize(512);
KeyPair KeyPair = keyGen.generateKeyPair();
PublicKey pubk = KeyPair.getPublic();
PrivateKey prvk = KeyPair.getPrivate();
System.out.println("privatekey" + prvk);
saveprvKey(prvk);
 
//} catch(Exception ex) {
//System.out.println("Exception: " + ex);
//ex.printStackTrace();
//}
return pubk;
}
 
/*The saveprvKey method is used to save the private key to the system. the encoded
*privatekey is encoded into base64 before it is stored.
*/
private static void saveprvKey(PrivateKey prvky)throws Exception {
 
byte[] byteSeed = prvky.getEncoded();
 
BASE64Encoder encoder = new BASE64Encoder();
String seed = encoder.encodeBuffer(byteSeed);
FileWriter fw = new FileWriter("f:/privkey.txt");
fw.write(seed);
fw.close();
 
}
 
/*The loadPrvkey method is used to load the private key that is stored on the 
*system into the program. this then allows for the private key to be used
*to decrypt the image byte array. The private key is decoded using base64
*and then the decoded version is returned
*/
 
public static PrivateKey loadPrvkey() throws Exception{
 
FileInputStream fis = new FileInputStream("f:/privkey.txt");
byte[] encKey = new byte[fis.available()];
fis.read(encKey);
fis.close();
 
String seed = new String(encKey);
 
BASE64Decoder decoder = new BASE64Decoder();
byte[] byteSeed = decoder.decodeBuffer(seed);
//PrivateKey prvKey = getPrivate(byteSeed);
//PrivateKey prvKey = new PrivateKey(byteSeed);
 
KeyFactory rsaKeyFac = KeyFactory.getInstance("RSA");
PKCS8EncodedKeySpec encodedKeySpec = new PKCS8EncodedKeySpec(byteSeed);
RSAPrivateKey prvKey = (RSAPrivateKey)rsaKeyFac.generatePrivate(encodedKeySpec);
 
return prvKey;
    
}
 
 
/* the main method is where the program is run from, this main method will 
* allow the user to choose between encryption and decryption with the entry 
*of an e or d in the user input console then the method chosen will be 
*called. it will either be imageConvert or Byteconvert.
*/
 
public static void main (String[] unused) throws Exception {
 
System.out.println("***********************************************");
System.out.println("***    Biometric Encrytion Program          ***");
System.out.println("***********************************************");
 
System.out.println("Please select a process");
System.out.println("e for encryption ");
System.out.println("d for decrytion");
 
boolean choose = false;
 
BufferedReader userInput = new BufferedReader(new InputStreamReader(System.in));
String choice = null;
choice = userInput.readLine();
        
if (choose = choice.equals("e")){
    
imageConvert();
    
System.out.println("Encryption");
}
else 
System.out.println("Decryption"); 
    
byteConvert();
}
}
this has also been posted in cryptography
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on May 13 2007
Added on Apr 15 2007
1 comment
7,537 views