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);

        }

  • 相关阅读:
    数据库基础+重置root密码
    Bug定级
    测试用例和测试方法
    测试基础
    HDOJ-1010 Tempter of the Bone(dfs)
    POJ
    HDU-2089 不要62 (数位DP)
    Happy!
    LOJ-1422 万圣节服装
    数字三角形(数塔问题)
  • 原文地址:https://www.cnblogs.com/jincieryi/p/9630052.html
Copyright © 2011-2022 走看看