Hi ALL:
I am working in JCE ..And I need some expertise help.
The problem is I am unable to encrypt(gif's)/decrypt all the files that I have encrypted.
I hope you can provide me technical help as to why I am unable to encrypt the GIF's and decrypt all the files.
Kindly email me at
ritesh_swd@suvistas.com Thanking you.
Regards
R.Ritesh
SUVISTAS SOFTWARE PVT LTD
HYDERABAD
INDIA
Encls:Source Code>>>>>>>>>>>>>>>>>>>>>>>.>>
import javax.crypto.*;
import java.security.*;
import java.math.*;
import java.security.spec.*;
import javax.crypto.spec.*;
import com.sun.crypto.provider.*;
import java.io.*;
import java.util.*;
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
public class Cipher2_TextFile extends Frame implements ActionListener,Serializable
{
//Declare the Providers
static SunJCE jce;
//Declare the Ciphers
Cipher cenc1;
Cipher cenc2;
Cipher cext;
//InputStreams Declarations
FileInputStream fis;
FileInputStream fis1;
OutputStreamWriter fos;
FileOutputStream fos1;//OutputStreamWriter fos1;
FileDialog fdlog;
String filename="";
CipherInputStream cis1;
CipherInputStream cis2;
// Salt
byte[] salt = {
(byte)0xc7, (byte)0x73, (byte)0x21, (byte)0x8c,
(byte)0x7e, (byte)0xc8, (byte)0xee, (byte)0x99
};
// Iteration count
int count = 20;
//Declare the Random Variables and the Retrieval of Seed from the User
String _pwd_seed="";//lblPasswordSeed.getText();
SecretKeySpec secretKey;
PBEParameterSpec pbeParamSpec;
PBEKeySpec pbeKeySpec;
byte[] pwd_seed;
//Secret Key for Extension Object
//Declarations for initial seed for Extension
private byte[] desKeyData = { (byte)0x01, (byte)0x02,(byte)0x03,(byte)0x04,(byte)0x05,(byte)0x06,(byte)0x07,(byte)0x08,(byte)0x09,
(byte)0x0A, (byte)0x0B,(byte)0x0C,(byte)0x0D,(byte)0x0E,(byte)0x0F,(byte)0x10,(byte)0x11,(byte)0x12
};
SecretKey pbeKey_ext;
//Define the Components and the Layout
GridBagLayout gridbag = new GridBagLayout();
GridBagConstraints c = new GridBagConstraints();
Label lblEncryption = new Label();
Label lblChooseFile = new Label();
Label lblPassword=new Label();
Label lblSpace1 = new Label();
Label lblSpace2 = new Label();
Label lblSpace3 = new Label();
Label lblSpace4 = new Label();
Label lblSpace5 = new Label();
Label lblSpace6 = new Label();
Label lblSpace7 = new Label();
Label lblStatus = new Label();
Label lblMsgs = new Label();
Label lblDrive=new Label();
Button btnBrowse = new Button("Browse");
//Generate the Key,Store the key and Write the key to a *.ENC file
Button btnEncrypt = new Button("Encrypt");
Button btnDecrypt = new Button("Decrypt");
//Button btnGenKey = new Button("Generate-Key");
TextField txtFilename = new TextField(25);
TextField txtPasswordSeed=new TextField(10);
TextField txtDrive=new TextField(25);
protected String gMessage = " ";
//A Dialog Frame is opened
FileDialog dlgOpen;
File file;
//Define the Encrypt class And Decrypt class
Cip_TextFile cip;
Dec_TextFile dec;
//Declare the Sealed Object
SealedObject so;
//A Static Initializer to Load the Cryptographic Service Provider
static
{
try{
// Add JCE to the list of providers
jce = new SunJCE();
Security.addProvider(jce);
}
catch(Exception e){}
}
static AccessControlContext acc;
//Static initializer to LOAD the SYSTEMS FILEs
static
{
try
{
//if (Class.forName("java.security.AccessControlContext") != null)
//{
//PolicyEngine.assertPermission(PermissionID.SYSTEM);
//}
if (Class.forName("java.security.AccessControlContext") != null)
{
acc = AccessController.getContext();
AllPermission per=new AllPermission();
acc.checkPermission(per);
}
/* grant my application AllPermission */
/*grant{
permission java.security.FilePermission "*";
};*/
}catch (Exception cnfe) {}
}
Cipher2_TextFile()
{
//Set the Layout
setLayout(gridbag);
lblEncryption.setText("Encryption/Decryption");
lblEncryption.setFont(new Font("Verdana",1,20));
lblChooseFile.setText("Choose File ");
lblPassword.setText("Enter Password ");
lblStatus.setText("STATUS");
lblDrive.setText("Enter the drive");
lblMsgs.setText("Select a File to Encrypt/Decrypt");
//ENcryption row
c.anchor = GridBagConstraints.CENTER;
c.gridwidth = GridBagConstraints.REMAINDER;
gridbag.setConstraints(lblEncryption,c);
add(lblEncryption);
//Two Space rows
c.anchor = GridBagConstraints.CENTER;
c.gridwidth = GridBagConstraints.REMAINDER;
gridbag.setConstraints(lblSpace1,c);
add(lblSpace1);
c.anchor = GridBagConstraints.CENTER;
c.gridwidth = GridBagConstraints.REMAINDER;
gridbag.setConstraints(lblSpace2,c);
add(lblSpace2);
//End of Space Rows
//Browse Row
c.gridwidth=1;
c.weightx = 0.0;
c.anchor = GridBagConstraints.WEST;
gridbag.setConstraints(lblChooseFile,c);
add(lblChooseFile);
c.weightx = 1.0;
c.gridwidth = GridBagConstraints.RELATIVE;
gridbag.setConstraints(txtFilename,c);
add(txtFilename);
c.weightx=0.0;
c.gridwidth = GridBagConstraints.REMAINDER;
gridbag.setConstraints(btnBrowse,c);
add(btnBrowse);
// End of Browse Row
//Space Row
c.anchor = GridBagConstraints.CENTER;
c.gridwidth = GridBagConstraints.REMAINDER;
gridbag.setConstraints(lblSpace6,c);
add(lblSpace6);
//End of Space Row
//Password Row
c.gridwidth=1;
c.weightx = 0.0;
c.anchor = GridBagConstraints.WEST;
gridbag.setConstraints(lblPassword,c);
add(lblPassword);
c.weightx = 1.0;
c.gridwidth = GridBagConstraints.RELATIVE;
gridbag.setConstraints(txtPasswordSeed,c);
char echo='*';
txtPasswordSeed.setEchoChar(echo);
add(txtPasswordSeed);
// End of password Row
//Space Row
c.anchor = GridBagConstraints.CENTER;
c.gridwidth = GridBagConstraints.REMAINDER;
gridbag.setConstraints(lblSpace7,c);
add(lblSpace7);
//End of Space Row
//Extension Row
c.gridwidth=1;
c.weightx = 0.0;
c.anchor = GridBagConstraints.WEST;
gridbag.setConstraints(lblDrive,c);
add(lblDrive);
c.weightx = 1.0;
c.gridwidth = GridBagConstraints.RELATIVE;
gridbag.setConstraints(txtDrive,c);
add(txtDrive);
//End of Extension Row
//Space Row
c.anchor = GridBagConstraints.CENTER;
c.gridwidth = GridBagConstraints.REMAINDER;
gridbag.setConstraints(lblSpace4,c);
add(lblSpace4);
//End of Space Row
//Encrypt Row
c.gridwidth = 1;
c.weightx = 2.0;
gridbag.setConstraints(lblSpace3,c);
add(lblSpace3);
c.weightx = 0.0;
c.insets = new Insets(0,110,0,0);
c.gridwidth = GridBagConstraints.RELATIVE;
gridbag.setConstraints(btnEncrypt,c);
add(btnEncrypt);
c.weightx = 0.0;
c.insets = new Insets(0,0,0,0);
c.gridwidth = GridBagConstraints.REMAINDER;
gridbag.setConstraints(btnDecrypt,c);
add(btnDecrypt);
//End of Cancel Row
//Space Row
c.anchor = GridBagConstraints.CENTER;
c.gridwidth = GridBagConstraints.REMAINDER;
gridbag.setConstraints(lblSpace5,c);
add(lblSpace5);
//End of Space Row
//Status Row
c.anchor = GridBagConstraints.WEST;
c.gridwidth = 1;
c.weightx = 0.0;
gridbag.setConstraints(lblStatus,c);
add(lblStatus);
c.gridwidth = GridBagConstraints.REMAINDER;
c.weightx = 1.0;
gridbag.setConstraints(lblMsgs,c);
add(lblMsgs);
btnBrowse.addActionListener(this);
btnEncrypt.addActionListener(this);
btnDecrypt.addActionListener(this);
}
public void actionPerformed(ActionEvent e)
{
//The BROWSE FUNCTION SEARCHES FOR THE FILESYSTEM FILES
if(e.getSource()==btnBrowse)
{
try
{
// create frame for file dialog
fdlog = new FileDialog(this,"Open",FileDialog.LOAD);
// bring the still invisible frame to the front, then show it
fdlog.toFront();
fdlog.show();
// capture both path and file selected by user; assemble into one file pathname
filename = fdlog.getDirectory() + fdlog.getFile();
//Check File
file = new File(filename);
}catch(Exception e4){}
try
{
txtFilename.setText(filename);
}catch(Exception e3){}
lblMsgs.setText(filename);
}
if(e.getSource()==btnEncrypt)
{
try
{
cip=new Cip_TextFile();
}
catch(Exception e2){}
}
if(e.getSource()==btnDecrypt)
{
try
{
dec=new Dec_TextFile();
}
catch(Exception e10)
{
System.out.println(e10);
}
}
}
public static void main(String args[])
{
Cipher2_TextFile ciph=new Cipher2_TextFile();
ciph.setSize(500,500);
ciph.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){System.exit(0);}
}
);
ciph.setVisible(true);
}
//Opens a File
//Begin the Class-Cipher_2_TextFile
private class Cip_TextFile
{
Cip_TextFile()
{
try
{
btnDecrypt.setEnabled(false);//The Decrypt is disabled
txtDrive.setEnabled(true);
//Development of a Cryptographically-safe PRNG
//lblMsgs.setText("The New Key ....");
//Encrypt the File
String finfilename = "";//for final Filename extracted from absolute path
String fi_filename="";//for the process of getting actual file name
String infilename = txtFilename.getText();//This is the actual file with absolte path given by user
//lblMsgs.setText("Encryptiing File ........");
//Generate the Key
_pwd_seed=txtPasswordSeed.getText();
char[] lineBuffer=_pwd_seed.toCharArray();
char[] ret=new char[_pwd_seed.length()];
System.arraycopy(lineBuffer, 0, ret, 0, _pwd_seed.length());
Arrays.fill(lineBuffer, ' ');
lblMsgs.setText("Key is copied ....");
lblMsgs.setText("Encryptiing File ........");
// Create PBE parameter set
pbeParamSpec = new PBEParameterSpec(salt,count);
pbeKeySpec = new PBEKeySpec(ret);
lblMsgs.setText("The Key Parameter is set ....");
//SecretkeyFactory Init
SecretKeyFactory keyFac = SecretKeyFactory.getInstance("PBEWithMD5AndDES");
SecretKey pbeKey = keyFac.generateSecret(pbeKeySpec);
System.out.println("The Key is finally generated ........");
lblMsgs.setText("The Key is finally generated ........");
//StringTokenizer for infile to get actual file name from absolute path
StringTokenizer stzinFile=new StringTokenizer(infilename,"\\");
while(stzinFile.hasMoreTokens()){
fi_filename = stzinFile.nextToken();
}
StringTokenizer stzinFile1=new StringTokenizer(fi_filename,".");
finfilename = stzinFile1.nextToken();
//Read the Extension of the Input FILE
String extRetrieve="";
StringTokenizer stzRFile1=new StringTokenizer(fi_filename,".");
while(stzRFile1.hasMoreTokens()){
extRetrieve = stzRFile1.nextToken();
}
System.out.println(extRetrieve);
//Encrypt the Extension.Initialise the Cipher for the extension
//Get a Seperate KEY for Extension
DESKeySpec desKeySpec = new DESKeySpec(desKeyData);
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
pbeKey_ext = keyFactory.generateSecret(desKeySpec);
cext=Cipher.getInstance("DES");
cext.init(Cipher.ENCRYPT_MODE,pbeKey_ext);
so = new SealedObject(extRetrieve,cext);
String outFilename ="";
outFilename="C:"+"\\"+finfilename+".enc";
System.out.println("The Key is finally generated ........");
lblMsgs.setText("Encryptiing FILE ........");
//Initialize the Cipher
cenc1=Cipher.getInstance("PBEWithMD5AndDES");
lblMsgs.setText("Initialising CIpher");
cenc1.init(Cipher.ENCRYPT_MODE,pbeKey,pbeParamSpec);
lblMsgs.setText("Initialised the CIpher");
//Read a InputStream
//Start writihng using OutputSTreams
fis=new FileInputStream(infilename);
cis1 = new CipherInputStream(fis, cenc1);
fos=new OutputStreamWriter(new FileOutputStream(outFilename));
InputStreamReader inp=new InputStreamReader(cis1);
BufferedWriter bugwrite=new BufferedWriter(fos);
char[] buf = new char[1024];
int len;
while ((len = inp.read(buf)) > 0) {
bugwrite.write(buf,0,len);
}
fis.close();
fos.close();
lblMsgs.setText("Encrypted file"+outFilename);
txtFilename.setText("");
btnDecrypt.setEnabled(true);//The Decrypt is disabled
txtDrive.setEnabled(true);
//btnEncrypt.setEnabled(false);//Now set the Encrypt disabled
txtPasswordSeed.setText("");
}catch(Exception e8)
{
lblMsgs.setText("There is a error");
System.err.print("The error is "+e8);
e8.printStackTrace();
}
//txtFilename.setText("");
}
}//End of Encrypt class
//Start of Decrypt class
private class Dec_TextFile
{
Dec_TextFile()
{
try
{
btnEncrypt.setEnabled(false);//The Encrypt is disabled
txtDrive.setEnabled(false);
lblMsgs.setText("Browse file to Decrypt...");
String decfilename = "";//for final Filename extracted from absolute path
String dec_filename="";//for the process of getting actual file name
String defilename = txtFilename.getText();//This is the actual file with absolte path given by user
//Retrieve and Store the seed
String de_seed=txtPasswordSeed.getText();
char[] deBuffer=de_seed.toCharArray();
//The Key is recollected here.Create PBE parameter set
//pbeParamSpec = new PBEParameterSpec(salt, count);
pbeKeySpec = new PBEKeySpec(deBuffer);
//SecretkeyFactory Init
SecretKeyFactory dkeyFac = SecretKeyFactory.getInstance("PBEWithMD5AndDES");
SecretKey dpbeKey = dkeyFac.generateSecret(pbeKeySpec);
//Initialize the Cipher again..TO UNSEAL THE EXTENSION OBJECT
cext.init(Cipher.DECRYPT_MODE,pbeKey_ext);
String ext1_de =(String)so.getObject(cext);
String Folder_Drive=txtDrive.getText();
String de_Filename ="";
de_Filename=Folder_Drive+"."+ext1_de;
lblMsgs.setText("Decryptiing File ........");
//Define the Cipher Class
cenc2=Cipher.getInstance("PBEWithMD5AndDES");
//Initialize the Cipher for Decryption
cenc2.init(Cipher.DECRYPT_MODE,dpbeKey,pbeParamSpec);
//Read the ENC file
fis1=new FileInputStream(defilename);
cis2 = new CipherInputStream(fis1,cenc2);
//Writing the content-encrypted to a new file(*.*)
/*fos1=new OutputStreamWriter(new FileOutputStream(de_Filename));
InputStreamReader dep=new InputStreamReader(cis2);
BufferedWriter de_write=new BufferedWriter(fos1);
lblMsgs.setText(de_Filename);
char[] buf = new char[1024];
int len;
while ((len = dep.read(buf)) > 0) {
de_write.write(buf,0,len);
}*/
fos1=new FileOutputStream(de_Filename);
byte[] debuf = new byte[1024];
int delen;
while ((delen = cis2.read(debuf)) > 0) {
fos1.write(debuf, 0, delen);
}
lblMsgs.setText("File Decrypted...");
//fis1.close();
//fos1.close();
lblMsgs.setText("The file is decrypted"+de_Filename);
btnDecrypt.setEnabled(false);//The Encrypt is disabled
btnEncrypt.setEnabled(true);//The Encrypt is disabled
//Set the Appliaction back to normal
btnDecrypt.setEnabled(true);
txtFilename.setText("");
txtPasswordSeed.setText("");
txtDrive.setText("");
lblMsgs.setText("Select file to Encrypt/Decrypt..");
}
catch(Exception e11)
{
System.out.println("The error is"+e11);
//e11.printStackTrace();
e11.printStackTrace();
}
}
}
//End of Decrypt class
}
------------------