码迷,mamicode.com
首页 > 其他好文 > 详细

des/3des

时间:2018-08-31 10:52:19      阅读:201      评论:0      收藏:0      [点我收藏+]

标签:pad   base64   +=   pcr   targe   efault   rip   utf8   填充   

一、python

1、 des3

python平台的DES3 + base64 加密解密, 有两个常用的库pycrypto和pyDes

1)pycrypto

des3.py

#coding=utf-8
from Crypto.Cipher import _DES3
import base64
import json
BS = _DES3.block_size

def pad(s):
    return s + (BS - len(s) % BS) * chr(BS - len(s) % BS)
    #定义 padding 即 填充 为PKCS7

def unpad(s):
    return s[0:-ord(s[-1])]

class prpcrypt():

    def __init__(self, key):
        self.key = key
        self.mode = _DES3.MODE_CBC #模式为CBC       
        self.iv  = IV #self.iv 为 IV 即偏移量,ECB模式不使用IV

    # DES3的加密模式为CBC
    def encrypt(self, text):
        text = pad(text)
        cryptor = _DES3.new(self.key, self.mode, self.iv)
        
        x = len(text) % 8
        if x != 0:
            text = text + \0‘ * (8 - x)  # 不满16,32,64位补0
        # print(text)
        self.ciphertext = cryptor.encrypt(text)
        return base64.standard_b64encode(self.ciphertext).decode("utf-8")

    # DES3的解密模式为CBC
    def decrypt(self, text):
        cryptor = _DES3.new(self.key, self.mode, self.iv)
        de_text = base64.standard_b64decode(text)
        plain_text = cryptor.decrypt(de_text)
        # st = str(plain_text.decode("utf-8")).rstrip(‘\0‘)         
        # out = unpad(st)
        # return out
        #上面注释内容解密如果运行报错,就注释掉试试
        return plain_text

if __name__ == __main__:
    #ECB模式不使用IV 
    IV=b00000000
    des3 = prpcrypt(‘123456789012345678901234‘)  # 自己设定的密钥
    tick = {
        "protocolHead": "gis_fl",
        "protocolType": 1000000
    }
    js = json.dumps(tick)   #字典转str,再加密
    #js = str(tick)
    print  type(js)
    e = des3.encrypt(js)    # 加密内容
    d = des3.decrypt(e)     #解密内容
    print e                 #加密后
    print d                 #解密后

 参考https://www.cnblogs.com/qq405921147/p/9176691.html

2)pyDes

参考

pyDes库

https://www.cnblogs.com/txw1958/archive/2012/07/20/python-des-3des.html
https://blog.csdn.net/sbdxxcjh/article/details/38460409

des3 + base64

https://www.jb51.net/article/112549.htm

 

二、nodejs

1、 des3加密

在线工具 http://tool.chacuo.net/crypt3des

感谢这位哥,轻轻一百度就有现成的

http://mygo.iteye.com/blog/2018882

var assert = require(‘assert‘);  
    var crypto = require(‘crypto‘);  
      
    function test_des(param) {  
        var key = new Buffer(param.key);  
        var iv = new Buffer(param.iv ? param.iv : 0)  
        var plaintext = param.plaintext  
        var alg = param.alg  
        var autoPad = param.autoPad  
          
        //encrypt  
        var cipher = crypto.createCipheriv(alg, key, iv);  
        cipher.setAutoPadding(autoPad)  //default true  
        var ciph = cipher.update(plaintext, ‘utf8‘, ‘hex‘);  
        ciph += cipher.final(‘hex‘);  
        console.log(alg, ciph)  
      
        //decrypt  
        var decipher = crypto.createDecipheriv(alg, key, iv);  
        decipher.setAutoPadding(autoPad)  
        var txt = decipher.update(ciph, ‘hex‘, ‘utf8‘);  
        txt += decipher.final(‘utf8‘);      
        assert.equal(txt, plaintext, ‘fail‘);  
    }  
      
    test_des({  
        alg: ‘des-ecb‘,  
        autoPad: true,  
        key: ‘01234567‘,  
        plaintext: ‘1234567812345678‘,  
        iv: null  
    })  
      
    test_des({  
        alg: ‘des-cbc‘,  
        autoPad: true,  
        key: ‘01234567‘,  
        plaintext: ‘1234567812345678‘,  
        iv: ‘12345678‘  
    })  
      
    test_des({  
        alg: ‘des-ede3‘,    //3des-ecb  
        autoPad: true,  
        key: ‘0123456789abcd0123456789‘,  
        plaintext: ‘1234567812345678‘,  
        iv: null  
    })  
      
    test_des({  
        alg: ‘des-ede3-cbc‘,    //3des-cbc  
        autoPad: true,  
        key: ‘0123456789abcd0123456789‘,  
        plaintext: ‘1234567812345678‘,  
        iv: ‘12345678‘  
    })

我只用到了3des-ecb,数据格式是base64, 将加密和解密两个函数的数据格式从hex改成base64就OK了。

封装一下

 

var crypto = require(‘crypto‘);  

var key_qbox10 = ‘123456789012345678901234‘;

function des3Encrypt(param) {  
    var key = new Buffer(param.key);  
    var iv = new Buffer(param.iv ? param.iv : 0)  
    var plaintext = param.plaintext  
    var alg = param.alg  
    var autoPad = param.autoPad  
 
    var cipher = crypto.createCipheriv(alg, key, iv);  
    cipher.setAutoPadding(autoPad)
    var ciph = cipher.update(plaintext, ‘utf8‘, ‘base64‘);  
    ciph += cipher.final(‘base64‘);   
    return ciph;
};  

function des3Decrypt(param) {  
    var key = new Buffer(param.key);  
    var iv = new Buffer(param.iv ? param.iv : 0)  
    var plaintext = param.plaintext  
    var alg = param.alg  
    var autoPad = param.autoPad  
 
    var decipher = crypto.createDecipheriv(alg, key, iv);  
    decipher.setAutoPadding(autoPad)  
    var txt = decipher.update(plaintext, ‘base64‘, ‘utf8‘);  
    txt += decipher.final(‘utf8‘);      
    return txt;  
};

exports.decode=function(data){
    var para = {
        alg:‘des-ede3‘,
        autoPad:true,
        plaintext:data,
        iv:null,
        key:key_qbox10
    };
    var decode_str = des3Decrypt(para);
    return decode_str;
}

exports.encode =function(data){
    var para = {
        alg:‘des-ede3‘,
        autoPad:true,
        plaintext:data,
        iv:null,
        key:key_qbox10
    };
    var encode_str = des3Encrypt(para);
    return encode_str;
}

 

demo:

decode

function recv_routine(data){

    var obj = JSON.parse(des3.decode(data));
    //console.log("<<<<<<<<<<<<<<<<<<<<"+JSON.stringify(obj));
    //response tick
    if( obj.protocolType === 1000000 ){
        console.log("<<<<<<<<<<<<<<<<<<<<"+JSON.stringify(obj));
    }
}

encode

client.connection.sendUTF(des3.encode(JSON.stringify(proto.tick)));

 

des/3des

标签:pad   base64   +=   pcr   targe   efault   rip   utf8   填充   

原文地址:https://www.cnblogs.com/dong1/p/9563840.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!