Mr_老冷 发表于 2022-11-25 18:10:58

【源码】【通用】EC官方DES加解密java源码

对应的js暴露函数是:
encodeDecoder.desEncrypt DES加密​
encodeDecoder.desDecrypt DES解密​
encodeDecoder.des3Encrypt 3DES加密​
encodeDecoder.des3Decrypt 3DES解密

源码:


import android.util.Base64;

import java.io.IOException;
import java.security.InvalidKeyException;
import java.security.Key;
import java.security.SecureRandom;

import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;
import javax.crypto.spec.DESedeKeySpec;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;

public class DesUtil {

    private final static String DES = "DES";


    /**
   * Description 根据键值进行加密
   *
   * @param data
   * @param key加密键byte数组
   * @return
   * @throws Exception
   */
    public static String desCrypt(String data, String key) throws Exception {
      byte[] bt = encrypt(data.getBytes(), key.getBytes());
      return android.util.Base64.encodeToString(bt, 0);
    }

    /**
   * Description 根据键值进行解密
   *
   * @param data
   * @param key加密键byte数组
   * @return
   * @throws IOException
   * @throws Exception
   */
    public static String desDecrypt(String data, String key) throws Exception {
      if (data == null)
            return null;
      byte[] buf = android.util.Base64.decode(data, Base64.DEFAULT);
      byte[] bt = decrypt(buf, key.getBytes());
      return new String(bt);

    }

    /**
   * Description 根据键值进行加密
   *
   * @param key 加密键byte数组
   * @return
   * @throws Exception
   */
    private static byte[] encrypt(byte[] data, byte[] key) throws Exception {
      // 生成一个可信任的随机数源
      SecureRandom sr = new SecureRandom();
      // 从原始密钥数据创建DESKeySpec对象
      DESKeySpec dks = new DESKeySpec(key);

      // 创建一个密钥工厂,然后用它把DESKeySpec转换成SecretKey对象
      SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(DES);
      SecretKey securekey = keyFactory.generateSecret(dks);

      // Cipher对象实际完成加密操作
      Cipher cipher = Cipher.getInstance(DES);

      // 用密钥初始化Cipher对象
      cipher.init(Cipher.ENCRYPT_MODE, securekey, sr);

      return cipher.doFinal(data);
    }

    /**
   * Description 根据键值进行解密
   *
   * @param data
   * @param key加密键byte数组
   * @return
   * @throws Exception
   */
    private static byte[] decrypt(byte[] data, byte[] key) throws Exception {
      // 生成一个可信任的随机数源
      SecureRandom sr = new SecureRandom();

      // 从原始密钥数据创建DESKeySpec对象
      DESKeySpec dks = new DESKeySpec(key);

      // 创建一个密钥工厂,然后用它把DESKeySpec转换成SecretKey对象
      SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(DES);
      SecretKey securekey = keyFactory.generateSecret(dks);

      // Cipher对象实际完成解密操作
      Cipher cipher = Cipher.getInstance(DES);

      // 用密钥初始化Cipher对象
      cipher.init(Cipher.DECRYPT_MODE, securekey, sr);

      return cipher.doFinal(data);
    }

    public static String des3Crypto(String src, String password) throws Exception {

      DESedeKeySpec deskey = new DESedeKeySpec(password.getBytes());
      SecretKeyFactory skf = SecretKeyFactory.getInstance("DESede");
      Key key = skf.generateSecret(deskey);
      Cipher cipher1 = Cipher.getInstance("DESede/CBC/PKCS5Padding");
      IvParameterSpec iv = new IvParameterSpec("12345678".getBytes());
      cipher1.init(Cipher.ENCRYPT_MODE, key, iv);
      byte[] bbb = cipher1.doFinal(src.getBytes());

      if (bbb != null) {
            bbb = android.util.Base64.encode(bbb, 0);
            return new String(bbb);
      }
      return null;
    }

    public static String des3Decrypt(String src, String password) throws Exception {
      byte[] d = (android.util.Base64.decode(src, 0));
      if (d == null) {
            return null;
      }
      DESedeKeySpec deskey = new DESedeKeySpec(password.getBytes());
      SecretKeyFactory skf = SecretKeyFactory.getInstance("DESede");
      Key key = skf.generateSecret(deskey);
      Cipher cipher1 = Cipher.getInstance("DESede/CBC/PKCS5Padding");
      IvParameterSpec iv = new IvParameterSpec("12345678".getBytes());
      cipher1.init(Cipher.DECRYPT_MODE, key, iv);
      byte[] bbb = cipher1.doFinal(d);
      if (bbb == null) {
            return null;
      }
      return new String(bbb);
    }


}

jiucai123 发表于 2023-4-3 23:16:27

++++++++++++

q2845821496 发表于 2023-4-8 13:50:48

e9nHKqqJe9nHKqqJe9nHKqqJ

373737 发表于 2023-9-1 14:29:46

6666666666666666666666666666666666
页: [1]
查看完整版本: 【源码】【通用】EC官方DES加解密java源码