加密库.js

    1. package com.dodonew.online.util;
    2. import android.util.Base64;
    3. import java.security.MessageDigest;
    4. import javax.crypto.Cipher;
    5. import javax.crypto.SecretKey;
    6. import javax.crypto.SecretKeyFactory;
    7. import javax.crypto.spec.DESKeySpec;
    8. import javax.crypto.spec.IvParameterSpec;
    9. public class DesSecurity {
    10. Cipher deCipher;
    11. Cipher enCipher;
    12. public DesSecurity(String key, String iv) throws Exception {
    13. if (key == null) {
    14. throw new NullPointerException("Parameter is null!");
    15. }
    16. InitCipher(key.getBytes(), iv.getBytes());
    17. }
    18. private void InitCipher(byte[] secKey, byte[] secIv) throws Exception {
    19. MessageDigest md = MessageDigest.getInstance("MD5");
    20. md.update(secKey);
    21. SecretKey key = SecretKeyFactory.getInstance("DES").generateSecret(new DESKeySpec(md.digest()));
    22. IvParameterSpec iv = new IvParameterSpec(secIv);
    23. this.enCipher = Cipher.getInstance("DES/CBC/PKCS5Padding");
    24. this.deCipher = Cipher.getInstance("DES/CBC/PKCS5Padding");
    25. this.enCipher.init(1, key, iv);
    26. this.deCipher.init(2, key, iv);
    27. }
    28. public String encrypt64(byte[] data) throws Exception {
    29. return Base64.encodeToString(this.enCipher.doFinal(data), 0);
    30. }
    31. public byte[] decrypt64(String data) throws Exception {
    32. return this.deCipher.doFinal(Base64.decode(data, 0));
    33. }
    34. }
    1. function DES_Encrypt() {
    2. let iv = CryptoJS.enc.Utf8.parse("32028092");
    3. let key = CryptoJS.enc.Hex.parse(CryptoJS.MD5("65102933").toString());
    4. var result = CryptoJS.DES.encrypt(rowdata, key, {
    5. iv: iv,
    6. mode: CryptoJS.mode.CBC,
    7. padding: CryptoJS.pad.Pkcs7
    8. }).toString();
    9. return result;
    10. }

    这个apk的重点是对key的解析:

    1. MessageDigest md = MessageDigest.getInstance("MD5");
    2. md.update(secKey);
    3. SecretKey key = SecretKeyFactory.getInstance("DES").generateSecret(new DESKeySpec(md.digest()));

    这儿是把传入的secKey直接md5得出的结果并没有hex解析就放进SecretKeyFactory进行解析
    另外就是,解密的时候,记得吧“\n”删掉
    image.png