Using java to encrypt integers

I am trying to encrypt some integers in java using java.security and javax.crypto.

The problem is that the Cipher class only encrypts byte arrays. I cannot directly convert an integer to a byte string (or can I?). What is the best way to do this?

Should I convert an integer to string and string to byte []? This seems too inefficient.

Does anyone know a quick / easy or efficient way to do this?

Please let me know.

Thanks in advance.

Jbu

+6
java integer cryptography bytearray encryption
Nov 26 '08 at 19:06
source share
6 answers

You can turn ints into byte [] with a DataOutputStream, for example:

ByteArrayOutputStream baos = new ByteArrayOutputStream (); DataOutputStream dos = new DataOutputStream (baos); dos.writeInt (i); byte[] data = baos.toByteArray(); // do encryption 

Then decrypt it later:

 byte[] decrypted = decrypt (data); ByteArrayInputStream bais = new ByteArrayInputStream (data); DataInputStream dis = new DataInputStream (bais); int j = dis.readInt(); 
+12
Nov 26 '08 at 19:10
source share

You can also use BigInteger to convert:

  BigInteger.valueOf(integer).toByteArray(); 
+9
Nov 26 '08 at 19:14
source share

Just use NIO. It is intended for this specific purpose. ByteBuffer and IntBuffer will do what you need quickly, efficiently and elegantly. It will handle large / small final conversions, "direct" buffers for high-performance I / O, and you can even mix data types into a byte buffer.

Converting integers to bytes:

 ByteBuffer bbuffer = ByteBuffer.allocate(4*theIntArray.length); IntBuffer ibuffer = bbuffer.asIntBuffer(); //wrapper--doesn't allocate more memory ibuffer.put(theIntArray); //add your int here; can use //array if you want byte[] rawBytes = bbuffer.array(); //returns array backed by bbuffer-- //ie *doesn't* allocate more memory 

Convert bytes to integers:

 ByteBuffer bbuffer = ByteBuffer.wrap(rawBytes); IntBuffer ibuffer = bbuffer.asIntBuffer(); while(ibuffer.hasRemaining()) System.out.println(ibuffer.get()); //also has bulk operators 
+5
Nov 26 '08 at 21:15
source share

I found the following code that can help you, since Integer in Java always has a length of 4 bytes.

 public static byte[] intToFourBytes(int i, boolean bigEndian) { if (bigEndian) { byte[] data = new byte[4]; data[3] = (byte) (i & 0xFF); data[2] = (byte) ((i >> 8) & 0xFF); data[1] = (byte) ((i >> 16) & 0xFF); data[0] = (byte) ((i >> 24) & 0xFF); return data; } else { byte[] data = new byte[4]; data[0] = (byte) (i & 0xFF); data[1] = (byte) ((i >> 8) & 0xFF); data[2] = (byte) ((i >> 16) & 0xFF); data[3] = (byte) ((i >> 24) & 0xFF); return data; } } 

Further information on the bigEndian parameter can be found here: http://en.wikipedia.org/wiki/Endianness

+3
Nov 26 '08 at 19:12
source share

create a 4-byte array and copy int into the array in 4 steps, with bitwise AND and a bit, as Paulo said.

But remember that block algorithms such as AES and DES work with 8 or 16 byte blocks, so you will need to place the array according to what the algorithm needs. Perhaps leave the first 4 bytes of an 8-byte array as 0, and the remaining 4 bytes contain an integer.

0
Dec 22 '08 at 22:50
source share

Just use:

  Integer.toString(int).getBytes(); 

Make sure you use the original int and getBytes () will return an array of bytes. There is no need to do anything even more complicated.

To go back:

  Integer.parseInt(encryptedString); 
0
Jun 23 '15 at 15:09
source share



All Articles