zoukankan      html  css  js  c++  java
  • AES对称加密算法

    package cn.jsonlu.passguard.utils;
    
    
    import org.apache.commons.codec.binary.Base64;
    
    import javax.crypto.Cipher;
    import javax.crypto.spec.SecretKeySpec;
    
    /**
     * @author JsonLu
     * @email jsonlu@qq.com
     * @since 2016/1/5 17:34
     */
    public class AESUtil {
    
        /**
         * 密钥
         */
        public static final String KEY = "0123456789012345";
    
        /**
         * @param input
         * @param key
         * @return
         */
        public static String encrypt(String input, String key) {
            byte[] crypted = null;
            try {
                SecretKeySpec skey = new SecretKeySpec(key.getBytes(), "AES");
                Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
                cipher.init(Cipher.ENCRYPT_MODE, skey);
                crypted = cipher.doFinal(input.getBytes());
            } catch (Exception e) {
                System.out.println(e.toString());
            }
            return new String(Base64.encodeBase64(crypted));
        }
    
        /**
         * @param input
         * @param key
         * @return
         */
        public static String decrypt(String input, String key) {
            byte[] output = null;
            try {
                SecretKeySpec skey = new SecretKeySpec(key.getBytes(), "AES");
                Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
                cipher.init(Cipher.DECRYPT_MODE, skey);
                output = cipher.doFinal(Base64.decodeBase64(input));
            } catch (Exception e) {
                System.out.println(e.toString());
            }
            return new String(output);
        }
    
        public static void main(String[] args) {
            String key = "1234567891234567";
            String data = "i_love_coder";
            System.out.println(AESUtil.decrypt(AESUtil.encrypt(data, key), key));
            System.out.println(AESUtil.encrypt(data, key));
        }
    }
  • 相关阅读:
    宾得镜头资料
    先感动自己才能感动别人
    关于单反相机中的APSC
    K10D和凤凰镜头
    Vista的新快捷键
    微软雅黑字体“演”字变“漠”字的bug
    Windows XP无线零配置服务
    剑走偏锋,用XP的启动管理来搞定Vista、XP双系统
    BCB中的目录选择对话框的实现
    MagicAjax 使用
  • 原文地址:https://www.cnblogs.com/Jsonlu/p/5211881.html
Copyright © 2011-2022 走看看