Encrypt a byte array given the secret key spec - Java Security

Java examples for Security:Key

Description

Encrypt a byte array given the secret key spec

Demo Code


//package com.java2s;

import javax.crypto.Cipher;

import javax.crypto.spec.SecretKeySpec;

public class Main {
    /**//from   w  ww. j a  va  2 s  . c o  m
     * Encrypt a byte array given the secret key spec
     * @param message
     * @param skeyspec
     * @return
     * @throws Exception
     */
    public static byte[] encrypt(byte[] message, SecretKeySpec skeyspec)
            throws Exception {
        Cipher cipher = Cipher.getInstance(skeyspec.getAlgorithm());
        cipher.init(Cipher.ENCRYPT_MODE, skeyspec);
        byte[] encrypted = cipher.doFinal(message);
        return encrypted;
    }
}

Related Tutorials