• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

Why this code running incorrect?

 
Ranch Hand
Posts: 113
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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());
}
}
}

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic