1. 程式人生 > 實用技巧 >python解密java加密的資料(DES)

python解密java加密的資料(DES)

python解密java加密的資料(DES)

java加解密工具是這個:

package com.sinoiov.eimp.common.util;

import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;
import java.io.IOException;
import java.security.SecureRandom; /** * DES加密 解密演算法 * */ public class SymmetricUtils { private final static String DES = "DES"; private final static String ENCODE = "GBK"; private final static String defaultKey = "netwxactive"; /** * 使用 預設key 加密 * @param data 待加密資料 *
@return * @throws Exception */ public static String encrypt(String data) throws Exception { byte[] bt = encrypt(data.getBytes(ENCODE), defaultKey.getBytes(ENCODE)); String strs = new BASE64Encoder().encode(bt); return strs; } /** * 使用 預設key 解密 *
@param data 待解密資料 * @return * @throws IOException * @throws Exception */ public static String decrypt(String data) throws IOException, Exception { if (data == null) { return null; } BASE64Decoder decoder = new BASE64Decoder(); byte[] buf = decoder.decodeBuffer(data); byte[] bt = decrypt(buf, defaultKey.getBytes(ENCODE)); return new String(bt, ENCODE); } /** * Description 根據鍵值進行加密 * @param data 待加密資料 * @param key 金鑰 * @return * @throws Exception */ public static String encrypt(String data, String key) throws Exception { byte[] bt = encrypt(data.getBytes(ENCODE), key.getBytes(ENCODE)); String strs = new BASE64Encoder().encode(bt); return strs; } /** * 根據鍵值進行解密 * @param data 待解密資料 * @param key 金鑰 * @return * @throws IOException * @throws Exception */ public static String decrypt(String data, String key) throws IOException, Exception { if (data == null) { return null; } BASE64Decoder decoder = new BASE64Decoder(); byte[] buf = decoder.decodeBuffer(data); byte[] bt = decrypt(buf, key.getBytes(ENCODE)); return new String(bt, ENCODE); } /** * Description 根據鍵值進行加密 * * @param data * @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); } }
View Code

網上都有的;

python解密:

  1. 首先安裝庫: pip installpyDes==2.0.1
  2. 程式碼
     1 import base64
     2 from pyDes import des, PAD_PKCS5, ECB
     3 
     4 
     5 class DesUtil(object):
     6     def __init__(self, mode=ECB, padmode=PAD_PKCS5, key=None, iv=None):
     7         self.padmode = padmode
     8         self.k = des(key, mode, iv, pad=None, padmode=padmode)
     9 
    10     def encrypt(self, content):
    11         if not isinstance(content, str):
    12             content = str(content)
    13         en = self.k.encrypt(content.encode("utf-8"), padmode=self.padmode)
    14         return base64.b64encode(en)
    15 
    16     def descrypy(self, content):
    17         de = self.k.decrypt(base64.b64decode(content), padmode=self.padmode)
    18         print("de:", de)
    19         return de.decode("utf-8", 'ignore')
    20 
    21 
    22 if __name__ == "__main__":
    23     read_to_descrypy = "sfasdfsdgsjgsjg"
    24     key = "xxx"
    25     des_util = DesUtil(key=key, iv=key)
    26 
    27     str_de = des_util.descrypy(read_to_descrypy)
    28     # print("加密結果", str_en)
    29     print("解密結果", str_de)
    30     print("str_de:", type(str_de))
    31     print("str_de:", type(eval(str_de)))
    32     print("str_de:", eval(str_de))

    其中read_to_descrypy是需要解密的字串;key是祕鑰(注意如果超過8位,務必擷取前8位即可)

  3. 這塊程式碼的侷限是包含的中文字元解密不出來,這也是de.decode("utf-8", 'ignore')中ignore的目的。因為我做的需求只涉及英文字母和數字,所以沒有進一步處理(我覺得應該是編碼問題,如果能對的上,應該能正確解密)。
  4. 以上