开发者

Decryption of AES in Java

French : slt, comment je peut faire le dechiffrement en utilisant AES c'est mon code :

in English : Hi, how can I do the decryption using AES? This is my code:

public class NewClass1{

    private Key key;

    private void generateKey() throws NoSuchAlgorithmException{
        KeyGenerator generator;
        generator = KeyGenerator.getInstance("AES");
        generator.init(new SecureRandom());
        key = generator.generateKey();
    }

    private String decrypt(String encrypted) throws InvalidKeyException,
        NoSuchAlgorithmException,
        NoSuchPaddingException,
        IllegalBlockSizeException,
        BadPaddingException,
        IOException{

        Cipher cipher = Cipher.getInstance("AES");
        cipher.init(Cipher.DECRYPT_MODE开发者_JAVA技巧, key);
        BASE64Decoder decoder = new BASE64Decoder();
        byte[] raw = decoder.decodeBuffer(encrypted);
        byte[] stringBytes = cipher.doFinal(raw);
        // converts the decoded message to a String
        String clear = new String(stringBytes);
        return clear;
    }

    public NewClass1(String encrypted){
        try{
            System.out.println("encrypted message: " + encrypted);
            generateKey();
            String decrypted = decrypt(encrypted);
            System.out.println("decrypted message: " + decrypted);
        } catch(NoSuchAlgorithmException e){
            e.printStackTrace();
        } catch(NoSuchPaddingException e){
            e.printStackTrace();
        } catch(InvalidKeyException e){
            e.printStackTrace();
        } catch(UnsupportedEncodingException e){
            e.printStackTrace();
        } catch(IllegalBlockSizeException e){
            e.printStackTrace();
        } catch(BadPaddingException e){
            e.printStackTrace();
        } catch(IOException e){
            e.printStackTrace();
        }
    }

    public static void main(String[] args){
        new NewClass1("vbfhdhhhjhtrrrrrrrrrrrrrrjrdfes");
    }
}


Some of the very basics are wrong here. Your code implies the "vbfhdhhhjhtrrrrrrrrrrrrrrjrdfes" is the base64 encoding of the result of an AES encryption. It's not. You code also generates a random decryption key. That can't work. I'd suggest starting with some Wikipedia articles on encryption.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜