Win a copy of Head First Agile this week in the Agile forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Why this code running incorrect?  RSS feed

 
Dorj Galaa
Ranch Hand
Posts: 113
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm try encrypt file encrypt program runned ok code is
import java.security.*;
import javax.crypto.*;
import javax.crypto.spec.*;
import java.io.*;
import com.sun.crypto.provider.*;
import java.util.*;
public class Encrypt
{
public static void main(String args[])
{
try
{
Provider sunJce = new SunJCE();
Security.addProvider(sunJce);
Cipher cipher1 = Cipher.getInstance("DES");
Cipher cipher2 = Cipher.getInstance("DES");
KeyGenerator KG1 = KeyGenerator.getInstance("DES");
KG1.init(56);
Key mykey = KG1.generateKey();
cipher1.init(Cipher.ENCRYPT_MODE, mykey);
FileInputStream fis=new FileInputStream("B.txt");
FileOutputStream fos=new FileOutputStream("C.txt");
byte[] input=new byte[5];
int a=-1;
String Line=null;
while ((a=fis.read(input))!=-1)
{
byte output[]=cipher1.doFinal(input);
System.out.println(Integer.toString(cipher1.getOutputSize(5)));
fos.write(output);
fos.flush();
}
fis.close();
fos.flush();
fos.close();
}
catch(Exception e)
{
System.out.println(e.getMessage());
}
}
}
But decrypt program is not working. caught Given final block not properly padded error. How to DECRYPT?

Decryption code is
import java.security.*;
import javax.crypto.*;
import javax.crypto.spec.*;
import java.io.*;
import com.sun.crypto.provider.*;
import java.util.*;
public class Decrypt
{
public static void main(String args[])
{
try
{
Provider sunJce = new SunJCE();
Security.addProvider(sunJce);
Cipher cipher1 = Cipher.getInstance("DES");
Cipher cipher2 = Cipher.getInstance("DES");
KeyGenerator KG1 = KeyGenerator.getInstance("DES");
KG1.init(56);
Key mykey = KG1.generateKey();
cipher1.init(Cipher.DECRYPT_MODE, mykey);
FileInputStream fis=new FileInputStream("C.txt");
FileOutputStream fos=new FileOutputStream("C1.txt");
byte[] input=new byte[8];
int a=-1;
String Line=null;
while ((a=fis.read(input))!=-1)
{
byte output[]=cipher1.doFinal(input);
System.out.println(Integer.toString(cipher1.getOutputSize(5)));
fos.write(output);
fos.flush();
}
fis.close();
fos.flush();
fos.close();
}
catch(Exception e)
{
System.out.println(e.getMessage());
}
}
}

 
Alan Wentz
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I found a post on the java.sun forums regarding this issue.
A solution, which I have tried and it worked for me unlike many of the suggestions that others made that were "supposed" to have worked for them, is using javax.commerce.util.BASE64Encoder and Decoder. These classes can be found in the Java Wallet API at java.sun.com. Put the jecf.jar file in your classpath and you're ready to go.
Start by BASE64Encoding the unecrypted String first then encrypting it. Now you may store it or whatever. Whenever you need it decrypt it then BASE64Decode it. Did the trick for me.
Good luck.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!