• 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

Cryptography Help!!! URGENT

 
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 code is and program working correct. But I can't decode C.txt file. how to decode?
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());
}
}
}
 
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