go标准库的学习-crypto/des
参考: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的更多相关文章
- go标准库的学习-crypto/md5
参考:https://studygolang.com/pkgdoc 导入方式: import "crypto/md5" md5包实现了MD5哈希算法,参见RFC 1321. Con ...
- go标准库的学习-crypto/sha1
参考:https://studygolang.com/pkgdoc 导入方式: import "crypto/sha1" sha1包实现了SHA1哈希算法,参见RFC 3174. ...
- go标准库的学习-crypto/sha256
参考:https://studygolang.com/pkgdoc 导入方式: import "crypto/sha256" sha256包实现了SHA224和SHA256哈希算法 ...
- go标准库的学习-crypto/rand
参考:https://studygolang.com/pkgdoc 导入方式: import "crypto/rand" rand包实现了用于加解密的更安全的随机数生成器. Var ...
- go标准库的学习-crypto/aes
参考:https://studygolang.com/pkgdoc 导入方式: import "crypto/aes" aes包实现了AES加密算法,参见U.S. Federal ...
- go标准库的学习-net/http
参考:https://studygolang.com/pkgdoc 概念解释: request:用户请求的信息,用来解析用户的请求信息,包括post.get.cookie.url等信息 respons ...
- go标准库的学习-database/sql
参考:https://studygolang.com/pkgdoc 导入方式: import "database/sql" sql包提供了保证SQL或类SQL数据库的泛用接口. 使 ...
- python 标准库基础学习之开发工具部分1学习
#2个标准库模块放一起学习,这样减少占用地方和空间#标准库之compileall字节编译源文件import compileall,re,sys#作用是查找到python文件,并把它们编译成字节码表示, ...
- python calendar标准库基础学习
# -*- coding: utf-8 -*-# 作者:新手__author__ = 'Administrator'#标准库:日期时间基础学习:calendar:处理日期#例1import calen ...
随机推荐
- Umbraco 7 特点
Umbraco 7 features at a glance The backend is mainly built on .NET C# MVC. There are some leftovers ...
- asp.net通过后台代码给前台设置css样式,下拉列表在js中的取值
后台根据不同的用户登陆隐藏或显示前台div标签 前台: 将div声明成服务器端控件 <div id="div1" runat="server">.. ...
- [PHP] 算法-统计一个数字在排序数组中出现的次数的PHP实现
统计一个数字在排序数组中出现的次数. 1.有序的数组查找,使用二分法 2.二分法查找第一次出现的位置,二分法查找最后一次出现的位置,end - start +1 left=getLeft(data,k ...
- java中import static和import的区别【转】
转自:http://blog.csdn.net/ygc87/article/details/7371254
- 【Spring】25、Spring代理。 BeanNameAutoProxyCreator 与 ProxyFactoryBean
一般我们可以使用ProxyBeanFactory,并配置proxyInterfaces,target和interceptorNames实现,但如果需要代理的bean很多,无疑会对spring配置文件的 ...
- Java-IO:复制文件
import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStrea ...
- MAC下搭建个人博客
安装homebrew ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/inst ...
- BZOJ2820: YY的GCD(反演)
题解 题意 题目链接 Sol 反演套路题.. 不多说了,就是先枚举一个质数,再枚举一个约数然后反演一下. 最后可以化成这样子 \[\sum_{i = 1}^n \frac{n}{k} \frac{n} ...
- 获取子元素节点(children,childNodes)
在js中获取元素节点有DOM对应的方法如getElementsByTagName()等等..对于获取子元素还有另外一种获取方法ChildNodes 不过ChidNodes在高级浏览器除(IE6-8)里 ...
- 教你如何使用云服务器去搭建SS
注册云服务器 (首先推荐Vultr,注册链接:https://www.vultr.com/?ref=6962741,其他云服务商如阿里云HK,Linode等亦可使用,按需选择) 这里拿Vultr举例: ...