麻豆黑色丝袜jk制服福利网站-麻豆精品传媒视频观看-麻豆精品传媒一二三区在线视频-麻豆精选传媒4区2021-在线视频99-在线视频a

千鋒教育-做有情懷、有良心、有品質的職業教育機構

手機站
千鋒教育

千鋒學習站 | 隨時隨地免費學

千鋒教育

掃一掃進入千鋒手機站

領取全套視頻
千鋒教育

關注千鋒學習站小程序
隨時隨地免費學習課程

當前位置:首頁  >  技術干貨  > Go語言常用加密算法和加密技術全面解析!

Go語言常用加密算法和加密技術全面解析!

來源:千鋒教育
發布人:xqq
時間: 2023-12-27 06:59:54 1703631594

Go語言常用加密算法和加密技術全面解析!

在現代計算機領域,安全性非常重要,而加密技術是保證計算機信息系統安全性的重要措施之一。為了滿足這個需求,Go語言提供了豐富的加密算法和加密技術,本文將為大家詳細解析Go語言中常用的加密算法和加密技術。

一、對稱加密算法

對稱加密算法是加密和解密使用相同的密鑰的一種加密方式。常用的對稱加密算法有DES、3DES、AES等。Go語言中的crypto包中提供了多種對稱加密算法。

以下是一個示例,使用AES加密算法加密字符串,然后再進行解密:

`go

package main

import (

"crypto/aes"

"crypto/cipher"

"fmt"

)

func AesEncrypt(plainText, key byte) (byte, error) {

block, err := aes.NewCipher(key)

if err != nil {

return nil, err

}

iv := byte("1234567890123456")

plainText = pkcs5Padding(plainText, block.BlockSize())

blockMode := cipher.NewCBCEncrypter(block, iv)

cipherText := make(byte, len(plainText))

blockMode.CryptBlocks(cipherText, plainText)

return cipherText, nil

}

func AesDecrypt(cipherText, key byte) (byte, error) {

block, err := aes.NewCipher(key)

if err != nil {

return nil, err

}

iv := byte("1234567890123456")

blockMode := cipher.NewCBCDecrypter(block, iv)

plainText := make(byte, len(cipherText))

blockMode.CryptBlocks(plainText, cipherText)

plainText = pkcs5UnPadding(plainText)

return plainText, nil

}

func pkcs5Padding(cipherText byte, blockSize int) byte {

padding := blockSize - len(cipherText)%blockSize

padText := bytes.Repeat(byte{byte(padding)}, padding)

return append(cipherText, padText...)

}

func pkcs5UnPadding(plainText byte) byte {

length := len(plainText)

unPadding := int(plainText)

return plainText

}

func main() {

plainText := byte("Hello World")

key := byte("12345678901234567890123456789012")

cipherText, err := AesEncrypt(plainText, key)

if err != nil {

fmt.Println(err)

return

}

fmt.Printf("Cipher Text: %x\n", cipherText)

plainTextResult, err := AesDecrypt(cipherText, key)

if err != nil {

fmt.Println(err)

return

}

fmt.Printf("Plain Text: %s\n", plainTextResult)

}

二、非對稱加密算法非對稱加密算法也稱為公鑰加密算法,加密和解密使用不同的密鑰。常用的非對稱加密算法有RSA、ECC等。Go語言中的crypto包中也提供了多種非對稱加密算法。以下是一個示例,使用RSA加密算法加密字符串,然后再進行解密:`gopackage mainimport (    "crypto/rand"    "crypto/rsa"    "crypto/x509"    "encoding/pem"    "fmt")func RsaEncrypt(plainText byte, publicKey byte) (byte, error) {    block, _ := pem.Decode(publicKey)    if block == nil {        return nil, fmt.Errorf("public key pem decode failed")    }    pubInterface, err := x509.ParsePKIXPublicKey(block.Bytes)    if err != nil {        return nil, err    }    pub := pubInterface.(*rsa.PublicKey)    cipherText, err := rsa.EncryptPKCS1v15(rand.Reader, pub, plainText)    if err != nil {        return nil, err    }    return cipherText, nil}func RsaDecrypt(cipherText byte, privateKey byte) (byte, error) {    block, _ := pem.Decode(privateKey)    if block == nil {        return nil, fmt.Errorf("private key pem decode failed")    }    pri, err := x509.ParsePKCS8PrivateKey(block.Bytes)    if err != nil {        return nil, err    }    priKey := pri.(*rsa.PrivateKey)    plainText, err := rsa.DecryptPKCS1v15(rand.Reader, priKey, cipherText)    if err != nil {        return nil, err    }    return plainText, nil}func main() {    plainText := byte("Hello World")    publicKey, privateKey := GenerateRsaKeyPair(2048)    cipherText, err := RsaEncrypt(plainText, publicKey)    if err != nil {        fmt.Println(err)        return    }    fmt.Printf("Cipher Text: %x\n", cipherText)    plainTextResult, err := RsaDecrypt(cipherText, privateKey)    if err != nil {        fmt.Println(err)        return    }    fmt.Printf("Plain Text: %s\n", plainTextResult)}func GenerateRsaKeyPair(bitsize int) (byte, byte) {    priKey, err := rsa.GenerateKey(rand.Reader, bitsize)    if err != nil {        return nil, nil    }    priDER := x509.MarshalPKCS1PrivateKey(priKey)    priPEM := pem.EncodeToMemory(&pem.Block{        Type:  "RSA PRIVATE KEY",        Bytes: priDER,    })    publicKey := priKey.PublicKey    pubDER, err := x509.MarshalPKIXPublicKey(&publicKey)    if err != nil {        return nil, nil    }    pubPEM := pem.EncodeToMemory(&pem.Block{        Type:  "PUBLIC KEY",        Bytes: pubDER,    })    return pubPEM, priPEM}

三、哈希算法

哈希算法將任意長度的消息壓縮到固定長度的摘要中。常用的哈希算法有SHA-1、SHA-256、MD5等。Go語言中的crypto包中也提供了多種哈希算法。

以下是一個示例,使用SHA-256算法計算字符串的哈希值:

`go

package main

import (

"crypto/sha256"

"fmt"

)

func ComputeSha256Hash(data byte) byte {

h := sha256.New()

h.Write(data)

return h.Sum(nil)

}

func main() {

data := byte("Hello World")

hash := ComputeSha256Hash(data)

fmt.Printf("SHA-256 Hash: %x\n", hash)

}

四、數字簽名數字簽名是一種基于公鑰密碼學的技術,用來保證消息的完整性、真實性和不可否認性。常用的數字簽名算法有RSA、ECC等。Go語言中的crypto包中也提供了多種數字簽名算法。以下是一個示例,使用RSA算法對數據進行數字簽名,然后再進行驗證:`gopackage mainimport (    "crypto/rand"    "crypto/rsa"    "crypto/sha256"    "crypto/x509"    "encoding/pem"    "fmt")func RsaSign(data byte, privateKey byte) (byte, error) {    block, _ := pem.Decode(privateKey)    if block == nil {        return nil, fmt.Errorf("private key pem decode failed")    }    pri, err := x509.ParsePKCS8PrivateKey(block.Bytes)    if err != nil {        return nil, err    }    priKey := pri.(*rsa.PrivateKey)    h := sha256.New()    h.Write(data)    hash := h.Sum(nil)    signature, err := rsa.SignPKCS1v15(rand.Reader, priKey, crypto.SHA256, hash)    if err != nil {        return nil, err    }    return signature, nil}func RsaVerify(data, signature, publicKey byte) (bool, error) {    block, _ := pem.Decode(publicKey)    if block == nil {        return false, fmt.Errorf("public key pem decode failed")    }    pubInterface, err := x509.ParsePKIXPublicKey(block.Bytes)    if err != nil {        return false, err    }    pub := pubInterface.(*rsa.PublicKey)    h := sha256.New()    h.Write(data)    hash := h.Sum(nil)    err = rsa.VerifyPKCS1v15(pub, crypto.SHA256, hash, signature)    if err != nil {        return false, err    }    return true, nil}func main() {    data := byte("Hello World")    publicKey, privateKey := GenerateRsaKeyPair(2048)    signature, err := RsaSign(data, privateKey)    if err != nil {        fmt.Println(err)        return    }    fmt.Printf("Signature: %x\n", signature)    ok, err := RsaVerify(data, signature, publicKey)    if err != nil {        fmt.Println(err)        return    }    fmt.Printf("Verify Result: %t\n", ok)}

總結:

上述代碼示例了Go語言中常用的加密算法和加密技術,包括對稱加密算法、非對稱加密算法、哈希算法和數字簽名。這些技術在實際應用中非常有用,能夠幫助開發人員保證系統的安全性。

以上就是IT培訓機構千鋒教育提供的相關內容,如果您有web前端培訓鴻蒙開發培訓python培訓linux培訓,java培訓,UI設計培訓等需求,歡迎隨時聯系千鋒教育。

tags:
聲明:本站稿件版權均屬千鋒教育所有,未經許可不得擅自轉載。
10年以上業內強師集結,手把手帶你蛻變精英
請您保持通訊暢通,專屬學習老師24小時內將與您1V1溝通
免費領取
今日已有369人領取成功
劉同學 138****2860 剛剛成功領取
王同學 131****2015 剛剛成功領取
張同學 133****4652 剛剛成功領取
李同學 135****8607 剛剛成功領取
楊同學 132****5667 剛剛成功領取
岳同學 134****6652 剛剛成功領取
梁同學 157****2950 剛剛成功領取
劉同學 189****1015 剛剛成功領取
張同學 155****4678 剛剛成功領取
鄒同學 139****2907 剛剛成功領取
董同學 138****2867 剛剛成功領取
周同學 136****3602 剛剛成功領取
相關推薦HOT
主站蜘蛛池模板: 国产女合集| 免费人成在线观看网站| 免费无遮挡肉动漫在线观看| 免费看的一级毛片| 精品国产免费一区二区三区| 一个人看的www视频免费在线观看| 国产线路中文字幕| 日本韩国在线视频| 国产女人18毛片水真多18精品| 日本三级s电影| 玖玖玖影院| 好男人好视频手机在线| 国产传媒一区二区三区呀| 亚洲va久久久噜噜噜久久狠狠| 精品精品国产高清a毛片| 伊人久久精品亚洲午夜| 国产成人精品综合在线观看| 波多野结衣与老人公569| 国产大片91精品免费看3| 久久久久久久99精品免费观看 | 一级毛片视频在线| 男人的天堂毛片| 天天综合网天天综合色| 在线观看高嫁肉柳1一4集中文| 欧美视频亚洲视频| 国产精品一区二区av| 两性高清性色生活片性高清←片| 免费又黄又硬又爽大片| 中文字字幕在线精品乱码app| 无毒不卡在线观看| 亚洲免费视频一区二区三区| 篠田优在线一区中文字幕| 久久青青草原精品影院| 日本一道本在线视频| 日本一本高清| 大胸校花被老头粗暴在线观看 | 色cccwww| 男的把j放进女人下面视频免费| 色妞色妞| 天天做天天摸天天爽天天爱| 免费人成在线|