Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using java to encrypt integers

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

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

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

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

Please let me know.

Thanks in advance.

jbu

like image 292
jbu Avatar asked Nov 26 '08 19:11

jbu


People also ask

Which is the best encryption algorithm in Java?

The Advanced Encryption Standard (AES) is a widely used symmetric-key encryption algorithm.

How can we encrypt the password using Java?

Password-Based Encryption using Salt and Base64: The password-based encryption technique uses plain text passwords and salt values to generate a hash value. And the hash value is then encoded as a Base64 string. Salt value contains random data generated using an instance of Random class from java. util package.

How does encryption work in Java?

There are 2 key based encryption algorithms: Symmetric and Asymmetric algorithms. There are various cryptographic parameters which need to be configured correctly for a crypto-system to be secured; these include key size, mode of operation, padding scheme, IV, etc. For symmetric encryption use the AES algorithm.


3 Answers

You can turn ints into a byte[] using a DataOutputStream, like this:

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

Then to decrypt it later:

byte[] decrypted = decrypt (data);
ByteArrayInputStream bais = new ByteArrayInputStream (data);
DataInputStream dis = new DataInputStream (bais);
int j = dis.readInt();
like image 104
jodonnell Avatar answered Sep 20 '22 18:09

jodonnell


You can also use BigInteger for conversion:

 BigInteger.valueOf(integer).toByteArray();
like image 39
asalamon74 Avatar answered Sep 19 '22 18:09

asalamon74


Just use NIO. It's designed for this specific purpose. ByteBuffer and IntBuffer will do what you need quickly, efficiently, and elegantly. It'll handle big/little endian conversion, "direct" buffers for high performance IO, and you can even mix data types into the byte buffer.

Convert integers into bytes:

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

Convert bytes into integers:

ByteBuffer bbuffer = ByteBuffer.wrap(rawBytes);
IntBuffer ibuffer = bbuffer.asIntBuffer();
while(ibuffer.hasRemaining())
   System.out.println(ibuffer.get());      //also has bulk operators
like image 35
James Schek Avatar answered Sep 22 '22 18:09

James Schek