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

<p>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?</p>

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

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

<p>Please let me know.</p>

<p>Thanks in advance.</p>

<p>jbu</p>

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();
```
```