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

        }

  • 相关阅读:
    yii分页
    ajax分页
    批删,全选
    网站开发的愿景
    margin collapse 坍塌
    URI URL URN
    Servlet
    Http请求
    进程间通信
    网络编程
  • 原文地址:https://www.cnblogs.com/jincieryi/p/9630052.html
Copyright © 2011-2022 走看看