久久精品国产亚洲高清|精品日韩中文乱码在线|亚洲va中文字幕无码久|伊人久久综合狼伊人久久|亚洲不卡av不卡一区二区|精品久久久久久久蜜臀AV|国产精品19久久久久久不卡|国产男女猛烈视频在线观看麻豆

千鋒教育-做有情懷、有良心、有品質(zhì)的職業(yè)教育機(jī)構(gòu)

手機(jī)站
千鋒教育

千鋒學(xué)習(xí)站 | 隨時(shí)隨地免費(fèi)學(xué)

千鋒教育

掃一掃進(jìn)入千鋒手機(jī)站

領(lǐng)取全套視頻
千鋒教育

關(guān)注千鋒學(xué)習(xí)站小程序
隨時(shí)隨地免費(fèi)學(xué)習(xí)課程

當(dāng)前位置:首頁  >  技術(shù)干貨  > Go語言常用加密算法和加密技術(shù)全面解析!

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

來源:千鋒教育
發(fā)布人:xqq
時(shí)間: 2023-12-27 06:59:54 1703631594

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

在現(xiàn)代計(jì)算機(jī)領(lǐng)域,安全性非常重要,而加密技術(shù)是保證計(jì)算機(jī)信息系統(tǒng)安全性的重要措施之一。為了滿足這個(gè)需求,Go語言提供了豐富的加密算法和加密技術(shù),本文將為大家詳細(xì)解析Go語言中常用的加密算法和加密技術(shù)。

一、對(duì)稱加密算法

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

以下是一個(gè)示例,使用AES加密算法加密字符串,然后再進(jìn)行解密:

`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)

}

二、非對(duì)稱加密算法非對(duì)稱加密算法也稱為公鑰加密算法,加密和解密使用不同的密鑰。常用的非對(duì)稱加密算法有RSA、ECC等。Go語言中的crypto包中也提供了多種非對(duì)稱加密算法。以下是一個(gè)示例,使用RSA加密算法加密字符串,然后再進(jìn)行解密:`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包中也提供了多種哈希算法。

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

`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)

}

四、數(shù)字簽名數(shù)字簽名是一種基于公鑰密碼學(xué)的技術(shù),用來保證消息的完整性、真實(shí)性和不可否認(rèn)性。常用的數(shù)字簽名算法有RSA、ECC等。Go語言中的crypto包中也提供了多種數(shù)字簽名算法。以下是一個(gè)示例,使用RSA算法對(duì)數(shù)據(jù)進(jìn)行數(shù)字簽名,然后再進(jìn)行驗(yàn)證:`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)}

總結(jié):

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

以上就是IT培訓(xùn)機(jī)構(gòu)千鋒教育提供的相關(guān)內(nèi)容,如果您有web前端培訓(xùn),鴻蒙開發(fā)培訓(xùn)python培訓(xùn),linux培訓(xùn),java培訓(xùn),UI設(shè)計(jì)培訓(xùn)等需求,歡迎隨時(shí)聯(lián)系千鋒教育。

tags:
聲明:本站稿件版權(quán)均屬千鋒教育所有,未經(jīng)許可不得擅自轉(zhuǎn)載。
10年以上業(yè)內(nèi)強(qiáng)師集結(jié),手把手帶你蛻變精英
請(qǐng)您保持通訊暢通,專屬學(xué)習(xí)老師24小時(shí)內(nèi)將與您1V1溝通
免費(fèi)領(lǐng)取
今日已有369人領(lǐng)取成功
劉同學(xué) 138****2860 剛剛成功領(lǐng)取
王同學(xué) 131****2015 剛剛成功領(lǐng)取
張同學(xué) 133****4652 剛剛成功領(lǐng)取
李同學(xué) 135****8607 剛剛成功領(lǐng)取
楊同學(xué) 132****5667 剛剛成功領(lǐng)取
岳同學(xué) 134****6652 剛剛成功領(lǐng)取
梁同學(xué) 157****2950 剛剛成功領(lǐng)取
劉同學(xué) 189****1015 剛剛成功領(lǐng)取
張同學(xué) 155****4678 剛剛成功領(lǐng)取
鄒同學(xué) 139****2907 剛剛成功領(lǐng)取
董同學(xué) 138****2867 剛剛成功領(lǐng)取
周同學(xué) 136****3602 剛剛成功領(lǐng)取
相關(guān)推薦HOT
如何通過Golang打造高性能Web應(yīng)用

如何通過Golang打造高性能Web應(yīng)用作為一種快速、高效、安全和簡(jiǎn)單的編程語言,Golang在Web應(yīng)用程序開發(fā)中越來越受歡迎。在本文中,我們將介紹如...詳情>>

2023-12-27 08:10:16
優(yōu)化技巧Go語言中幾種常見的性能優(yōu)化方式

優(yōu)化技巧:Go語言中幾種常見的性能優(yōu)化方式Go語言是一種高效、簡(jiǎn)潔的編程語言,但是在實(shí)際開發(fā)中,我們還是會(huì)遇到一些性能瓶頸,這時(shí)我們就需要...詳情>>

2023-12-27 08:03:14
原理分析Go語言的并發(fā)編程實(shí)現(xiàn)及最佳實(shí)踐

原理分析:Go語言的并發(fā)編程實(shí)現(xiàn)及最佳實(shí)踐隨著計(jì)算機(jī)技術(shù)的不斷發(fā)展和進(jìn)步,軟件開發(fā)也在不斷地演進(jìn)和完善,其中一個(gè)重要的趨勢(shì)就是并發(fā)編程。...詳情>>

2023-12-27 08:01:29
如何使用Golang構(gòu)建高性能Web應(yīng)用

如何使用Golang構(gòu)建高性能Web應(yīng)用隨著互聯(lián)網(wǎng)的快速發(fā)展,高性能Web應(yīng)用已經(jīng)成為許多企業(yè)的追求目標(biāo)。而在這個(gè)過程中,選擇合適的編程語言和框架...詳情>>

2023-12-27 07:36:51
如何在html中引入javascript代碼

在HTML中引入JavaScript代碼有幾種常見的方法,包括內(nèi)聯(lián)腳本、外部腳本文件和模塊腳本。以下是每種方法的示例和說明:1、內(nèi)聯(lián)腳本(Inline Scri...詳情>>

2023-12-27 07:31:56
快速通道