Java: Cipher package (encrypt and decrypt). invalid key error

Posted by noinflection on Stack Overflow See other posts from Stack Overflow or by noinflection
Published on 2010-05-06T21:49:33Z Indexed on 2010/05/06 22:38 UTC
Read the original article Hit count: 349

Filed under:
|
|
|

Hello folks, i am doing a class with static methods to encrypt and decrypt a message using javax.crypto. I have 2 static methods that use ecipher and dcipher in order to do what they are supossed to do i need to initialize some variables (which are static also). But when i try to use it i get InvalidKeyException with the parameters i give to ecipher.init(...). I can't find why. Here is the code:

    private static byte[] raw = {-31,   17,   7,  -34,  59, -61, -60,  -16, 
                              26,   87, -35,  114,   0, -53,  99, -116, 
                             -82, -122,  68,   47,  -3, -17, -21,  -82, 
                             -50,  126, 119, -106, -119, -5, 109,   98};
    private static SecretKeySpec skeySpec;
    private static Cipher ecipher;
    private static Cipher dcipher;

    static {
        try {
            skeySpec = new SecretKeySpec(raw, "AES");
            // Instantiate the cipher
            ecipher = Cipher.getInstance("AES");
            dcipher = Cipher.getInstance("AES");
            ecipher.init(Cipher.ENCRYPT_MODE, skeySpec);
            dcipher.init(Cipher.DECRYPT_MODE, skeySpec);
        } catch (NoSuchAlgorithmException e) {
            throw new UnhandledException("No existe el algoritmo deseado", e);
        } catch (NoSuchPaddingException e) {
            throw new UnhandledException("No existe el padding deseado", e);
        } catch (InvalidKeyException e) {
            throw new UnhandledException("Clave invalida", e);
        }
    }

© Stack Overflow or respective owner

Related posts about java

Related posts about cipher