zoukankan      html  css  js  c++  java
  • DES加解密

    @Test

    public void test() throws Exception {

           String data = "lala";

            String key = "helloworld";

           String result = SystemUtil.encryptDES(data, key);

           Assert.assertEquals(SystemUtil.decryptDES(result, key), data);

           System.out.println(result);

      }

    /**

         * DES

         *  加密

         * @param data

         * @param key

         * @return

         * @throws Exception

         */

        public static byte[] encryptDES(byte[] data, byte[] key) throws Exception {

           SecureRandom sr = new SecureRandom();

           DESKeySpec dks = new DESKeySpec(key);

           SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(SystemUtil.KEY_ALGORITHM_DES);

           SecretKey securekey = keyFactory.generateSecret(dks);

          Cipher cipher = Cipher.getInstance(SystemUtil.KEY_ALGORITHM_DES);

           cipher.init(Cipher.ENCRYPT_MODE, securekey, sr);

           return cipher.doFinal(data);

        }

    /**

         * DES

         *解密

         * @param data

         * @param key

         * @return

         * @throws Exception

         */

        public static byte[] decryptDES(byte[] data, byte[] key) throws Exception {

           SecureRandom sr = new SecureRandom();

           DESKeySpec dks = new DESKeySpec(key);

           SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(SystemUtil.KEY_ALGORITHM_DES);

           SecretKey securekey = keyFactory.generateSecret(dks);

           Cipher cipher = Cipher.getInstance(SystemUtil.KEY_ALGORITHM_DES);

           cipher.init(Cipher.DECRYPT_MODE, securekey, sr);

           return cipher.doFinal(data);

        }

  • 相关阅读:
    js设计模式-工厂模式
    js设计模式-构造函数模式
    js设计模式-单例模式
    为什么要用事件委托and 为什么移动端click事件要设计延迟
    css属性支持
    翻译express
    无法点击,无法获取inputvalue值得原因
    cookie删除不掉的问题
    56.com面试
    angular.js phonecat翻译
  • 原文地址:https://www.cnblogs.com/jincieryi/p/9630052.html
Copyright © 2011-2022 走看看