Encrypt and Decrypt a Hex Value PINBLOCK
843810May 9 2003 — edited May 16 2003Hi everyone,
I am developing a Kiosk self service for a bank, in the kiosk I will get the track2 of the card and the NIP (PIN) inserted by the customer.
I need to build a PINBLOCK with the information that I will get from the kiosk (its no problem), it is like the next one
PIN BLOCK: 0483729AFEE8DDD9
I need to encrypt using DES using this key:
KEY: 1D174695326154F0
And to get this value:
Encrypted PIN BLOCK: C5150A7E52C37BC5
I dont know what is the problem, this is the output of the program
cleartext as hex array 04:83:72:9A:FE:E8:DD:0D
ciphertext as hex arrayAE:4B:2F:A8:D3:6D:3A:7E:A9:55:01:75:33:BC:D8:C9
clearText as hex array04:83:72:9A:FE:E8:DD:0D
This is encrypting in a good way, but I need to get this value :C5150A7E52C37BC5 like the encrypted value, I dont know what I am doing wrong,
I dont know if I need to specify a mode or padding?
Help!!!!!!
I am using the next Code:
public static void encrypt(String dataToEncrypt) {
// byte[] cleartext;
byte[] ciphertext;
byte[] recovered;
byte[] desKeyData = { (byte)0x1D, (byte)0x17, (byte)0x46,
(byte)0x95, (byte)0x32, (byte)0x61, (byte)0x54,
(byte)0xF0};
Cipher desCipher;
SecretKeyFactory keyFactory = null;
SecretKey desKey = null;
try {
DESKeySpec desKeySpec = new DESKeySpec(desKeyData);
keyFactory = SecretKeyFactory.getInstance("DES");
desKey = keyFactory.generateSecret(desKeySpec);
} catch (Exception e) {
System.out.println(e.getMessage());
}
try {
desCipher = Cipher.getInstance("DES");
desCipher.init(Cipher.ENCRYPT_MODE, desKey);
byte[] cleartext = { (byte)0x04, (byte)0x83, (byte)0x72,
(byte)0x9A, (byte)0xFE, (byte)0xE8, (byte)0xDD,
(byte)0xD};
System.out.println(" cleartext as hex array " + toHexString(cleartext));
ciphertext = desCipher.doFinal(cleartext);
System.out.println("Block Size::" + cleartext[0]);
System.out.println("ciphertext " + new String(ciphertext));
System.out.println("ciphertext as hex array" + toHexString(ciphertext));
desCipher.init(Cipher.DECRYPT_MODE, desKey);
recovered = desCipher.doFinal(ciphertext);
System.out.println("clearText as hex array" + toHexString(recovered));
System.out.println("recovered " + new String(recovered));
} catch (Exception e) {
System.out.println(e.getMessage());
}
} }
private static void byte2hex(byte b, StringBuffer buf) {
char[] hexChars =
{ '0', '1', '2', '3', '4', '5', '6', '7', '8',
'9', 'A', 'B', 'C', 'D', 'E',
'F' };
int high = ((b & 0xf0) >> 4);
int low = (b & 0x0f);
buf.append(hexChars[high]);
buf.append(hexChars[low]);
}
/*
* Converts a byte array to hex string
* (from examples)
*/
private static String toHexString(byte[] block) {
StringBuffer buf = new StringBuffer();
int len = block.length;
for (int i = 0; i < len; i++) {
byte2hex(block, buf);
if (i < len - 1) {
buf.append(":");
}
}
return buf.toString();
}