Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
//License from project: Open Source License 

import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;

import javax.crypto.NoSuchPaddingException;
import javax.crypto.SecretKey;
import javax.crypto.spec.IvParameterSpec;
import java.security.*;

public class Main {
    /**
     * Decrypts a encrypted and encoded message given a key.
     * @param cipherBytes
     * @param key
     * @return
     * @throws NoSuchAlgorithmException
     * @throws NoSuchAlgorithmException
     * @throws InvalidAlgorithmParameterException
     * @throws IllegalBlockSizeException
     * @throws BadPaddingException
     * @throws InvalidAlgorithmParameterException
     * @throws NoSuchPaddingException
     * @throws InvalidKeyException
     */
    public static byte[] decryptMessage(byte[] cipherBytes, SecretKey key) throws NoSuchAlgorithmException,
            NoSuchAlgorithmException, InvalidAlgorithmParameterException, IllegalBlockSizeException,
            BadPaddingException, InvalidAlgorithmParameterException, NoSuchPaddingException, InvalidKeyException {
        Cipher cipher = Cipher.getInstance("AES");
        //ipher cipher = Cipher.getInstance("AES/CBC/PKCS7PADDING");
        byte[] init = new byte[128 / 8];
        SecureRandom secureRandom = new SecureRandom();
        //secureRandom.nextBytes(init);
        for (int i = 0; i < 16; i++)
            init[i] = 0;
        cipher.init(Cipher.DECRYPT_MODE, key, new IvParameterSpec(init));
        byte[] textBytes = cipher.doFinal(cipherBytes);
        return textBytes;
    }
}