I'm working on a portion of the codes to encrypt the ATM card of the user.

Public class AtmCardAuth{
public static byte[] computeCheckBytes(int acctNum, int pin)throws Exception {
   // byte[] ret = new byte[32];
   // for(int i=0; i<ret.length; ++i)    ret[i] = (byte)i;
   // return ret;
   String password = Integer.toString(pin);
   String accNumber = Integer.toString(acctNum);
   
   
   
   SecretKey key = new SecretKeySpec(password.getBytes(),"DES");
   Cipher ecipher = Cipher.getInstance("DES");
   ecipher.init(Cipher.ENCRYPT_MODE, key);
   
   byte[] encrypted = ecipher.doFinal(accNumber.getBytes());
   return encrypted;
   
  }
}

AtmCardAuth.computeCheckBytes(5,8842378);

The AtmCardAuth function is use to encrypt the user account number and pin number into a card_5.atm file.An error prompt out while running the code, it mentioned "Invalid key length: 7 bytes". Any solution that can solve the problem. The acctNum and pin parameters value must be in integer type in order to complete the codes.

From what I read from the docs DES cipher needs an 8 Byte Key. If you change your pin to 8 bytes you can run without problems.

Since you are gonna do this ciphering and deciphering, you can add a 0 to front or end of the pin cipher it and when deciphering it you can remove it.

I came across with an ATM encryption algorithm from the internet

First time PIN generation:
Imagine your card number is 4129123456784321.
There is a cryptographic function f and a key k so that
f(4129123456784321, k) = 9876543212345678
The function f and key k are known to the ATM PIN machine.
The ATM PIN machine choses a random pin, lets say 1234.
Then the ATM PIN machine takes the first 4 characters of the encrypted number (9876), subtracts your chosen pin (1234), and stores the result (8642) in the ATM server. This is known as the pin offset.
The ATM PIN machine prints 1234 into your pin mailer using an attached printer.

The problem is most of the encryption of the Java security is generate a byte value as a result of the encryption. I wonder is there any method to implement the "f(4129123456784321, k) = 9876543212345678" ?

both numbers are just examples, and not to be taken literally :)

So yes, there exists a cryptographic function f(a,x) that yields a result b.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.