加密解密在实际开发中应用比较广泛,常用加解密分为:“对称式”、“非对称式”和”数字签名“。对称式:对称加密(也叫私钥加密)指加密和解密使用相同密钥的加密算法。具体算法主要有des算法,3des算法,tdea算法,blowfish算法,rc5算法,idea算法。非对称加密(公钥加密):指加密和解密使用不同密钥的加密算法,也称为公私钥加密。具体算法主要有rsa、elgamal、背包算法、rabin、d-h、ecc(椭圆曲线加密算法)。数字签名:数字签名是非对称密钥加密技术与数字摘要技术的应用。主要算法有md5、hmac、sha1等。以下介绍golang语言主要的加密解密算法实现。
md5
md5信息摘要算法是一种被广泛使用的密码散列函数,可以产生出一个128位(16进制,32个字符)的散列值(hash value),用于确保信息传输完整一致。
func getmd5string(s string) string { h := md5.new() h.write([]byte(s)) return hex.encodetostring(h.sum(nil))}
hmac
hmac是密钥相关的哈希运算消息认证码(hash-based message authentication code)的缩写, 它通过一个标准算法,在计算哈希的过程中,把key混入计算过程中。和自定义的加salt算法不同,hmac算法针对所有哈希算法都通用,无论是md5还是sha-1。采用hmac替代自己的salt算法,可以使程序算法更标准化,也更安全。示例
//key随意设置 data 要加密数据func hmac(key, data string) string { hash:= hmac.new(md5.new, []byte(key)) // 创建对应的md5哈希加密算法 hash.write([]byte(data)) return hex.encodetostring(hash.sum([]byte()))}func hmacsha256(key, data string) string { hash:= hmac.new(sha256.new, []byte(key)) //创建对应的sha256哈希加密算法 hash.write([]byte(data)) return hex.encodetostring(hash.sum([]byte()))}
sha1
sha-1可以生成一个被称为消息摘要的160位(20字节)散列值,散列值通常的呈现形式为40个十六进制数。
func sha1(data string) string { sha1 := sha1.new() sha1.write([]byte(data)) return hex.encodetostring(sha1.sum([]byte()))}
aes
密码学中的高级加密标准(advanced encryption standard,aes),又称rijndael加密法,是美国联邦政府采用的一种区块加密标准。这个标准用来替代原先的des(data encryption standard),已经被多方分析且广为全世界所使用。aes中常见的有三种解决方案,分别为aes-128、aes-192和aes-256。如果采用真正的128位加密技术甚至256位加密技术,蛮力攻击要取得成功需要耗费相当长的时间。aes 有五种加密模式:
电码本模式(electronic codebook book (ecb))
密码分组链接模式(cipher block chaining (cbc))
计算器模式(counter (ctr))
密码反馈模式(cipher feedback (cfb))
输出反馈模式(output feedback (ofb))
ecb模式
出于安全考虑,golang默认并不支持ecb模式。
package mainimport ( crypto/aes fmt)func aesencrypt(src []byte, key []byte) (encrypted []byte) { cipher, _ := aes.newcipher(generatekey(key)) length := (len(src) + aes.blocksize) / aes.blocksize plain := make([]byte, length*aes.blocksize) copy(plain, src) pad := byte(len(plain) - len(src)) for i := len(src); i < len(plain); i++ { plain[i] = pad } encrypted = make([]byte, len(plain)) // 分组分块加密 for bs, be := 0, cipher.blocksize(); bs <= len(src); bs, be = bs+cipher.blocksize(), be+cipher.blocksize() { cipher.encrypt(encrypted[bs:be], plain[bs:be]) } return encrypted}func aesdecrypt(encrypted []byte, key []byte) (decrypted []byte) { cipher, _ := aes.newcipher(generatekey(key)) decrypted = make([]byte, len(encrypted)) // for bs, be := 0, cipher.blocksize(); bs 0 { trim = len(decrypted) - int(decrypted[len(decrypted)-1]) } return decrypted[:trim]}func generatekey(key []byte) (genkey []byte) { genkey = make([]byte, 16) copy(genkey, key) for i := 16; i < len(key); { for j := 0; j < 16 && i < len(key); j, i = j+1, i+1 { genkey[j] ^= key[i] } } return genkey}func main() { source:=hello world fmt.println(原字符:,source) //16byte密钥 key:=1443flfsawfdas encryptcode:=aesencrypt([]byte(source),[]byte(key)) fmt.println(密文:,string(encryptcode)) decryptcode:=aesdecrypt(encryptcode,[]byte(key)) fmt.println(解密:,string(decryptcode))}
cbc模式
package mainimport( bytes crypto/aes fmt crypto/cipher encoding/base64)func main() { orig := hello world key := 0123456789012345 fmt.println(原文:, orig) encryptcode := aesencrypt(orig, key) fmt.println(密文: , encryptcode) decryptcode := aesdecrypt(encryptcode, key) fmt.println(解密结果:, decryptcode)}func aesencrypt(orig string, key string) string { // 转成字节数组 origdata := []byte(orig) k := []byte(key) // 分组秘钥 // newcipher该函数限制了输入k的长度必须为16, 24或者32 block, _ := aes.newcipher(k) // 获取秘钥块的长度 blocksize := block.blocksize() // 补全码 origdata = pkcs7padding(origdata, blocksize) // 加密模式 blockmode := cipher.newcbcencrypter(block, k[:blocksize]) // 创建数组 cryted := make([]byte, len(origdata)) // 加密 blockmode.cryptblocks(cryted, origdata) return base64.stdencoding.encodetostring(cryted)}func aesdecrypt(cryted string, key string) string { // 转成字节数组 crytedbyte, _ := base64.stdencoding.decodestring(cryted) k := []byte(key) // 分组秘钥 block, _ := aes.newcipher(k) // 获取秘钥块的长度 blocksize := block.blocksize() // 加密模式 blockmode := cipher.newcbcdecrypter(block, k[:blocksize]) // 创建数组 orig := make([]byte, len(crytedbyte)) // 解密 blockmode.cryptblocks(orig, crytedbyte) // 去补全码 orig = pkcs7unpadding(orig) return string(orig)}//补码//aes加密数据块分组长度必须为128bit(byte[16]),密钥长度可以是128bit(byte[16])、192bit(byte[24])、256bit(byte[32])中的任意一个。func pkcs7padding(ciphertext []byte, blocksize int) []byte { padding := blocksize - len(ciphertext)%blocksize padtext := bytes.repeat([]byte{byte(padding)}, padding) return append(ciphertext, padtext...)}//去码func pkcs7unpadding(origdata []byte) []byte { length := len(origdata) unpadding := int(origdata[length-1]) return origdata[:(length - unpadding)]}
crt模式
package mainimport ( bytes crypto/aes crypto/cipher fmt)//加密func aesctrcrypt(plaintext []byte, key []byte) ([]byte, error) { //1. 创建cipher.block接口 block, err := aes.newcipher(key) if err != nil { return nil, err } //2. 创建分组模式,在crypto/cipher包中 iv := bytes.repeat([]byte(1), block.blocksize()) stream := cipher.newctr(block, iv) //3. 加密 dst := make([]byte, len(plaintext)) stream.xorkeystream(dst, plaintext) return dst, nil}func main() { source:=hello world fmt.println(原字符:,source) key:=1443flfsawfdasds encryptcode,_:=aesctrcrypt([]byte(source),[]byte(key)) fmt.println(密文:,string(encryptcode)) decryptcode,_:=aesctrcrypt(encryptcode,[]byte(key)) fmt.println(解密:,string(decryptcode))}
cfb模式
package mainimport ( crypto/aes crypto/cipher crypto/rand encoding/hex fmt io)func aesencryptcfb(origdata []byte, key []byte) (encrypted []byte) { block, err := aes.newcipher(key) if err != nil { //panic(err) } encrypted = make([]byte, aes.blocksize+len(origdata)) iv := encrypted[:aes.blocksize] if _, err := io.readfull(rand.reader, iv); err != nil { //panic(err) } stream := cipher.newcfbencrypter(block, iv) stream.xorkeystream(encrypted[aes.blocksize:], origdata) return encrypted}func aesdecryptcfb(encrypted []byte, key []byte) (decrypted []byte) { block, _ := aes.newcipher(key) if len(encrypted) < aes.blocksize { panic(ciphertext too short) } iv := encrypted[:aes.blocksize] encrypted = encrypted[aes.blocksize:] stream := cipher.newcfbdecrypter(block, iv) stream.xorkeystream(encrypted, encrypted) return encrypted}func main() { source:=hello world fmt.println(原字符:,source) key:=abcdefghijklmno1//16位 encryptcode:=aesencryptcfb([]byte(source),[]byte(key)) fmt.println(密文:,hex.encodetostring(encryptcode)) decryptcode:=aesdecryptcfb(encryptcode,[]byte(key)) fmt.println(解密:,string(decryptcode))}
ofb模式
package mainimport ( bytes crypto/aes crypto/cipher crypto/rand encoding/hex fmt io)func aesencryptofb( data[]byte,key []byte) ([]byte, error) { data = pkcs7padding(data, aes.blocksize) block, _ := aes.newcipher([]byte(key)) out := make([]byte, aes.blocksize + len(data)) iv := out[:aes.blocksize] if _, err := io.readfull(rand.reader, iv); err != nil { return nil, err } stream := cipher.newofb(block, iv) stream.xorkeystream(out[aes.blocksize:], data) return out, nil}func aesdecryptofb( data[]byte,key []byte) ([]byte, error) { block, _ := aes.newcipher([]byte(key)) iv := data[:aes.blocksize] data = data[aes.blocksize:] if len(data) % aes.blocksize != 0 { return nil, fmt.errorf(data is not a multiple of the block size) } out := make([]byte, len(data)) mode := cipher.newofb(block, iv) mode.xorkeystream(out, data) out= pkcs7unpadding(out) return out, nil}//补码//aes加密数据块分组长度必须为128bit(byte[16]),密钥长度可以是128bit(byte[16])、192bit(byte[24])、256bit(byte[32])中的任意一个。func pkcs7padding(ciphertext []byte, blocksize int) []byte { padding := blocksize - len(ciphertext)%blocksize padtext := bytes.repeat([]byte{byte(padding)}, padding) return append(ciphertext, padtext...)}//去码func pkcs7unpadding(origdata []byte) []byte { length := len(origdata) unpadding := int(origdata[length-1]) return origdata[:(length - unpadding)]}func main() { source:=hello world fmt.println(原字符:,source) key:=1111111111111111//16位 32位均可 encryptcode,_:=aesencryptofb([]byte(source),[]byte(key)) fmt.println(密文:,hex.encodetostring(encryptcode)) decryptcode,_:=aesdecryptofb(encryptcode,[]byte(key)) fmt.println(解密:,string(decryptcode))}
rsa加密
首先使用openssl生成公私钥
硬件接口的类型
洗碗机哪个牌子好?最新洗碗机十大排名让你放心选!
Micro Ⅲ Lite微型高性能红外热成像机芯介绍
诺基亚5G订单已经赶超华为 华为面临危机?
芬兰吸引中国锂电材料企业投资建厂
golang语言的加密解密算法实现
浅谈一下汽车连接器的材料
stm32单片机的基本工作原理
斐纳TOMEFON智能扫地机器人怎么样
IP网络维护的四大难题,如何运营好IP网络
负电压是如何产生的
印刷电路板行业的压板机油冷却操作及保养说明
飞秒激光加工系统介绍
区块链如何对传统金融体系进行改造
如何从大脑活动中解码自然语言呢?
慕展热点大搜罗丨从芯片代工服务到数据存储,富士通嫡系部队出动(文末有福利)
比亚迪DiLink3.0“高温消毒杀菌”黑科技了解一下
多个整机厂商展示统一操作系统UOS桌面整机产品
边缘计算可以推动物联网世界什么发展
EMC基础:使用电容和电感降噪的方法