Go   发布时间:2022-04-09  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了Golang(13)Security and Secret大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

Golang(13)Security and Secret

@H_618_7@1. Simple BASE64
package main

import (
"encoding/base64"
"fmt"
)

func base64Encode(src String) String {
return base64.StdEncoding.EncodeToString([]byte(srC))
}

func base64Decode(src String) (String,error) {
c,err := base64.StdEncoding.DecodeString(srC)
return String(C),err
}

func main() {
// encode
Hello := "Hello sillycat,you will do auth again."
debyte := base64Encode(Hello)
fmt.Println(debytE)
// decode
enbyte,err := base64Decode(debytE)
if err != nil {
fmt.Println(err.Error())
}

if Hello != enbyte {
fmt.Println("Hello is not equal to enbyte")
}

fmt.Println(enbytE)
}

@H_618_7@2. AES and DES
AES(Advanced Encryption Standard),DES(Data Encryption Standard)

Go AES is just using a module

package main

import (
"crypto/aes"
"crypto/cipher"
"fmt"
"os"
)

var commonIV = []byte{0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0a,0x0b,0x0c,0x0d,0x0e,0x0f}

func main() {
//需要去加密的字符串
plaintext := []byte("My English name is Carl")
//如果传入加密串的话,plaint就是传入的字符串
if len(os.Args) > 1 {
plaintext = []byte(os.Args[1])
}

//aes的加密字符串
key_text := "astaxie12798akljzmknm.ahkjkljl;k"
if len(os.Args) > 2 {
key_text = os.Args[2]
}

fmt.Println(len(key_text))

// 创建加密算法aes
c,err := aes.NewCipher([]byte(key_text))
if err != nil {
fmt.Printf("Error: NewCipher(%d bytes) = %s",len(key_text),err)
os.Exit(-1)
}

//加密字符串
cfb := cipher.NewCFBEncrypter(c,commonIV)
ciphertext := make([]byte,len(plaintext))
cfb.XORKeyStream(ciphertext,plaintext)
fmt.Printf("%s=>%x\n",plaintext,ciphertext)

// 解密字符串
cfbdec := cipher.NewCFBDecrypter(c,commonIV)
plaintextCopy := make([]byte,len(plaintext))
cfbdec.XORKeyStream(plaintextCopy,ciphertext)
fmt.Printf("%x=>%s\n",ciphertext,plaintextCopy)
}

A tool for String HEX
@R_616_10107@://www.String-functions.com/hex-String.aspx


@H_618_7@References:
@R_616_10107@s://github.com/astaxie/build-web-application-with-golang/blob/master/ebook/09.6.md

大佬总结

以上是大佬教程为你收集整理的Golang(13)Security and Secret全部内容,希望文章能够帮你解决Golang(13)Security and Secret所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。