• TripleDES golang/python/ts 实现方式


    1.TripleDES

    TripleDES或3DES 是一种加密技术,通过应用数据加密标准(DES)算法的三次传递来增强数据的安全性,属于对称加密算法;

    2. 实现方式

    2.1. react-ts

    function encrypt(text){
    	import CryptoJS from 'crypto-js'
    	const key = "qwertyuiopasdfghjklzxcvb"
    	const text ="yuanli"
    	const iv = "01234567"
    	const result = CryptoJS.TripleDES.encrypt(text, CryptoJS.enc.Utf8.parse(key), {
    		mode: CryptoJS.mode.CBC,
    		padding: CryptoJS.pad.Pkcs7,
    		iv: CryptoJS.enc.Utf8.parse(iv)
    	})
    	return result
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    2.2.golang (test well)

    import (
    	"bytes"
    	"crypto/cipher"
    	"crypto/des"
    	"encoding/base64"
    	"fmt"
    )
    
    func Encrypt(plain string) (string, error) {
    	key := "qwertyuiopasdfghjklzxcvb"
    	block, err := des.NewTripleDESCipher([]byte(key))
    	if err != nil {
    		return "", err
    	}
    	input := []byte(plain)
    	input = PKCS5Padding(input, block.BlockSize())
    	iv := "01234567"
    	blockMode := cipher.NewCBCEncrypter(block, []byte(iv))
    	crypted := make([]byte, len(input))
    	blockMode.CryptBlocks(crypted, input)
    	return base64.StdEncoding.EncodeToString(crypted), err
    }
    
    func PKCS5Padding(input []byte, blockSize int) []byte {
    	padding := blockSize - len(input)%blockSize
    	padText := bytes.Repeat([]byte{byte(padding)}, padding)
    	return append(input, padText...)
    }
    
    func main() {
    	s, err := Encrypt("yuanli")
    	if err != nil {
    		panic(err)
    	}
    	fmt.Println(s) # Z82teOQw6FE=
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37

    2.3. python

    pip install pycryptodome
    pip install Crypto
    
    • 1
    • 2
    import base64
    
    from Crypto.Cipher import DES3
    
    class TripleDESEncryption():
        def __init__(self, key):
            self.key = key
            self.iv = b'01234567'
            self.length = DES3.block_size
            self.des3 = DES3.new(self.key, DES3.MODE_CBC, self.iv)
            self.unpad = lambda date: date[0:-ord(date[-1])]
    
        def pad(self,text):
            count = len(text.encode('utf-8'))
            add = self.length - (count%self.length)
            entext = text + (chr(add) * add)
            return entext
    
        def encrypt(self, text):
            res = self.des3.encrypt(self.pad(text).encode('utf-8'))
            message = str(base64.b64encode(res), encoding="utf8")
            return message
    
    if __name__ == '__main__':
        t = TripleDESEncryption("qwertyuiopasdfghjklzxcvb")
        e = t.encrypt("yuanli")
        print(e) # Z82teOQw6FE=
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
  • 相关阅读:
    gitlab 12.7恢复
    WebGIS外包开发流程
    网络编程的学习
    4款企业常用的工时管理系统盘点
    03 【柱状图】
    docker 数据持久化
    Spring依赖注入、循环依赖——三级缓存
    如何在Python中操作MySQL?
    【webrtc】老版本的OnReceivedPayloadData 及VCMPacket
    数据库临时表(Temporary Table)
  • 原文地址:https://blog.csdn.net/Yuan_xii/article/details/134299948