Python DES加密

时间:2016-11-17 03:34:36

标签: java python django encryption

我有一个项目,我们的提供商有自己的DES加密逻辑,但我使用django作为我的后端。我可以使用子进程运行java但我实际上计划将该java代码转换为python ..

这是我的代码:

import java.io.*;
import java.security.SecureRandom;
import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;
import sun.misc.BASE64Encoder;
import sun.misc.BASE64Decoder;

public class PHPDESEncrypt {
    String key;
    public PHPDESEncrypt() {

    }
    public PHPDESEncrypt(String key) {
        this.key = key;
    }

    public byte[] desEncrypt(byte[] plainText) throws Exception {
        SecureRandom sr = new SecureRandom();
        DESKeySpec dks = new DESKeySpec(key.getBytes());
        SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
        SecretKey key = keyFactory.generateSecret(dks);
        Cipher cipher = Cipher.getInstance("DES");
        cipher.init(Cipher.ENCRYPT_MODE, key, sr);
        byte data[] = plainText;
        byte encryptedData[] = cipher.doFinal(data);
        return encryptedData;
    }

    public String encrypt(String input) throws Exception {
        return base64Encode(desEncrypt(input.getBytes())).replaceAll("\\s*", "");
    }

    public String base64Encode(byte[] s) {
        if (s == null) return null;
        BASE64Encoder b = new BASE64Encoder();
        return b.encode(s);
    }

    public static void main(String args[]) {
        try {
            PHPDESEncrypt d = new PHPDESEncrypt(args[0]);
            String p=d.encrypt(args[1]);
            System.out.println(p);
        }
        catch (Exception e) {
            e.printStackTrace();
        }
    }

    public String getKey() {
        return key;
    }

    public void setKey(String key) {
        this.key = key;
    }
}

有关如何转换此内容的任何建议?像图书馆或在线工具?或者如果有足够好的人可以轻松转换这个

会更好

我在java中运行它:

java PHPDESEncrypt "3r108w5A" "cagent=sample/\\\\/loginname=sample/\\\\/password=password/\\\\/method=ca/\\\\/actype=0/\\\\/cur=USD"

结果应为:

lt5tEqnzRCx67vJ8j3Ap5zIVZYkT01Ho+irM1NPdReJqwyrP9vlypDXvExx9sCOEJPcPCET0aPpKFkW1punRxP/uD8IFM1j4umwLuJpxPQTOdwJsbuuRmhmkFml6l3OV

2 个答案:

答案 0 :(得分:3)

将pyDes与python3一起使用,这是演示代码:

def des_ecb_encode(source, key):
    des_obj = des(key, ECB, IV=None, pad=None, padmode=PAD_PKCS5)
    des_result = des_obj.encrypt(source)
    return base64.encodestring(des_result)


if __name__ == '__main__':

    src = b'cagent=81288128/\\\\/method=tc'
    key = b'12341234'
    encrypted = des_ecb_encode(src, key)
    print('encrypted: ', encrypted)

它会打印

encrypted:  b'IGcOAYEQN88F1NFLtBOK23PMeg42F7r8jchYOmglMAs=\n'

答案 1 :(得分:0)

我已经使用pycrypto回答了这里的代码

from Crypto.Cipher import DES
from base64 import b64encode

def pad(s):
    return s + (DES.block_size - len(s) % DES.block_size) * \
        chr(DES.block_size - len(s) % DES.block_size)


def get_params(data):
    params = ''
    if not data:
        return params
    for key, value in data.iteritems():
        if key and value:
            params += '{0}={1}/\\\/'.format(key, value)
    return params[:-4]


def des_encrypt(plain_text):
        key = '3r108w5A'
        des = DES.new(key)
        return b64encode(des.encrypt(pad(plain_text)))


create_params = {'cagent': 'sample',
                 'loginname': 'sample',
                 'password': 'password',
                 'method': 'ca',
                 'actype': '0',
                 'cur': 'USD'}

params = des_encrypt(get_params(create_params))