• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

Golang des.NewTripleDESCipher函数代码示例

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

本文整理汇总了Golang中crypto/des.NewTripleDESCipher函数的典型用法代码示例。如果您正苦于以下问题:Golang NewTripleDESCipher函数的具体用法?Golang NewTripleDESCipher怎么用?Golang NewTripleDESCipher使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。



在下文中一共展示了NewTripleDESCipher函数的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Golang代码示例。

示例1: desDecode

func desDecode(b []byte) ([]byte, error) {
	td, err := des.NewTripleDESCipher(deskey)
	if err != nil {
		logger.Println(err)
		return nil, err
	}
	// blockMode := cipher.NewCBCDecrypter(block, key)
	// orig := make([]byte, len(b))
	// blockMode.CryptBlocks(orig, b)
	// logger.Println(string(orig))

	n := len(b) / td.BlockSize()
	var rb []byte
	for i := 0; i < n; i++ {
		dst := make([]byte, td.BlockSize())
		td.Decrypt(dst, b[i*8:(i+1)*8])
		rb = append(rb, dst[:]...)
	}

	lastValue := int(rb[len(rb)-1])
	logger.Println(string(rb[0 : len(rb)-lastValue]))

	// 移除最后的0
	return bytes.TrimRight(rb, string([]byte{0})), nil
}
开发者ID:SeaSunOpenSource,项目名称:go-jxhttp,代码行数:25,代码来源:gm.go


示例2: init

func init() {
	var err error
	block3DES, err = des.NewTripleDESCipher(make([]byte, 24))
	if err != nil {
		panic(err)
	}
}
开发者ID:plobsing,项目名称:vitess,代码行数:7,代码来源:hash_index.go


示例3: NewBlock

func NewBlock(method string, keyfile string) (c cipher.Block, err error) {
	logger.Debugf("Crypt Wrapper with %s preparing.", method)

	file, err := os.Open(keyfile)
	if err != nil {
		return
	}
	defer file.Close()

	key := make([]byte, KEYSIZE)
	_, err = io.ReadFull(file, key)
	if err != nil {
		return
	}

	switch method {
	case "aes":
		c, err = aes.NewCipher(key)
	case "des":
		c, err = des.NewCipher(key)
	case "tripledes":
		c, err = des.NewTripleDESCipher(key)
	}
	return
}
开发者ID:nicecai,项目名称:goproxy,代码行数:25,代码来源:conn.go


示例4: tripleDESDecode

func tripleDESDecode(key, src string) string {
	if len(key) != 16 && len(key) != 24 {
		panic("key length error, must be 16 or 24")
	}

	data, err := coder.DecodeString(src)
	if err != nil {
		panic(err)
	}

	tripleDESKey := make([]byte, 0, 24)
	if len(key) == 16 {
		tripleDESKey = append(tripleDESKey, key[:16]...)
		tripleDESKey = append(tripleDESKey, key[:8]...)
	} else {
		tripleDESKey = append(tripleDESKey, key[:]...)
	}

	td, err := des.NewTripleDESCipher(tripleDESKey)
	if err != nil {
		panic(err)
	}

	n := len(data) / td.BlockSize()
	var rb []byte
	for i := 0; i < n; i++ {
		dst := make([]byte, td.BlockSize())
		td.Decrypt(dst, data[i*8:(i+1)*8])
		rb = append(rb, dst[:]...)
	}

	lastValue := int(rb[len(rb)-1])
	return string(rb[0 : len(rb)-lastValue])
}
开发者ID:nemowen,项目名称:golang,代码行数:34,代码来源:3des.go


示例5: Encrypt3DESECB

func Encrypt3DESECB(input []byte, key []byte) []byte {
	if len(key) != 24 {
		log.Panic("Key must be 24 bytes")
	}
	block, err := des.NewTripleDESCipher(key)
	if err != nil {
		log.Panic(err)
	}
	bs := block.BlockSize()
	//log.Printf("Block size: %d", bs)
	if len(input)%bs != 0 {
		log.Panic("Input should be a multiple of blocksize")
	}
	//log.Printf("Input length: %d", len(input))
	m := len(input) / bs
	//log.Printf("Going for %d cycles", m)
	data := []byte{}
	buf := make([]byte, bs)
	for i := 0; i < m; i++ {
		//log.Printf("%d: byte: %s", i, hex.EncodeToString(input[0:bs]))
		block.Encrypt(buf, input)
		input = input[bs:]
		data = append(data, buf...)
	}
	return data
}
开发者ID:tdeckers,项目名称:utcar,代码行数:26,代码来源:util.go


示例6: cipher3DES

func cipher3DES(key, iv []byte, isRead bool) interface{} {
	block, _ := des.NewTripleDESCipher(key)
	if isRead {
		return cipher.NewCBCDecrypter(block, iv)
	}
	return cipher.NewCBCEncrypter(block, iv)
}
开发者ID:randombit,项目名称:hacrypto,代码行数:7,代码来源:cipher_suites.go


示例7: main

func main() {
	plainText := []byte("Bob loves Alice.")

	// Key must be 24(8*3) bytes because tdes runs des 3 times
	// plain text -> (DES encrypt) -> (DES decrypt) -> (DES encrypt) -> cipher text
	// DES-EDE2 uses the same key for first and the third encrypt
	// DES-EDE3 uses different key for all encrypt/decrypt
	key := []byte("passw0rdpassw0rdpassw0rd")

	// Create Cipher block for TDES
	block, err := des.NewTripleDESCipher(key)
	if err != nil {
		fmt.Printf("err: %s", err)
		return
	}

	// Create initialization vector from rand.reader
	iv := make([]byte, des.BlockSize)
	if _, err := io.ReadFull(rand.Reader, iv); err != nil {
		fmt.Printf("err: %s", err)
		return
	}

	// Encrypt with CBC mode
	cipherText := make([]byte, len(plainText))
	encryptMode := cipher.NewCBCEncrypter(block, iv)
	encryptMode.CryptBlocks(cipherText, plainText)
	fmt.Printf("Cipher text: %v\n", cipherText)

	// Decrypt with CBC mode
	decryptedText := make([]byte, len(cipherText))
	decryptMode := cipher.NewCBCDecrypter(block, iv)
	decryptMode.CryptBlocks(decryptedText, cipherText)
	fmt.Printf("Decrypted text: %s\n", string(decryptedText))
}
开发者ID:tcnksm,项目名称:go-crypto,代码行数:35,代码来源:main.go


示例8: TripleDESDecode

// 3DES decode
// Just like encode
func TripleDESDecode(plaintext string) string {

	ciphertext, err := base64.StdEncoding.DecodeString(plaintext)
	if err != nil {
		panic(err)
	}

	block, err := des.NewTripleDESCipher(de2key)
	if err != nil {
		panic(err)
	}

	bs := block.BlockSize()
	if len(ciphertext) < bs {
		panic("ciphertext too short")
	}

	// CBC mode always works in whole blocks.
	if len(ciphertext)%bs != 0 {
		panic("ciphertext is not a multiple of the block size")
	}

	mode := cipher.NewCBCDecrypter(block, iv)

	// CryptBlocks can work in-place if the two arguments are the same.
	mode.CryptBlocks(ciphertext, ciphertext)

	ciphertext = PKCS5UnPadding(ciphertext)
	return string(ciphertext)
}
开发者ID:feiquanbifeng,项目名称:go-projects,代码行数:32,代码来源:des.go


示例9: init

func init() {
	var err error
	block3DES, err = des.NewTripleDESCipher(make([]byte, 24))
	if err != nil {
		panic(err)
	}
	planbuilder.Register("hash", NewHashVindex)
}
开发者ID:henryanand,项目名称:vitess,代码行数:8,代码来源:hash.go


示例10: NewTripleDesConn

func NewTripleDesConn(conn net.Conn, key []byte, iv []byte) (sc net.Conn, err error) {
	block, err := des.NewTripleDESCipher(key)
	if err != nil {
		return
	}
	in := cipher.NewCFBDecrypter(block, iv)
	out := cipher.NewCFBEncrypter(block, iv)
	return CryptConn{conn.(*net.TCPConn), in, out}, nil
}
开发者ID:justzx2011,项目名称:goproxy,代码行数:9,代码来源:keyfile.go


示例11: encryptPinAP

func encryptPinAP(decPin []byte, passLen int, strCardNo string, cardNoLen int,
	encPin []byte, pinKey string, als Logger) error {
	strPin := make([]byte, 16)
	pinCardNo := make([]byte, 32)

	byteCopy(pinCardNo, []byte("0000"), 4)
	iLen := len(strCardNo)
	byteCopy(pinCardNo[4:], []byte(strCardNo[iLen-13:]), 12)
	myStr := fmt.Sprintf("CardPan:[%d][%s]", 16, string(pinCardNo[:16]))
	als.WriteLog(hlog.DEBUG_LEVEL, myStr, hlog.RPT_TO_FILE, nil, 0)

	byteSet(strPin, 'F', 16)
	if decPin[7] == 0 {
		passLen = 6
	}
	myStr = fmt.Sprintf("%02d%s", passLen, string(decPin[:passLen]))
	byteCopy(strPin, []byte(myStr), len(myStr))

	bcdCardNo, err := hex.DecodeString(string(pinCardNo[:16]))
	if err != nil {
		als.WriteLog(hlog.ERR_LEVEL, err.Error(), hlog.RPT_TO_FILE, nil, 0)
		return err
	}
	pinBlock, err1 := hex.DecodeString(string(strPin[:16]))
	if err1 != nil {
		als.WriteLog(hlog.ERR_LEVEL, err1.Error(), hlog.RPT_TO_FILE, nil, 0)
		return err1
	}
	binKey, err2 := hex.DecodeString(pinKey)
	if err2 != nil {
		als.WriteLog(hlog.ERR_LEVEL, err2.Error(), hlog.RPT_TO_FILE, nil, 0)
		return err2
	}
	iLen = len(binKey)
	for i := 0; i < 8; i++ {
		pinBlock[i] ^= bcdCardNo[i]
	}

	if iLen == 8 {
		hDes(pinBlock, encPin, binKey, ENCRYPT)
	} else {
		x, err := des.NewTripleDESCipher(binKey)
		if err != nil {
			als.WriteLog(hlog.ERR_LEVEL, err.Error(), hlog.RPT_TO_FILE, nil, 0)
			return err
		}
		x.Encrypt(encPin, pinBlock)
		// hDes3(pinBlock, encPin, binKey, iLen, ENCRYPT)
	}

	als.WriteLog(hlog.DEBUG_LEVEL, "加密前密码", hlog.RPT_TO_FILE, decPin, passLen)
	als.WriteLog(hlog.DEBUG_LEVEL, "加密后密码", hlog.RPT_TO_FILE, encPin, 8)

	return nil
}
开发者ID:hwch,项目名称:go-dev,代码行数:55,代码来源:des_example.go


示例12: decrypt

func (eci encryptedContentInfo) decrypt(key []byte) ([]byte, error) {
	alg := eci.ContentEncryptionAlgorithm.Algorithm
	if !alg.Equal(oidEncryptionAlgorithmDESCBC) && !alg.Equal(oidEncryptionAlgorithmDESEDE3CBC) && !alg.Equal(oidEncryptionAlgorithmAES256CBC) {
		fmt.Printf("Unsupported Content Encryption Algorithm: %s\n", alg)
		return nil, ErrUnsupportedAlgorithm
	}

	// EncryptedContent can either be constructed of multple OCTET STRINGs
	// or _be_ a tagged OCTET STRING
	var cyphertext []byte
	if eci.EncryptedContent.IsCompound {
		// Complex case to concat all of the children OCTET STRINGs
		var buf bytes.Buffer
		cypherbytes := eci.EncryptedContent.Bytes
		for {
			var part []byte
			cypherbytes, _ = asn1.Unmarshal(cypherbytes, &part)
			buf.Write(part)
			if cypherbytes == nil {
				break
			}
		}
		cyphertext = buf.Bytes()
	} else {
		// Simple case, the bytes _are_ the cyphertext
		cyphertext = eci.EncryptedContent.Bytes
	}

	var block cipher.Block
	var err error

	switch {
	case alg.Equal(oidEncryptionAlgorithmDESCBC):
		block, err = des.NewCipher(key)
	case alg.Equal(oidEncryptionAlgorithmDESEDE3CBC):
		block, err = des.NewTripleDESCipher(key)
	case alg.Equal(oidEncryptionAlgorithmAES256CBC):
		block, err = aes.NewCipher(key)
	}
	if err != nil {
		return nil, err
	}

	iv := eci.ContentEncryptionAlgorithm.Parameters.Bytes
	if len(iv) != block.BlockSize() {
		return nil, errors.New("pkcs7: encryption algorithm parameters are malformed")
	}
	mode := cipher.NewCBCDecrypter(block, iv)
	plaintext := make([]byte, len(cyphertext))
	mode.CryptBlocks(plaintext, cyphertext)
	if plaintext, err = unpad(plaintext, mode.BlockSize()); err != nil {
		return nil, err
	}
	return plaintext, nil
}
开发者ID:GauntletWizard,项目名称:vault,代码行数:55,代码来源:pkcs7.go


示例13: new

// new returns a fresh instance of the given cipher.
func (cipher CipherFunction) new(key []byte) (block cipher.Block) {
	switch cipher {
	case Cipher3DES:
		block, _ = des.NewTripleDESCipher(key)
	case CipherCAST5:
		block, _ = cast5.NewCipher(key)
	case CipherAES128, CipherAES192, CipherAES256:
		block, _ = aes.NewCipher(key)
	}
	return
}
开发者ID:rphillips,项目名称:gosec,代码行数:12,代码来源:packet.go


示例14: TripleDesEncrypt

func TripleDesEncrypt(origData, key []byte) ([]byte, error) {
	block, err := des.NewTripleDESCipher(key)
	if err != nil {
		return nil, err
	}
	origData = PKCS5Padding(origData, block.BlockSize())
	blockMode := cipher.NewCBCEncrypter(block, key[:8])
	crypted := make([]byte, len(origData))
	blockMode.CryptBlocks(crypted, origData)
	return crypted, nil
}
开发者ID:congtaowang,项目名称:golang-utils,代码行数:11,代码来源:des.go


示例15: TripleDesDecrypt

func TripleDesDecrypt(crypted, key []byte) ([]byte, error) {
	block, err := des.NewTripleDESCipher(key)
	if err != nil {
		return nil, err
	}
	blockMode := cipher.NewCBCDecrypter(block, key[:8])
	origData := make([]byte, len(crypted))
	blockMode.CryptBlocks(origData, crypted)
	origData = PKCS5UnPadding(origData)
	return origData, nil
}
开发者ID:congtaowang,项目名称:golang-utils,代码行数:11,代码来源:des.go


示例16: newBlockCipher

func newBlockCipher(name string, key []byte) (cipher.Block, error) {
	switch name {
	case "aes128":
		return aes.NewCipher(key)
	case "aes256":
		return aes.NewCipher(key)
	case "3des":
		return des.NewTripleDESCipher(key)
	default:
		return nil, fmt.Errorf("unknown cipher '%s'", name)
	}
}
开发者ID:GaloisInc,项目名称:hacrypto,代码行数:12,代码来源:make_legacy_aead_tests.go


示例17: DeData

func (this *Pub) DeData(cryptData []byte) ([]byte, error) {
	return cryptData, nil
	block, err := des.NewTripleDESCipher(DES_KEY)
	if err != nil {
		return nil, err
	}
	blockMode := cipher.NewCBCDecrypter(block, DES_KEY[:8])
	origData := make([]byte, len(cryptData))
	blockMode.CryptBlocks(origData, cryptData)
	origData = this.unPadding(origData)
	return origData, nil
}
开发者ID:hxhxd,项目名称:s5helper,代码行数:12,代码来源:main.go


示例18: EnData

func (this *Pub) EnData(originData []byte) ([]byte, error) {
	return originData, nil
	block, err := des.NewTripleDESCipher(DES_KEY)
	if err != nil {
		return nil, err
	}
	originData = this.padding(originData, block.BlockSize())
	blockMode := cipher.NewCBCEncrypter(block, DES_KEY[:8])
	crypted := make([]byte, len(originData))
	blockMode.CryptBlocks(crypted, originData)
	return crypted, nil
}
开发者ID:hxhxd,项目名称:s5helper,代码行数:12,代码来源:main.go


示例19: Decrypt

func (this *SecurityTripleDES) Decrypt(key, data []byte) ([]byte, error) {
	key = this.GetKey(key, this.keySize)
	block, err := des.NewTripleDESCipher(key)
	if err != nil {
		return nil, err
	}
	blockMode := cipher.NewCBCDecrypter(block, this.GetIV(key, this.ivSize))
	originalData := make([]byte, len(data))
	blockMode.CryptBlocks(originalData, data)
	originalData = ZeroUnPadding(originalData)
	return originalData, nil
}
开发者ID:artwebs,项目名称:aogo,代码行数:12,代码来源:SecurityDES.go


示例20: Encrypt

func (this *SecurityTripleDES) Encrypt(key, data []byte) ([]byte, error) {
	key = this.GetKey(key, this.keySize)
	block, err := des.NewTripleDESCipher(key)
	if err != nil {
		return nil, err
	}
	originalData := PKCS5Padding(data, block.BlockSize())
	blockMode := cipher.NewCBCEncrypter(block, this.GetIV(key, this.ivSize))
	crypted := make([]byte, len(originalData))
	blockMode.CryptBlocks(crypted, originalData)
	return crypted, nil
}
开发者ID:artwebs,项目名称:aogo,代码行数:12,代码来源:SecurityDES.go



注:本文中的crypto/des.NewTripleDESCipher函数示例整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Golang dsa.GenerateKey函数代码示例发布时间:2022-05-24
下一篇:
Golang des.NewCipher函数代码示例发布时间:2022-05-24
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap