【Android工具类】比DES加密更安全的算法——3DES加密算法

来源:互联网 发布:数据精灵微信版本 编辑:程序博客网 时间:2024/05/22 04:49

   转载请注明出处:http://blog.csdn.net/zhaokaiqiang1992

    在前面的文章里面,我们讨论了DES算法,同时也明白了如何才能保证不同平台下的加密和解密结果的一致性。但是DES作为出现了很长时间的一种加密算法,随着计算机运算能力的加强,DES加密容易被暴力破解,其安全性变得有点低。于是,为了增强数据的安全性,3DES算法就应运而生了。

    3DES,顾名思义,就是对DES加密算法的改进,3DES通过对每个数据进行3次DES加密,从而降低被破解的可能性。

     如果我们要使用3DES加密,需要以下几个步骤

    ①传入共同约定的密钥(keyBytes)以及算法(Algorithm),来构建SecretKey密钥对象

        SecretKey deskey = new SecretKeySpec(keyBytes, Algorithm);    

    ②根据算法实例化Cipher对象。它负责加密/解密

        Cipher c1 = Cipher.getInstance(Algorithm);    

    ③传入加密/解密模式以及SecretKey密钥对象,实例化Cipher对象

        c1.init(Cipher.ENCRYPT_MODE, deskey);    

    ④传入字节数组,调用Cipher.doFinal()方法,实现加密/解密,并返回一个byte字节数组

        c1.doFinal(src);

   

    具体的代码实现过程如下

package com.qust.rollcallstudent.utils;    import java.security.InvalidAlgorithmParameterException;  import java.security.Key;  import java.security.spec.AlgorithmParameterSpec;  import java.util.Locale;    import javax.crypto.Cipher;  import javax.crypto.SecretKeyFactory;  import javax.crypto.spec.DESKeySpec;  import javax.crypto.spec.IvParameterSpec;    /**  *   * @ClassName: com.qust.rollcallstudent.utils.DESUtil  * @Description: DES加密解密工具包  * @author zhaokaiqiang  * @date 2014-11-13 下午8:40:56  *   */  public class DESUtil {        public static final String ALGORITHM_DES = "DES/CBC/PKCS5Padding";        /**      * DES算法,加密      *       * @param data      *            待加密字符串      * @param key      *            加密私钥,长度不能够小于8位      * @return 加密后的字节数组,一般结合Base64编码使用      * @throws InvalidAlgorithmParameterException      * @throws Exception      */      public static String encode(String key, String data) {          if (data == null)              return null;          try {              DESKeySpec dks = new DESKeySpec(key.getBytes());              SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");              // key的长度不能够小于8位字节              Key secretKey = keyFactory.generateSecret(dks);              Cipher cipher = Cipher.getInstance(ALGORITHM_DES);              IvParameterSpec iv = new IvParameterSpec("12345678".getBytes());              AlgorithmParameterSpec paramSpec = iv;              cipher.init(Cipher.ENCRYPT_MODE, secretKey, paramSpec);              byte[] bytes = cipher.doFinal(data.getBytes());              return byte2String(bytes);          } catch (Exception e) {              e.printStackTrace();              return data;          }      }        /**      * DES算法,解密      *       * @param data      *            待解密字符串      * @param key      *            解密私钥,长度不能够小于8位      * @return 解密后的字节数组      * @throws Exception      *             异常      */      public static String decode(String key, String data) {          if (data == null)              return null;          try {              DESKeySpec dks = new DESKeySpec(key.getBytes());              SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");              // key的长度不能够小于8位字节              Key secretKey = keyFactory.generateSecret(dks);              Cipher cipher = Cipher.getInstance(ALGORITHM_DES);              IvParameterSpec iv = new IvParameterSpec("12345678".getBytes());              AlgorithmParameterSpec paramSpec = iv;              cipher.init(Cipher.DECRYPT_MODE, secretKey, paramSpec);              return new String(cipher.doFinal(byte2hex(data.getBytes())));          } catch (Exception e) {              e.printStackTrace();              return data;          }      }        /**      * 二行制转字符串      *       * @param b      * @return      */      private static String byte2String(byte[] b) {          StringBuilder hs = new StringBuilder();          String stmp;          for (int n = 0; b != null && n < b.length; n++) {              stmp = Integer.toHexString(b[n] & 0XFF);              if (stmp.length() == 1)                  hs.append('0');              hs.append(stmp);          }          return hs.toString().toUpperCase(Locale.CHINA);      }        /**      * 二进制转化成16进制      *       * @param b      * @return      */      private static byte[] byte2hex(byte[] b) {          if ((b.length % 2) != 0)              throw new IllegalArgumentException();          byte[] b2 = new byte[b.length / 2];          for (int n = 0; n < b.length; n += 2) {              String item = new String(b, n, 2);              b2[n / 2] = (byte) Integer.parseInt(item, 16);          }          return b2;      }    } 


0 0
原创粉丝点击