Here you can find the source of encryptBytes(byte[] data, byte[] key)
Parameter | Description |
---|---|
data | - Information to be encrypted. |
key | - Key to use for encryption. |
public static byte[] encryptBytes(byte[] data, byte[] key)
//package com.java2s; import java.security.InvalidKeyException; import java.security.NoSuchAlgorithmException; import javax.crypto.BadPaddingException; import javax.crypto.Cipher; import javax.crypto.IllegalBlockSizeException; import javax.crypto.NoSuchPaddingException; import javax.crypto.spec.SecretKeySpec; public class Main { /**//from ww w . j a v a 2 s . c o m * Encrypts data using the AES encryption cipher. * * @param data * - Information to be encrypted. * @param key * - Key to use for encryption. * @return The encrypted data. */ public static byte[] encryptBytes(byte[] data, byte[] key) { try { Cipher cipher = Cipher.getInstance("AES"); SecretKeySpec sKey = new SecretKeySpec(key, "AES"); cipher.init(Cipher.ENCRYPT_MODE, sKey); return cipher.doFinal(data); } catch (NoSuchAlgorithmException e) { e.printStackTrace(); } catch (NoSuchPaddingException e) { e.printStackTrace(); } catch (InvalidKeyException e) { e.printStackTrace(); } catch (IllegalBlockSizeException e) { e.printStackTrace(); } catch (BadPaddingException e) { e.printStackTrace(); } return null; } }