# 2 Re: BadPaddingException: data must start with zero??
Here is my code,can you see any problems in it........???
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.io.*;
import java.security.*;
import javax.crypto.*;
import java.math.BigInteger;
import sun.misc.*;
public class RunCrypto1
{
public static void main(String[] args)
{
JFrame frame = new JFrame();
frame.setTitle("Cryptotron");
FileHandler fh = new FileHandler(frame);
frame.setSize(500,300);
frame.getContentPane().add(fh);
frame.setVisible(true);
}
}
class FileHandler extends JPanel implements ActionListener
{
File f;
KeyPairGenerator keyGen;
SecureRandom random;
KeyPair key;
PublicKey pub;
PrivateKey priv;
Cipher cipher2;
private JFrame associatedFrame;
private JTextArea viewArea = new JTextArea(8,40);
private JMenuBar bar = new JMenuBar();
private JMenu fileMenu = new JMenu("File");
private JMenu quitMenu = new JMenu("Quit");
private JMenuItem selectChoice = new JMenuItem("Open File");
private JMenuItem reallyQuitChoice = new JMenuItem("Exit");
private JMenuItem cancelChoice = new JMenuItem("Cancel");
private JPanel buttonPanel = new JPanel();
private JButton Encrypt = new JButton("Encrypt");
private JButton Decrypt = new JButton("Decrypt");
private String selectedFile = null;
private String dir = null;
public FileHandler(JFrame frameIn)
{
associatedFrame = frameIn;
add(viewArea);
bar.add(fileMenu);
bar.add(quitMenu);
fileMenu.add(selectChoice);
quitMenu.add(reallyQuitChoice);
quitMenu.add(cancelChoice);
frameIn.setJMenuBar(bar);
Encrypt.addActionListener(this);
Decrypt.addActionListener(this);
buttonPanel.setLayout(new FlowLayout(FlowLayout.CENTER,10,20));
buttonPanel.add(Encrypt);
buttonPanel.add(Decrypt);
add(buttonPanel, "South");
selectChoice.addActionListener(this);
reallyQuitChoice.addActionListener(this);
cancelChoice.addActionListener(this);
try
{
keyGen = KeyPairGenerator.getInstance("RSA");
random = SecureRandom.getInstance("SHA1PRNG", "SUN");
keyGen.initialize(1024,random);
key = keyGen.generateKeyPair();
pub = key.getPublic();
priv = key.getPrivate();
cipher2 = Cipher.getInstance("RSA/ECB/PKCS1Padding");
}
catch ( Exception er)
{
er.printStackTrace();
System.out.println("error..............");
}
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource() == selectChoice)
{
JFileChooser jfc = new JFileChooser();
jfc.setFileSelectionMode(JFileChooser.FILES_ONLY);
jfc.setMultiSelectionEnabled(false);
int status = jfc.showOpenDialog(this);
if(status == JFileChooser.APPROVE_OPTION)
{
System.out.println("File Choosen : " + jfc.getSelectedFile().getName());
f = jfc.getSelectedFile();
}
viewArea.append("Selected File: " + f + '\n');
}
if(e.getSource() == reallyQuitChoice)
{
System.exit(0);
}
if(e.getSource()==cancelChoice)
{
viewArea.append("Quit option cancelled \n" );
}
if(e.getSource()==Encrypt)
{
try
{
viewArea.append("Start Encryption \n" );
cipher2.init(Cipher.ENCRYPT_MODE, pub);
BufferedReader fs = new BufferedReader(new FileReader(f));
String oldFileName = f.getName();
String aLine = "";
String something = "";
while(null != (aLine = fs.readLine()))
{
something = aLine;
System.out.println(something);
}
fs.close();
byte [] cleartext = null;
cleartext = something.getBytes("UTF8");
//System.out.println(cleartext);
viewArea.append(". \n" );
byte[] ciphertext = null;
ciphertext = cipher2.doFinal(cleartext);
//System.out.println(ciphertext.toString());
BASE64Encoder encod = new BASE64Encoder();
String base = encod.encode(ciphertext);
System.out.println(base);
f.delete();
f = new File(oldFileName);
BufferedWriter os = new BufferedWriter(new FileWriter(f));
os.write("" + base);
viewArea.append("File "+ f + " has been Encrypted \n");
viewArea.append(". \n" );
viewArea.append(". \n" );
os.close();
}
catch ( Exception er)
{
er.printStackTrace();
System.out.println("error..............");
}
}
if(e.getSource()==Decrypt)
{
try
{
viewArea.append("Start Decryption \n");
cipher2.init(Cipher.DECRYPT_MODE, priv);
BufferedReader zz = new BufferedReader(new FileReader(f));
String oldFileName2 = f.getName();
String aLine2 = "";
String something2 = "";
while(null != (aLine2 = zz.readLine()))
{
something2 = aLine2;
System.out.println(something2);
}
zz.close();
byte [] ciphertext2 = null;
ciphertext2 = something2.getBytes();
//System.out.println(ciphertext2);
BASE64Decoder decoder = new BASE64Decoder();
byte [] raw1 = decoder.decodeBuffer(something2);
//System.out.println(something2);
byte[] cleartext2 = cipher2.doFinal(ciphertext2);
String res = new String(cleartext2, "UTF8");
//System.out.println("fffffffffff");
System.out.println(res);
f.delete();
f = new File(oldFileName2);
BufferedWriter os2 = new BufferedWriter(new FileWriter(f));
os2.write("" + cleartext2);
viewArea.append("File Decrypted \n");
os2.close();
}
catch (javax.crypto.BadPaddingException hfhh) {
System.out.println("BadPaddingException : " + hfhh);
}
catch ( Exception er)
{
er.printStackTrace();
System.out.println("error......again!!!");
}
}
}
}