Decrypt a byte array given the same secret key spec used to encrypt the message - Java Security

Java examples for Security:Key

Description

Decrypt a byte array given the same secret key spec used to encrypt the message

Demo Code


//package com.java2s;

import javax.crypto.Cipher;

import javax.crypto.spec.SecretKeySpec;

public class Main {
    /**//  www .  j a v  a2s .co  m
     * Decrypt a byte array given the same secret key spec used to encrypt the message
     *
     * @param message
     * @param skeyspec
     * @return
     * @throws Exception
     */
    public static byte[] decrypt(byte[] message, SecretKeySpec skeyspec)
            throws Exception {
        Cipher cipher = Cipher.getInstance(skeyspec.getAlgorithm());
        cipher.init(Cipher.DECRYPT_MODE, skeyspec);
        byte[] decrypted = cipher.doFinal(message);
        return decrypted;
    }
}

Related Tutorials