BadPaddingException

I am trying to construct a program that encrypts/decrypts files (containing cash amounts to be used like bank cards) and I can't seem to get past this badpaddingexception errors. I am only new to Java and encryption so any help is greatly appreciated.

import javax.crypto.*;
import javax.crypto.spec.*;
import java.security.*;
import java.io.*;
import java.util.Scanner;
import java.nio.*;

public class test4
{
public static void main(String[] args) throws Exception
{
byte[] plainData;
byte[] encryptedData;
byte[] decryptData;
int depositamount = 0, withdrawamount= 0, accountbalance=0;
String acctNum,pwd;
int counter=0;
int choice=1;
String answer="";
byte[] bos=null;
int accbalance=0;

Scanner input = new Scanner(System.in);

System.out.println("New Card? y/n");
answer=input.nextLine();
if (answer.charAt(0)=='Y' || answer.charAt(0)=='y')
{
KeyGenerator keygen = KeyGenerator.getInstance("DES");
keygen.init(56);
SecretKey key = keygen.generateKey();
byte[] keyBytes=key.getEncoded();
String filename="secret4942.key";
File sFile = new File(filename);
FileOutputStream sFileStream = new FileOutputStream(sFile);
sFileStream.write(keyBytes);
sFileStream.close();
}
System.out.println("Welcome to the Smart Cash Card Manager");
System.out.println();
while (choice > 0|| choice <4)
{
System.out.println("Access Menu");
System.out.println("0 to Quit");
System.out.println("1 to Load cash");
System.out.println("2 to Use cash");
System.out.println("3 to Check balance");
System.out.println();
System.out.println("Your choice");
choice = input.nextInt();
if (choice==1)
{
File f2 = new File("cash4942.mny");
File f1 = new File("secret4942.key");
FileInputStream in2 = new FileInputStream(f2);
FileInputStream in1 = new FileInputStream(f1);
byte [] byteResult = new byte[(int)f1.length()];
in1.read(byteResult);
in1.close();
SecretKey result = new SecretKeySpec(byteResult,"DES");
encryptedData = new byte[(int)f2.length()];
in2.read(encryptedData);
Cipher cipher= Cipher.getInstance("DES/ECB/PKCS5Padding");
cipher.init(Cipher.DECRYPT_MODE,result);
decryptData = cipher.doFinal(encryptedData);
ByteArrayInputStream bis2 = new ByteArrayInputStream(decryptData);
DataInputStream ins2 = new DataInputStream(bis2);
String message=ins2.readLine();
int accbal=Integer.parseInt(message);
System.out.println("Enter amount:");
depositamount=input.nextInt();
accountbalance=accbal+depositamount;
System.out.println("$" + depositamount + " added to the
card
");
String str = new Integer(accountbalance).toString();
cipher.init(Cipher.ENCRYPT_MODE, result);
encryptedData = cipher.doFinal(str.getBytes());
File aFile = new File("cash4942.txt");
FileOutputStream target2 = new FileOutputStream(aFile);
target2.write(encryptedData);
target2.close();
}
else if (choice==2)
{
File f2 = new File("cash4942.txt");
File f1 = new File("secret4942.key");
FileInputStream in2 = new FileInputStream(f2);
FileInputStream in1 = new FileInputStream(f1);
byte [] byteResult = new byte[(int)f1.length()];
in1.read(byteResult);
SecretKey result = new SecretKeySpec(byteResult,"DES");
encryptedData = new byte[(int)f2.length()];
in2.read(encryptedData);
Cipher cipher= Cipher.getInstance("DES/ECB/PKCS5Padding");
cipher.init(Cipher.DECRYPT_MODE,result);
decryptData = cipher.doFinal(encryptedData);
ByteArrayInputStream bis2 = new ByteArrayInputStream(decryptData);
DataInputStream ins2 = new DataInputStream(bis2);
String message=ins2.readLine();
int accbal=Integer.parseInt(message);
System.out.println("Enter amount");
withdrawamount=input.nextInt();
if (withdrawamount<accbal)
{
accbal=accbal-withdrawamount;
System.out.println("$" + withdrawamount + " withdrawn from the card
");
}
else if (withdrawamount>accbal)
{
System.out.println("Sorry, not enough money in he card.
Request rejected");
}
String str = new Integer(accbal).toString();
cipher.init(Cipher.ENCRYPT_MODE, result);
encryptedData = cipher.doFinal(str.getBytes());
File aFile = new File("cash4942.txt");
FileOutputStream target2 = new FileOutputStream(aFile);
target2.write(encryptedData);
target2.close();
}
else if (choice==3)
{
File f2 = new File("cash4942.txt");
File f1 = new File("secret4942.key");
FileInputStream in2 = new FileInputStream(f2);
FileInputStream in1 = new FileInputStream(f1);
byte [] byteResult = new byte[(int)f1.length()];
in1.read(byteResult);
SecretKey result = new SecretKeySpec(byteResult,"DES");
encryptedData = new byte[(int)f2.length()];
in2.read(encryptedData);
Cipher cipher= Cipher.getInstance("DES/ECB/PKCS5Padding");
cipher.init(Cipher.DECRYPT_MODE,result);
decryptData = cipher.doFinal(encryptedData);
ByteArrayInputStream bis2 = new ByteArrayInputStream(decryptData);
DataInputStream ins2 = new DataInputStream(bis2);
String message=ins2.readLine();
int accbal=Integer.parseInt(message);
System.out.println("The card balance is " + accbal);
String str = new Integer(accountbalance).toString();
cipher.init(Cipher.ENCRYPT_MODE, result);
encryptedData = cipher.doFinal(str.getBytes());
File aFile = new File("cash4942.txt");
FileOutputStream target2 = new FileOutputStream(aFile);
target2.write(encryptedData);
target2.close();
}
else if (choice==0)
{
System.out.println("Bye
");
System.exit(0);
}
else // checking for other inputs
{
answer="";
System.out.println("Do you want to exit? (Y or N) ");
input.nextLine();
answer=input.nextLine(); //getting the inputs and allocating
if (answer.charAt(0)=='Y' || answer.charAt(0)=='y')
{
System.out.println("Bye");//gives the system command to print
System.exit(0);// gives command for the if statement
}
}
}
}
}
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories