Skip to Main Content

Java Security

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

RSA Encryption & Decryption ???

843811Apr 2 2010 — edited Apr 2 2010
I have been working with RSA Encryption & Decryption and i am not able to find a solution for this error.

When i run the program attached, i get the output as ....
Generating Keys...
Encrypted...
Exception In Decrypt ::: javax.crypto.BadPaddingException: Data must start with zero
Please Check this out and tell me what is the reason for this... Please do help me guyz else i cant complete my project....
package RSA;

import java.io.*;
import java.security.*;
import javax.crypto.*;

public class Operator
{
  byte[] Data = new byte[50];
  byte[] CipherText = new byte[50];
  byte[] inBytes = new byte[50];
  byte[] outBytes = new byte[50];

  public void Encrypt(File ToEnc)
  {
     try
     {
      File F = new File("./DataKeys/public.key");
      FileInputStream fin = new FileInputStream(F);
      ObjectInputStream ois = new ObjectInputStream(fin);
      PublicKey publickey = (PublicKey)ois.readObject();
      Cipher cipher = Cipher.getInstance("RSA");
      cipher.init(Cipher.ENCRYPT_MODE, publickey);
      FileInputStream fis = new FileInputStream(ToEnc);
      FileOutputStream fos = new FileOutputStream("./Encrypted/"+ToEnc.getName());
      while(fis.read(inBytes)!=-1)
      {
       outBytes = cipher.doFinal(inBytes);
       fos.write(outBytes);
       fos.flush();
      }
      ois.close();
      System.out.println("Encrypted...");
     }
     catch (Exception e)
     {
         System.out.println("Exception In Encrypt ::: " + e);
     }
    }

    public void Decrypt(File ToDec)
    {
     try
     {
      File F = new File("./DataKeys/private.key");
      FileInputStream fin = new FileInputStream(F);
      ObjectInputStream ois = new ObjectInputStream(fin);
      PrivateKey privatekey = (PrivateKey)ois.readObject();
      Cipher cipher = Cipher.getInstance("RSA");
      cipher.init(Cipher.DECRYPT_MODE, privatekey);
      FileInputStream fis = new FileInputStream(ToDec);
      FileOutputStream fos = new FileOutputStream("./Decrypted/"+ToDec.getName());
      while(fis.read(CipherText)!=-1)
      {
       Data = cipher.doFinal(CipherText);
       fos.write(Data);
       fos.flush();
      }
      ois.close();
      System.out.println("Decrypted...");
     }
     catch (Exception e)
     {
         System.out.println("Exception In Decrypt ::: " + e);
     }
 }

 public void GenerateKeys()
 {
     try
        {
           System.out.println("Generating Keys...");
           KeyPairGenerator Kpg = KeyPairGenerator.getInstance("RSA");
           Kpg.initialize(1024);
           KeyPair kp = Kpg.genKeyPair();
           PublicKey PublicKey = kp.getPublic();
           PrivateKey PrivateKey = kp.getPrivate();
           File F = new File("./DataKeys/public.key");
           File G = new File("./DataKeys/private.key");
           FileOutputStream foo1 = new FileOutputStream(F);
           FileOutputStream foo2 = new FileOutputStream(G);
           ObjectOutputStream oos1 = new ObjectOutputStream(foo1);
           ObjectOutputStream oos2 = new ObjectOutputStream(foo2);
           oos1.writeObject(PublicKey);
           oos2.writeObject(PrivateKey);
        }
        catch (Exception e)
        {
            System.out.println("Exception In Generating Key ::: " + e);
        }
 }

    public static void main(String[] args)
    {
        try
        {
          Operator Op = new Operator();
          Op.GenerateKeys();
          Op.Encrypt(new File("./DATA/Hello.txt"));
          Op.Decrypt(new File("./Encrypted/Hello.txt"));
        } catch (Exception e) {System.out.println(e);}
    }

}
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Apr 30 2010
Added on Apr 2 2010
1 comment
677 views