参考:https://studygolang.com/pkgdoc

导入方式:

import "crypto/des"

des包实现了DES标准和TDEA算法,参见U.S. Federal Information Processing Standards Publication 46-3。

Constants

const BlockSize = 

DES字节块的大小。

type KeySizeError

type KeySizeError int

func (KeySizeError) Error

func (k KeySizeError) Error() string

func NewCipher

func NewCipher(key []byte) (cipher.Block, error)

创建并返回一个使用DES算法的cipher.Block接口。

它与的区别在于它的key最长只能为8字节,否则会报错,如:

key := []byte("example w")

就会报错:

panic: crypto/des: invalid key size 

举例:

package main

import (
"fmt"
"crypto/des"
) func main() {
key := []byte("examplew") desCipher, err := des.NewCipher(key)
if err != nil {
panic(err)
}
var inputData = []byte{0x32, 0x43, 0xf6, 0xa8, 0x88, 0x5a, 0x30, 0x8d, 0x31, 0x31, 0x98, 0xa2, 0xe0, 0x37, 0x07, 0x34}
out := make([]byte, len(inputData))
desCipher.Encrypt(out, inputData)
fmt.Printf("Encrypted data : %#v\n", out) //Encrypted data : []byte{0xac, 0x53, 0x6b, 0xbd, 0x59, 0xc5, 0x82, 0x59, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0} plain := make([]byte, len(inputData))
desCipher.Decrypt(plain, out)
fmt.Printf("Decrypted data : %#v\n", plain) //Decrypted data : []byte{0x32, 0x43, 0xf6, 0xa8, 0x88, 0x5a, 0x30, 0x8d, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0}
}

func NewTripleDESCipher

func NewTripleDESCipher(key []byte) (cipher.Block, error)

创建并返回一个使用TDEA算法的cipher.Block接口。

举例:

package main

import (
"fmt"
"crypto/des"
) func main() {
ede2Key := []byte("example key 1234")
var tripleDESKey []byte
tripleDESKey = append(tripleDESKey, ede2Key[:]...)
tripleDESKey = append(tripleDESKey, ede2Key[:]...)
desCipher, err := des.NewTripleDESCipher(tripleDESKey)
if err != nil {
panic(err)
}
var inputData = []byte{0x32, 0x43, 0xf6, 0xa8, 0x88, 0x5a, 0x30, 0x8d, 0x31, 0x31, 0x98, 0xa2, 0xe0, 0x37, 0x07, 0x34}
out := make([]byte, len(inputData))
desCipher.Encrypt(out, inputData)
fmt.Printf("Encrypted data : %#v\n", out) //Encrypted data : []byte{0x39, 0x9e, 0xbe, 0xa9, 0xc3, 0xfa, 0x77, 0x5e, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0} plain := make([]byte, len(inputData))
desCipher.Decrypt(plain, out)
fmt.Printf("Decrypted data : %#v\n", plain) //Decrypted data : []byte{0x32, 0x43, 0xf6, 0xa8, 0x88, 0x5a, 0x30, 0x8d, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0}
}

go标准库的学习-crypto/des的更多相关文章

  1. go标准库的学习-crypto/md5

    参考:https://studygolang.com/pkgdoc 导入方式: import "crypto/md5" md5包实现了MD5哈希算法,参见RFC 1321. Con ...

  2. go标准库的学习-crypto/sha1

    参考:https://studygolang.com/pkgdoc 导入方式: import "crypto/sha1" sha1包实现了SHA1哈希算法,参见RFC 3174. ...

  3. go标准库的学习-crypto/sha256

    参考:https://studygolang.com/pkgdoc 导入方式: import "crypto/sha256" sha256包实现了SHA224和SHA256哈希算法 ...

  4. go标准库的学习-crypto/rand

    参考:https://studygolang.com/pkgdoc 导入方式: import "crypto/rand" rand包实现了用于加解密的更安全的随机数生成器. Var ...

  5. go标准库的学习-crypto/aes

    参考:https://studygolang.com/pkgdoc 导入方式: import "crypto/aes" aes包实现了AES加密算法,参见U.S. Federal ...

  6. go标准库的学习-net/http

    参考:https://studygolang.com/pkgdoc 概念解释: request:用户请求的信息,用来解析用户的请求信息,包括post.get.cookie.url等信息 respons ...

  7. go标准库的学习-database/sql

    参考:https://studygolang.com/pkgdoc 导入方式: import "database/sql" sql包提供了保证SQL或类SQL数据库的泛用接口. 使 ...

  8. python 标准库基础学习之开发工具部分1学习

    #2个标准库模块放一起学习,这样减少占用地方和空间#标准库之compileall字节编译源文件import compileall,re,sys#作用是查找到python文件,并把它们编译成字节码表示, ...

  9. python calendar标准库基础学习

    # -*- coding: utf-8 -*-# 作者:新手__author__ = 'Administrator'#标准库:日期时间基础学习:calendar:处理日期#例1import calen ...

随机推荐

  1. 近期ASP.NET问题汇总及对应的解决办法

    1. 使用SQL统计一个字符串中指定字符的个数,示例(统计0的个数): ','')) 2. 使用Forms认证,客户端本地时间不对无法登陆系统,解决办法: FormsAuthentication.Re ...

  2. Java中的集合迭代器

    集合的迭代器 任何集合都有迭代器. 任何集合类,都必须能以某种方式存取元素,否则这个集合容器就没有任何意义. 迭代器,也是一种模式(也叫迭代器模式).在java中它是一个对象,其目的是遍历并选中其中的 ...

  3. Maven的pom.xml文件详解【转载】

    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/20 ...

  4. blfs(systemv版本)学习笔记-编译安装i3-wm平铺式窗口管理器

    我的邮箱地址:zytrenren@163.com欢迎大家交流学习纠错! i3-wm项目的官网:https://i3wm.org/ 首先需要lfs基础上编译安装完整的xorg服务 我的xorg服务编译安 ...

  5. layui 图片上传+表单提交+ Spring MVC

    Layui 的上传是最常用的, 不可或缺, 记录一下代码, 以后复制都能用!! 1.前端HTML: <div class="layui-form-item"> < ...

  6. ajax小知识

    1.ajax发送get请求时,需要注意如下情况: var uri="http://127.0.0.1:8071/springmvcdemo/bigdataapi/publishdata&qu ...

  7. python之锁, 队列

    进程的其他方法 进程id,进程名字,查看进程是否活着is_alive()  terminate()发送结束进程的信号 import time import os from multiprocessin ...

  8. XML语言学习随笔

    XML和HTML都是W3C的定制的标准,XML的诞生本身是为了替代不成熟的HTML,但是因为现实的环境,XML替代HTML并未成功.之后W3C为了代码严谨性的决心,又发布了升级版的标记语言XHTML, ...

  9. python联系题1

    一.有四个数字:1.2.3.4,能组成多少个互不相同且无重复数字的三位数?各是多少? 程序分析:可填在百位.十位.个位的数字都是1.2.3.4.组成所有的排列后再去 掉不满足条件的排列. # _*_ ...

  10. 排错-LR安装No Background bmp defined in ...的解决办法

    LR安装No Background bmp defined in section General entry BGBmp的解决办法 by:授客 QQ:1033553122 问题描述: 我在win7装L ...