AES加密解密

public static String Encrypt(String sSrc,String sKey){
    try {
        byte[] raw = sKey.getBytes();
        SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
        //"算法/模式/补码方式"
        Cipher cipher = Cipher.getInstance("AES");
        cipher.init(Cipher.ENCRYPT_MODE, skeySpec);
        byte[] encrypted = cipher.doFinal(sSrc.getBytes());
        //此处使用BASE64做转码功能,同时能起到2次加密的作用。
        return new Base64().encodeToString(encrypted);
    }catch (Exception e){
        e.printStackTrace();
    }
    return "";
}

public static String Decrypt(String value,String sKey) {
    try {
        byte[] key = sKey.getBytes();
        SecretKeySpec skeySpec = new SecretKeySpec(key, "AES");
        Cipher dcipher = Cipher.getInstance("AES");
        dcipher.init(Cipher.DECRYPT_MODE, skeySpec);
        return new String(dcipher.doFinal(Base64.decodeBase64(value)));
    } catch (Exception e) {
        e.printStackTrace();
    }
    return "";
}

你可能感兴趣的:(java)