Android实现DES和3DES算法

import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;

public class DESUtils {
 private static byte[] parse(String str) {
  byte[] b = new byte[str.length() / 2];
  for (int i = 0, n = str.length(); i < n; i += 2) {
   b[i / 2] = (byte) (Integer.parseInt(str.substring(i, i + 2), 16));
  }
  return b;
 }

/**
  * 加密
  * @param src
  * @param password
  * @return
  */
 public static byte[] enCode(byte[] src, String password) {
  try {
   SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
   SecretKey key = keyFactory.generateSecret(new DESKeySpec(parse(password)));// 密钥
   Cipher cipher = Cipher.getInstance("DES");
   cipher.init(Cipher.ENCRYPT_MODE, key);
   return cipher.doFinal(src);// 明文
  } catch (Exception e) {
   e.printStackTrace();
  }
  return null;

}

/**
  * 解密
  * @param src
  * @param password
  * @return
  * @throws Exception
  */
 public static byte[] deCode(byte[] src, String password) throws Exception {
  SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
  SecretKey key = keyFactory.generateSecret(new DESKeySpec(parse(password)));// 密钥
  Cipher cipher = Cipher.getInstance("DES");
  cipher.init(Cipher.DECRYPT_MODE, key);
  return cipher.doFinal(src);// 密文
 }
}

3DES算法是进行了3次DES算法。
E是代表加密,D代表解密,K1,K2,K3代表3个密钥。加密解密都是用DES算法,一般来说密钥需要3个或两个密钥,如果3个密钥都相同,则变成了DES算法,如果对加密强度要求没有那么高,可以用两个密钥,对应的是K1和K3密钥相同。
3DES加密过程:EK1(加密)-->DK2(解密)-->EK3(加密)
3DES解密过程:DK3(解密)-->EK2(加密)-->DK1(解密)

3DES算法如下:

import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;

public class DES3Utils {

private static final String Algorithm = "DESede"; //定义 加密算法,可用 DES,DESede,Blowfish
   
    //keybyte为加密密钥,长度为24字节
    //src为被加密的数据缓冲区(源)
    public static byte[] encryptMode(byte[] keybyte, byte[] src) {
      try {
            //生成密钥
            SecretKey deskey = new SecretKeySpec(keybyte, Algorithm);

//加密
            Cipher c1 = Cipher.getInstance(Algorithm);
            c1.init(Cipher.ENCRYPT_MODE, deskey);
            return c1.doFinal(src);
        } catch (java.security.NoSuchAlgorithmException e1) {
            e1.printStackTrace();
        } catch (javax.crypto.NoSuchPaddingException e2) {
            e2.printStackTrace();
        } catch (java.lang.Exception e3) {
            e3.printStackTrace();
        }
        return null;
    }

//keybyte为加密密钥,长度为24字节
    //src为加密后的缓冲区
    public static byte[] decryptMode(byte[] keybyte, byte[] src) {     
    try {
            //生成密钥
            SecretKey deskey = new SecretKeySpec(keybyte, Algorithm);

//解密
            Cipher c1 = Cipher.getInstance(Algorithm);
            c1.init(Cipher.DECRYPT_MODE, deskey);
            return c1.doFinal(src);
        } catch (java.security.NoSuchAlgorithmException e1) {
            e1.printStackTrace();
        } catch (javax.crypto.NoSuchPaddingException e2) {
            e2.printStackTrace();
        } catch (java.lang.Exception e3) {
            e3.printStackTrace();
        }
        return null;
    }

//转换成十六进制字符串
    public static String byte2hex(byte[] b) {
        String hs="";
        String stmp="";

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/c1aa19a8ca74963383319fc31da2112c.html