TripleDES golang/python/ts 实现方式

2. 实现方式

2.1. react-ts

js 复制代码
function encrypt(text){
	import CryptoJS from 'crypto-js'
	const key = "saxbj%2xas"
	const text ="liyuan"
	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
}

2.2.golang (test well)

c 复制代码
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=
}

2.3. python

python 复制代码
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 = lamda 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
相关推荐
CodeCraft Studio几秒前
国产化Excel开发组件Spire.XLS教程:使用Python批量删除Excel分页符
开发语言·python·excel·python开发·spire.xls·excel api库·excel开发组件
free-elcmacom几秒前
深度学习<2>从“看单帧”到“懂故事”:视频模型的帧链推理,藏着机器读懂时间的秘密
人工智能·python·深度学习·音视频
ZAz_2 分钟前
DAY 44 Grad-CAM与Hook函数
python
高洁013 分钟前
基于Tensorflow库的RNN模型预测实战
人工智能·python·算法·机器学习·django
木子欢儿3 分钟前
在 Debian 13 上搭建一个 NTP (Network Time Protocol) 服务器
运维·服务器·开发语言·debian·php
weixin_462446236 分钟前
【实践原创】docker inspect --format 详解:Go 模板在 Docker 中的应用
docker·容器·golang
qq_381454999 分钟前
Go vs Java:极简主义与全能生态的终极对决
golang
多米Domi01112 分钟前
0x3f第九天复习(考研日)(10.57-14:00)
python·算法
凯子坚持 c13 分钟前
Qt 信号与槽机制深度解析
开发语言·qt
张彦峰ZYF13 分钟前
从路径抽象到安全归档 Python 文件组织实战
大数据·python·从路径抽象到安全归档·python 文件组织实战