Shani
Shani

Reputation: 447

Encrypt an Integer Value with DES

I want to encrypt an integer with DES, the resultant cipher text should also be an integer.

Decryption function should follow this notion as well.

I am trying to modifying the code at Encrypting a String with DES, by converting the byte array to integer, instead of using Base64 encoding. However the decryption function throws an exception of improper padding, as the conversion of integer to byte[] results in a 4 byte array.

Is there any other encryption algorithm that I can use to achieve this.

Upvotes: 1

Views: 1299

Answers (3)

imichaelmiers
imichaelmiers

Reputation: 3519

You want to look at Format Perserving Encryption. There are a couple of techniques for it, but in general all of them will generate a value in the same domain as your input ( i.e. integers, credit card numbers,etc)

Upvotes: 1

rossum
rossum

Reputation: 15693

DES has a 64 bit blocksize, so in general the output from the encryption of a 32 bit int will be a 64 bit block. It will be easier to encrypt a 64 bit long to another 64 bit long. Use ECB mode so that padding is not an issue, or at least you are adding zero bits to the front of your int to extend it to 64 bits.

If you merely want to smush up your int then Jim's suggestion is excellent.

Upvotes: 1

Jim Garrison
Jim Garrison

Reputation: 86774

If you are running an Integer value through DES to produce another Integer value, and you don't care about the cipher text weakness, then you are merely doing a very expensive hashing operation. You'd be better off generating a random integer as the key and bitwise xor-ing the exponent and the random integer. This would take nanoseconds to compute and have exactly the same security.

Upvotes: 2

Related Questions