溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

Golang中常見加密算法有哪些

發(fā)布時(shí)間:2023-05-10 15:23:56 來源:億速云 閱讀:109 作者:iii 欄目:開發(fā)技術(shù)

本文小編為大家詳細(xì)介紹“Golang中常見加密算法有哪些”,內(nèi)容詳細(xì),步驟清晰,細(xì)節(jié)處理妥當(dāng),希望這篇“Golang中常見加密算法有哪些”文章能幫助大家解決疑惑,下面跟著小編的思路慢慢深入,一起來學(xué)習(xí)新知識(shí)吧。

1.md5 加密——不可逆

MD5信息摘要算法是一種被廣泛使用的密碼散列函數(shù),可以產(chǎn)生出一個(gè)128位(16進(jìn)制,32個(gè)字符)的散列值(hash value),用于確保信息傳輸完整一致。

import (
    "crypto/md5"
    "encoding/hex"
    "fmt"
)

第一種

// MD5Str md5驗(yàn)證
func MD5Str(src string) string {
    h := md5.New()
    h.Write([]byte(src)) // 需要加密的字符串為
    fmt.Printf("%s\n", hex.EncodeToString(h.Sum(nil))) // 輸出加密結(jié)果
    return hex.EncodeToString(h.Sum(nil))
}

第二種

// MD5Str2 md5驗(yàn)證
func MD5Str2(src string) string {
    return fmt.Sprintf("%x", md5.Sum([]byte(src)))
}

2.hmacsha 加密——不可逆

HMAC是密鑰相關(guān)的哈希運(yùn)算消息認(rèn)證碼(Hash-based Message Authentication Code)的縮寫,它通過一個(gè)標(biāo)準(zhǔn)算法,在計(jì)算哈希的過程中,把key混入計(jì)算過程中。

和我們自定義的加salt算法不同,Hmac算法針對所有哈希算法都通用,無論是MD5還是SHA-1。采用Hmac替代我們自己的salt算法,可以使程序算法更標(biāo)準(zhǔn)化,也更安全。

hmac-md5加密

//key隨意設(shè)置 data 要加密數(shù)據(jù)
 
func Hmac(key, data string) string {
    // 創(chuàng)建對應(yīng)的md5哈希加密算法
    hash:= hmac.New(md5.New, []byte(key)) 
 
    hash.Write([]byte(data))
 
    return hex.EncodeToString(hash.Sum([]byte("")))
 
}

hamacsha1 加密

// HmacSha1 hmacSha1加密 key隨意設(shè)置 data 要加密數(shù)據(jù)
func HmacSha1(src, key string) string {
    m := hmac.New(sha1.New, []byte(key))
    m.Write([]byte(src))
    return hex.EncodeToString(m.Sum(nil))
}

hamacsha 256 加密

// HmacSHA256 hmacSha256驗(yàn)證  key隨意設(shè)置 data 要加密數(shù)據(jù)
func HmacSHA256(key, src string) string {
    m := hmac.New(sha256.New, []byte(key))
    m.Write([]byte(src))
    return hex.EncodeToString(m.Sum(nil))
}

hmacsha512加密

// HmacSHA512 hmacSha512驗(yàn)證
func HmacSHA512(key, src string) string {
    m := hmac.New(sha512.New, []byte(key))
    m.Write([]byte(src))
    return hex.EncodeToString(m.Sum(nil))
}

hamasha 調(diào)用

package main
 
import (
    "crypto/hmac"
    "crypto/md5"
    "crypto/sha1"
    "crypto/sha256"
    "crypto/sha512"
    "encoding/hex"
    "fmt"
)
 
// Hmac hmac驗(yàn)證 key隨意設(shè)置 data 要加密數(shù)據(jù)
func Hmac(key, data string) string {
 
    hash := hmac.New(md5.New, []byte(key)) // 創(chuàng)建對應(yīng)的md5哈希加密算法
 
    hash.Write([]byte(data))
 
    return hex.EncodeToString(hash.Sum([]byte("")))
 
}
 
// HmacSHA256 hmacSha256加密  key隨意設(shè)置 data 要加密數(shù)據(jù)
func HmacSHA256(key, src string) string {
    m := hmac.New(sha256.New, []byte(key))
    m.Write([]byte(src))
    return hex.EncodeToString(m.Sum(nil))
}
 
// HmacSHA512 hmacSha512加密 key隨意設(shè)置 data 要加密數(shù)據(jù)
func HmacSHA512(key, src string) string {
    m := hmac.New(sha512.New, []byte(key))
    m.Write([]byte(src))
    return hex.EncodeToString(m.Sum(nil))
}
 
// HmacSha1 hmacSha1加密 key隨意設(shè)置 data 要加密數(shù)據(jù)
func HmacSha1(src, key string) string {
    m := hmac.New(sha1.New, []byte(key))
    m.Write([]byte(src))
    return hex.EncodeToString(m.Sum(nil))
}
 
// SHA256Str sha256加密
func SHA256Str(src string) string {
    h := sha256.New()
    h.Write([]byte(src)) // 需要加密的字符串為
    // fmt.Printf("%s\n", hex.EncodeToString(h.Sum(nil))) // 輸出加密結(jié)果
    return hex.EncodeToString(h.Sum(nil))
}
 
func main() {
    hmac_ := Hmac("hybpjx", "始識(shí)")
    hamcsha1 := HmacSha1("hybpjx", "始識(shí)")
    hamcsha256 := HmacSHA256("hybpjx", "始識(shí)")
    hamacsha512 := HmacSHA512("hybpjx", "始識(shí)")
    fmt.Println(hmac_)
    fmt.Println(hamcsha1)
    fmt.Println(hamcsha256)
    fmt.Println(hamacsha512)
}

結(jié)果

d8801f70df7891764116e1ac003f7189
60d68e01c8a86f3b87e4e147e9f0fadce2a69661
b3f8ddf991288036864761a55046877adfe4f78ec9a89bb63932af92689b139f
b9b1fca0fe91522482ee1b2161e57d67482af6ef371614365b918c31ce774f9126ed627e378a063145f404ff2de7bd84f8e4798c385662ef4749e58e9209ca63

3.Sha 加密——不可逆

sha1

SHA-1可以生成一個(gè)被稱為消息摘要的160位(20字節(jié))散列值,散列值通常的呈現(xiàn)形式為40個(gè)十六進(jìn)制數(shù)。

func Sha1(data string) string {
    sha1_ := sha1.New()
    sha1_.Write([]byte(data))
    return hex.EncodeToString(sha1_.Sum([]byte("")))
}

sha256

SHA256算法使用的哈希值長度是256位。這是一個(gè)抽象類。此類的唯一實(shí)現(xiàn)是SHA256Managed。

// SHA256 sha256加密
func SHA256(src string) string {
    h := sha256.New()
    // 需要加密的字符串為
    h.Write([]byte(src))
    // fmt.Printf("%s\n", hex.EncodeToString(h.Sum(nil))) // 輸出加密結(jié)果
    return hex.EncodeToString(h.Sum(nil))
}

sha512

SHA (Secure Hash Algorithm,譯作安全散列算法) 是美國國家安全局 (NSA) 設(shè)計(jì),美國國家標(biāo)準(zhǔn)與技術(shù)研究院 (NIST) 發(fā)布的一系列密碼散列函數(shù)。

// SHA512 sha512加密
func SHA512(src string) string {
    h := sha512.New()
    // 需要加密的字符串為
    h.Write([]byte(src))
    // fmt.Printf("%s\n", hex.EncodeToString(h.Sum(nil))) // 輸出加密結(jié)果
    return hex.EncodeToString(h.Sum(nil))
}

sha調(diào)用

package main
 
import (
    "crypto/sha1"
    "crypto/sha256"
    "crypto/sha512"
    "encoding/hex"
    "fmt"
)
 
func Sha1(data string) string {
    sha1_ := sha1.New()
    sha1_.Write([]byte(data))
    return hex.EncodeToString(sha1_.Sum([]byte("")))
}
 
// SHA256 sha256加密
func SHA256(src string) string {
    h := sha256.New()
    // 需要加密的字符串為
    h.Write([]byte(src))
    // fmt.Printf("%s\n", hex.EncodeToString(h.Sum(nil))) // 輸出加密結(jié)果
    return hex.EncodeToString(h.Sum(nil))
}
 
// SHA512 sha512加密
func SHA512(src string) string {
    h := sha512.New()
    // 需要加密的字符串為
    h.Write([]byte(src))
    // fmt.Printf("%s\n", hex.EncodeToString(h.Sum(nil))) // 輸出加密結(jié)果
    return hex.EncodeToString(h.Sum(nil))
}
 
func main() {
    _sha1 := Sha1("始識(shí)")
    _sha256 := SHA256("始識(shí)")
    _sha512 := SHA512("始識(shí)")
    fmt.Println(_sha1)
    fmt.Println(_sha256)
    fmt.Println(_sha512)
}

結(jié)果

7bac01cc58a26f3cb280b0466794a89441279946
6ef99e6d3fe34a46afcdc438435728fe95ffdab18e389ddd31609edd6729b11d
0c04e9b79f488646d0eac6f65468248507939d643cc92709b14eb0d18d8f13db509ed5ccd3312d6c234408185a4611a42525dce9e8d32255640f56a2f836635a

4.base 加密 解密

加密

// BASE64StdEncode base編碼
func BASE64StdEncode(src string) string {
    return base64.StdEncoding.EncodeToString([]byte(src))
}

解密

// BASE64StdDecode base解碼
func BASE64StdDecode(src string) string {
    a, err := base64.StdEncoding.DecodeString(src)
    if err != nil {
        _ = fmt.Errorf("解密失敗,%v\n", err)
    }
    return string(a)
}

base64 調(diào)用

package main
 
import (
    "encoding/base64"
    "fmt"
)
 
// BASE64StdEncode base編碼
func BASE64StdEncode(src string) string {
    return base64.StdEncoding.EncodeToString([]byte(src))
}
 
// BASE64StdDecode base解碼
func BASE64StdDecode(src string) string {
    a, err := base64.StdEncoding.DecodeString(src)
    if err != nil {
        _ = fmt.Errorf("解密失敗,%v\n", err)
    }
    return string(a)
}
 
func main() {
    encodeBase64 := BASE64StdEncode("hybpjx")
    decodeBase64 := BASE64StdDecode(encodeBase64)
    fmt.Println(encodeBase64)
    fmt.Println(decodeBase64)
}

結(jié)果

aHlicGp4
hybpjx

5.AES 加密

高級(jí)加密標(biāo)準(zhǔn)(英語:Advanced Encryption Standard,縮寫:AES),在密碼學(xué)中又稱Rijndael加密法。這個(gè)標(biāo)準(zhǔn)用來替代原先的DES,已經(jīng)被多方分析且廣為全世界所使用。經(jīng)過五年的甄選流程,于2001年11月26日發(fā)布于FIPS PUB 197,并在2002年5月26日成為有效的標(biāo)準(zhǔn)。2006年,高級(jí)加密標(biāo)準(zhǔn)已然成為對稱密鑰加密中最流行的算法之一。

由于加密和解密的秘鑰是相同的,所以AES為對稱加密

package main
 
import (
    "bytes"
    "crypto/aes"
    "crypto/cipher"
    "encoding/base64"
    "fmt"
)
 
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)]
}
 
//AES加密
func AesEncrypt(origData, key []byte) ([]byte, error) {
    block, err := aes.NewCipher(key)
    if err != nil {
        return nil, err
    }
    blockSize := block.BlockSize()
    origData = PKCS7Padding(origData, blockSize)
    blockMode := cipher.NewCBCEncrypter(block, key[:blockSize])
    crypted := make([]byte, len(origData))
    blockMode.CryptBlocks(crypted, origData)
    return crypted, nil
}
 
//AES解密
func AesDecrypt(crypted, key []byte) ([]byte, error) {
    block, err := aes.NewCipher(key)
    if err != nil {
        return nil, err
    }
    blockSize := block.BlockSize()
    blockMode := cipher.NewCBCDecrypter(block, key[:blockSize])
    origData := make([]byte, len(crypted))
    blockMode.CryptBlocks(origData, crypted)
    origData = PKCS7UnPadding(origData)
    return origData, nil
}
 
func main() {
    text := "今晚打老虎"
    AesKey := []byte("0f90023fc9ae101e") //秘鑰長度為16的倍數(shù)
    fmt.Printf("明文: %s\n秘鑰: %s\n", text, string(AesKey))
    encrypted, err := AesEncrypt([]byte(text), AesKey)
    if err != nil {
        panic(err)
    }
    fmt.Printf("加密后: %s\n", base64.StdEncoding.EncodeToString(encrypted))
    origin, err := AesDecrypt(encrypted, AesKey)
    if err != nil {
        panic(err)
    }
    fmt.Printf("解密后明文: %s\n", string(origin))
}

CBC方式

package main
 
import (
    "bytes"
    "crypto/aes"
    "crypto/cipher"
    "encoding/base64"
    "encoding/hex"
    "log"
)
 
func AesEncryptCBC(origData []byte, key []byte) (encrypted []byte) {
    // 分組秘鑰
    // NewCipher該函數(shù)限制了輸入k的長度必須為16, 24或者32
    block, _ := aes.NewCipher(key)
    blockSize := block.BlockSize()                              // 獲取秘鑰塊的長度
    origData = pkcs5Padding(origData, blockSize)                // 補(bǔ)全碼
    blockMode := cipher.NewCBCEncrypter(block, key[:blockSize]) // 加密模式
    encrypted = make([]byte, len(origData))                     // 創(chuàng)建數(shù)組
    blockMode.CryptBlocks(encrypted, origData)                  // 加密
    return encrypted
}
func AesDecryptCBC(encrypted []byte, key []byte) (decrypted []byte) {
    block, _ := aes.NewCipher(key)                              // 分組秘鑰
    blockSize := block.BlockSize()                              // 獲取秘鑰塊的長度
    blockMode := cipher.NewCBCDecrypter(block, key[:blockSize]) // 加密模式
    decrypted = make([]byte, len(encrypted))                    // 創(chuàng)建數(shù)組
    blockMode.CryptBlocks(decrypted, encrypted)                 // 解密
    decrypted = pkcs5UnPadding(decrypted)                       // 去除補(bǔ)全碼
    return decrypted
}
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(origData []byte) []byte {
    length := len(origData)
    unpadding := int(origData[length-1])
    return origData[:(length - unpadding)]
}
func main() {
    origData := []byte("460154561234") // 待加密的數(shù)據(jù)
    key := []byte("9876787656785679")  // 加密的密鑰
    log.Println("原文:", string(origData))
 
    log.Println("------------------ CBC模式 --------------------")
    encrypted := AesEncryptCBC(origData, key)
    log.Println("密文(hex):", hex.EncodeToString(encrypted))
    log.Println("密文(base64):", base64.StdEncoding.EncodeToString(encrypted))
    decrypted := AesDecryptCBC(encrypted, key)
    log.Println("解密結(jié)果:", string(decrypted))
}

ECB方式

package main
 
import (
    "crypto/aes"
    "encoding/base64"
    "encoding/hex"
    "log"
)
 
 
func AesEncryptECB(origData []byte, key []byte) (encrypted []byte) {
    cipher, _ := aes.NewCipher(generateKey(key))
    length := (len(origData) + aes.BlockSize) / aes.BlockSize
    plain := make([]byte, length*aes.BlockSize)
    copy(plain, origData)
    pad := byte(len(plain) - len(origData))
    for i := len(origData); i < len(plain); i++ {
        plain[i] = pad
    }
    encrypted = make([]byte, len(plain))
    // 分組分塊加密
    for bs, be := 0, cipher.BlockSize(); bs <= len(origData); bs, be = bs+cipher.BlockSize(), be+cipher.BlockSize() {
        cipher.Encrypt(encrypted[bs:be], plain[bs:be])
    }
 
    return encrypted
}
func AesDecryptECB(encrypted []byte, key []byte) (decrypted []byte) {
    cipher, _ := aes.NewCipher(generateKey(key))
    decrypted = make([]byte, len(encrypted))
    //
    for bs, be := 0, cipher.BlockSize(); bs < len(encrypted); bs, be = bs+cipher.BlockSize(), be+cipher.BlockSize() {
        cipher.Decrypt(decrypted[bs:be], encrypted[bs:be])
    }
 
    trim := 0
    if len(decrypted) > 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() {
    origData := []byte("460154561234") // 待加密的數(shù)據(jù)
    key := []byte("9876787656785679")  // 加密的密鑰
    log.Println("原文:", string(origData))
 
    log.Println("------------------ ECB模式 --------------------")
    encrypted := AesEncryptECB(origData, key)
    log.Println("密文(hex):", hex.EncodeToString(encrypted))
    log.Println("密文(base64):", base64.StdEncoding.EncodeToString(encrypted))
    decrypted := AesDecryptECB(encrypted, key)
    log.Println("解密結(jié)果:", string(decrypted))
}

CFB 方式

package main
 
import (
    "crypto/aes"
    "crypto/cipher"
    "crypto/rand"
    "encoding/base64"
    "encoding/hex"
    "io"
    "log"
)
 
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() {
    origData := []byte("460154561234") // 待加密的數(shù)據(jù)
    key := []byte("9876787656785679")  // 加密的密鑰
    log.Println("原文:", string(origData))
 
    log.Println("------------------ CFB模式 --------------------")
    encrypted := AesEncryptCFB(origData, key)
    log.Println("密文(hex):", hex.EncodeToString(encrypted))
    log.Println("密文(base64):", base64.StdEncoding.EncodeToString(encrypted))
    decrypted := AesDecryptCFB(encrypted, key)
    log.Println("解密結(jié)果:", string(decrypted))
}

6.RSA加密

RSA是一種基于公鑰密碼體制的優(yōu)秀加密算法,1978年由美國(MIT)的李維斯特(Rivest)、沙米爾(Shamir)、艾德曼(Adleman)提的。

RSA算法是一種分組密碼體制算法,它的保密強(qiáng)度是建立在具有大素?cái)?shù)因子的合數(shù)其因子分解是困難的(基于大數(shù)分解的難度)。公鑰和私鑰是一對大素?cái)?shù)的函數(shù),從一個(gè)公鑰和密文中恢復(fù)出明文的難度等價(jià)于分解兩個(gè)大素?cái)?shù)之積。

RSA得到了世界上的最廣泛的應(yīng)用,ISO在1992年頒布的國際標(biāo)準(zhǔn)X.509中,將RSA算法正式納入國際標(biāo)準(zhǔn)。

RSA加密

package main
 
import (
    "crypto/rand"
    "crypto/rsa"
    "crypto/x509"
    "encoding/pem"
    "fmt"
    "os"
)
 
 
 
 
// GenerateRSAKey 生成RSA私鑰和公鑰,保存到文件中
func GenerateRSAKey(bits int){
    //GenerateKey函數(shù)使用隨機(jī)數(shù)據(jù)生成器random生成一對具有指定字位數(shù)的RSA密鑰
    //Reader是一個(gè)全局、共享的密碼用強(qiáng)隨機(jī)數(shù)生成器
    privateKey, err := rsa.GenerateKey(rand.Reader, bits)
    if err!=nil{
        panic(err)
    }
    //保存私鑰
    //通過x509標(biāo)準(zhǔn)將得到的ras私鑰序列化為ASN.1 的 DER編碼字符串
    // X509PrivateKey := x509.MarshalPKCS1PrivateKey(privateKey) // PKCS1 和 9 是不一致的
    X509PrivateKey,err := x509.MarshalPKCS8PrivateKey(privateKey)
    if err != nil {
        fmt.Println(err.Error())
        os.Exit(0)
    }
    //使用pem格式對x509輸出的內(nèi)容進(jìn)行編碼
    //創(chuàng)建文件保存私鑰
    privateFile, err := os.Create("private.pem")
    if err!=nil{
        panic(err)
    }
    defer privateFile.Close()
    //構(gòu)建一個(gè)pem.Block結(jié)構(gòu)體對象
    privateBlock:= pem.Block{Type: "PRIVATE KEY",Bytes:X509PrivateKey}
    //將數(shù)據(jù)保存到文件
    pem.Encode(privateFile,&privateBlock)
    //保存公鑰
    //獲取公鑰的數(shù)據(jù)
    publicKey:=privateKey.PublicKey
    //X509對公鑰編碼
    X509PublicKey,err:=x509.MarshalPKIXPublicKey(&publicKey)
    if err!=nil{
        panic(err)
    }
    //pem格式編碼
    //創(chuàng)建用于保存公鑰的文件
    publicFile, err := os.Create("public.pem")
    if err!=nil{
        panic(err)
    }
    defer publicFile.Close()
    //創(chuàng)建一個(gè)pem.Block結(jié)構(gòu)體對象
    publicBlock:= pem.Block{Type: "Public Key",Bytes:X509PublicKey}
    //保存到文件
    pem.Encode(publicFile,&publicBlock)
}
 
// RsaEncrypt RSA加密
func RsaEncrypt(plainText []byte,path string)[]byte{
    //打開文件
    file,err:=os.Open(path)
    if err!=nil{
        panic(err)
    }
    defer file.Close()
    //讀取文件的內(nèi)容
    info, _ := file.Stat()
    buf:=make([]byte,info.Size())
    file.Read(buf)
    //pem解碼
    block, _ := pem.Decode(buf)
    //x509解碼
    publicKeyInterface, err := x509.ParsePKIXPublicKey(block.Bytes)
    if err!=nil{
        panic(err)
    }
    //類型斷言
    publicKey:=publicKeyInterface.(*rsa.PublicKey)
    //對明文進(jìn)行加密
    cipherText, err := rsa.EncryptPKCS1v15(rand.Reader, publicKey, plainText)
    if err!=nil{
        panic(err)
    }
    //返回密文
    return cipherText
}
 
// RsaDecrypt RSA解密
func RsaDecrypt(cipherText []byte,path string) []byte{
    //打開文件
    file,err:=os.Open(path)
    if err!=nil{
        panic(err)
    }
    defer file.Close()
    //獲取文件內(nèi)容
    info, _ := file.Stat()
    buf:=make([]byte,info.Size())
    file.Read(buf)
    //pem解碼
    block, _ := pem.Decode(buf)
    //X509解碼
    privateKey, err := x509.ParsePKCS8PrivateKey(block.Bytes)
    if err!=nil{
        fmt.Println(err.Error())
        os.Exit(0)
    }
    //對密文進(jìn)行解密
    plainText,_:=rsa.DecryptPKCS1v15(rand.Reader,privateKey.(*rsa.PrivateKey),cipherText)
    //返回明文
    return plainText
}
 
 
func main(){
    // RSA/ECB/PKCS1Padding
    // RSA是算法,ECB是分塊模式,PKCS1Padding是填充模式
 
    // pkcs1私鑰生成openssl genrsa -out pkcs1.pem 1024
    // pkcs1轉(zhuǎn)pkcs8私鑰 :openssl pkcs8 -in pkcs8.pem -nocrypt -out pkcs1.pem
 
    // pkcs1 BEGIN RSA PRIVATE KEY
    // pkcs8 BEGIN PRIVATE KEY
 
    GenerateRSAKey(1024)
    publicPath := "public_key.pem"
    privatePath := "private_key.pem"
 
    publicPath = "public.pem"
    privatePath = "private.pem"
 
    txt := []byte("hello")
    encrptTxt := RsaEncrypt(txt,publicPath)
    decrptCode := RsaDecrypt(encrptTxt,privatePath)
    fmt.Println(string(decrptCode))
 
}

RSA分段加密

package main
 
import (
    "bytes"
    "crypto/rand"
    "crypto/rsa"
    "crypto/x509"
    "encoding/base64"
    "encoding/pem"
    "fmt"
    "log"
    "os"
)
 
func main() {
    GenerateRSAKey(2048)
    publicPath := "public.pem"
    privatePath := "private.pem"
    var a = []byte("hello")
    encrptTxt, err := RsaEncryptBlock(a, publicPath)
    if err != nil {
        fmt.Println(err.Error())
    }
    encodeString := base64.StdEncoding.EncodeToString(encrptTxt)
    decodeByte, err := base64.StdEncoding.DecodeString(encodeString)
    if err != nil {
        panic(err)
    }
    //生成RSA私鑰和公鑰,保存到文件中
    decrptCode := RSA_Decrypts(decodeByte, privatePath)
    fmt.Println(string(decrptCode))
 
}
 
 
func GenerateRSAKey(bits int) {
    //GenerateKey函數(shù)使用隨機(jī)數(shù)據(jù)生成器random生成一對具有指定字位數(shù)的RSA密鑰
    //Reader是一個(gè)全局、共享的密碼用強(qiáng)隨機(jī)數(shù)生成器
    privateKey, err := rsa.GenerateKey(rand.Reader, bits)
    if err != nil {
        panic(err)
    }
    //保存私鑰
    //通過x509標(biāo)準(zhǔn)將得到的ras私鑰序列化為ASN.1 的 DER編碼字符串
    // X509PrivateKey := x509.MarshalPKCS1PrivateKey(privateKey) // PKCS1 和 9 是不一致的
    X509PrivateKey, err := x509.MarshalPKCS8PrivateKey(privateKey)
    if err != nil {
        fmt.Println(err.Error())
        os.Exit(0)
    }
    //使用pem格式對x509輸出的內(nèi)容進(jìn)行編碼
    //創(chuàng)建文件保存私鑰
    privateFile, err := os.Create("private.pem")
    if err != nil {
        panic(err)
    }
    defer privateFile.Close()
    //構(gòu)建一個(gè)pem.Block結(jié)構(gòu)體對象
    privateBlock := pem.Block{Type: "PRIVATE KEY", Bytes: X509PrivateKey}
    //將數(shù)據(jù)保存到文件
    pem.Encode(privateFile, &privateBlock)
    //保存公鑰
    //獲取公鑰的數(shù)據(jù)
    publicKey := privateKey.PublicKey
    //X509對公鑰編碼
    X509PublicKey, err := x509.MarshalPKIXPublicKey(&publicKey)
    if err != nil {
        panic(err)
    }
    //pem格式編碼
    //創(chuàng)建用于保存公鑰的文件
    publicFile, err := os.Create("public.pem")
    if err != nil {
        panic(err)
    }
    defer publicFile.Close()
    //創(chuàng)建一個(gè)pem.Block結(jié)構(gòu)體對象
    publicBlock := pem.Block{Type: "Public Key", Bytes: X509PublicKey}
    //保存到文件
    pem.Encode(publicFile, &publicBlock)
}
 
// RSA_Decrypts RSA解密支持分段解密
func RSA_Decrypts(cipherText []byte, path string) []byte {
    //打開文件
    var bytesDecrypt []byte
    file, err := os.Open(path)
    if err != nil {
        panic(err)
    }
    defer file.Close()
    //獲取文件內(nèi)容
    info, _ := file.Stat()
    buf := make([]byte, info.Size())
    file.Read(buf)
    //pem解碼
    block, _ := pem.Decode(buf)
    //X509解碼
    privateKey, err := x509.ParsePKCS8PrivateKey(block.Bytes)
    if err != nil {
        fmt.Println(err.Error())
        os.Exit(0)
    }
    p := privateKey.(*rsa.PrivateKey)
    keySize := p.Size()
    srcSize := len(cipherText)
    log.Println("密鑰長度", keySize, "密文長度", srcSize)
    var offSet = 0
    var buffer = bytes.Buffer{}
    for offSet < srcSize {
        endIndex := offSet + keySize
        if endIndex > srcSize {
            endIndex = srcSize
        }
        bytesOnce, err := rsa.DecryptPKCS1v15(rand.Reader, p, cipherText[offSet:endIndex])
        if err != nil {
            return nil
        }
        buffer.Write(bytesOnce)
        offSet = endIndex
    }
    bytesDecrypt = buffer.Bytes()
    return bytesDecrypt
}
 
// RsaEncryptBlock 公鑰加密-分段
func RsaEncryptBlock(src []byte, path string) (bytesEncrypt []byte, err error) {
    //打開文件
    file, err := os.Open(path)
    if err != nil {
        panic(err)
    }
    defer file.Close()
    //讀取文件的內(nèi)容
    info, _ := file.Stat()
    buf := make([]byte, info.Size())
    file.Read(buf)
    //pem解碼
    block, _ := pem.Decode(buf)
    //x509解碼
    publicKeyInterface, err := x509.ParsePKIXPublicKey(block.Bytes)
    if err != nil {
        panic(err)
    }
    //類型斷言
    publicKey := publicKeyInterface.(*rsa.PublicKey)
    keySize, srcSize := publicKey.Size(), len(src)
    log.Println("密鑰長度", keySize, "明文長度", srcSize)
    offSet, once := 0, keySize-11
    buffer := bytes.Buffer{}
    for offSet < srcSize {
        endIndex := offSet + once
        if endIndex > srcSize {
            endIndex = srcSize
        }
        // 加密一部分
        bytesOnce, err := rsa.EncryptPKCS1v15(rand.Reader, publicKey, src[offSet:endIndex])
        if err != nil {
            return nil, err
        }
        buffer.Write(bytesOnce)
        offSet = endIndex
    }
    bytesEncrypt = buffer.Bytes()
    return
}

7.DES加密

DES(Data Encryption)是1977年美國聯(lián)邦信息處理標(biāo)準(zhǔn)(FIPS)中所采用的一種對稱密碼(FIPS46-3)。隨著計(jì)算機(jī)的進(jìn)步,DES已經(jīng)能夠被暴力破解,1997年的DES Challenge I 中用了96天破譯密鑰,1998年的DES Challenge II-1中用了41天,1998年的DES Challenge II-2中用了56小時(shí),1999年的DES Challenge III 中只用了22小時(shí)15分鐘。

DES是一種將64比特的明文加密成64比特的密文的對稱密碼算法,它的密鑰的長度是56比特。盡管從規(guī)格上來說,DES的密鑰長度是64比特,但由于每隔7比特會(huì)設(shè)置一個(gè)用于錯(cuò)誤檢查的比特,因此實(shí)質(zhì)上其密鑰長度是56比特。

DES 是以64比特的明文(比特序列)為一個(gè)單位來進(jìn)行加密的,這個(gè)64比特的單位稱為分組 ,一般來說,以分組為單位進(jìn)行處理的密碼算法稱為分組密碼,DES就是分組密碼的一種。

DES每次只能加密64比特的數(shù)據(jù),如果要加密的明文比較長,就需要對DES加密進(jìn)行迭代(反復(fù)),而迭代的具體方式就稱為模式。

DES 內(nèi)部實(shí)現(xiàn)理論:在 des 中各個(gè)步驟稱為輪,整個(gè)加密過程進(jìn)行16輪循環(huán)。

內(nèi)置庫完成

加密模式采用ECB、填充方式采用pkcs5padding、密碼使用"12345678",輸出時(shí)經(jīng)hex編碼。自己可以通過一些在線測試工具進(jìn)行測試,看結(jié)果是否一致。

package main
 
import (
    "bytes"
    "crypto/cipher"
    "crypto/des"
    "encoding/hex"
    "fmt"
)
 
func main() {
    data := []byte("hello world")
    key := []byte("12345678")
    iv := []byte("43218765")
 
    result, err := DesCBCEncrypt(data, key, iv)
    if err != nil {
        fmt.Println(err)
    }
    b := hex.EncodeToString(result)
    fmt.Println(b)
}
 
func DesCBCEncrypt(data, key, iv []byte) ([]byte, error) {
    block, err := des.NewCipher(key)
    if err != nil {
        return nil, err
    }
 
    data = pkcs5Padding(data, block.BlockSize())
    cryptText := make([]byte, len(data))
 
    blockMode := cipher.NewCBCEncrypter(block, iv)
    blockMode.CryptBlocks(cryptText, data)
    return cryptText, nil
}
 
func pkcs5Padding(cipherText []byte, blockSize int) []byte {
    padding := blockSize - len(cipherText)%blockSize
    padText := bytes.Repeat([]byte{byte(padding)}, padding)
    return append(cipherText, padText...)
}

使用第三方庫

package main
 
import (
    "fmt"
    "github.com/marspere/goencrypt"
)
 
func main() {
    // key為12345678
    // iv為空
    // 采用ECB分組模式
    // 采用pkcs5padding填充模式
    // 輸出結(jié)果使用base64進(jìn)行加密
    cipher := goencrypt.NewDESCipher([]byte("12345678"), []byte(""), goencrypt.ECBMode, goencrypt.Pkcs7, goencrypt.PrintBase64)
    cipherText, err := cipher.DESEncrypt([]byte("hello world"))
    if err != nil {
        fmt.Println(err)
        return
    }
    fmt.Println(cipherText)
}

8.3DES加密算法

3DES(或稱為Triple DES)是三重?cái)?shù)據(jù)加密算法(TDEA,Triple Data Encryption Algorithm)塊密碼的通稱。它相當(dāng)于是對每個(gè)數(shù)據(jù)塊應(yīng)用三次DES加密算法。
由于計(jì)算機(jī)運(yùn)算能力的增強(qiáng),原版DES密碼的密鑰長度變得容易被暴力破解;3DES即是設(shè)計(jì)用來提供一種相對簡單的方法,即通過增加DES的密鑰長度來避免類似的攻擊,而不是設(shè)計(jì)一種全新的塊密碼算法。

還有一個(gè)庫 非常NB

ECB模式下的3DES算法加解密信息,golang默認(rèn)只提供CBC模式

安裝:

go get github.com/thinkoner/openssl

代碼如下:

package main
 
import (
    "encoding/base64"
    "encoding/hex"
    "fmt"
    "github.com/forgoer/openssl"
)
 
func main() {
 
    //定義密鑰,必須是24byte
    key := []byte("123456789012345678901234")
    fmt.Println("密鑰:", key, hex.EncodeToString(key))
 
    //定義明文
    src := []byte("0102030109000000030000000F8898E37A7F8F3D742006111118080000FACE05")
 
    //3DES-ECB加密
    encodeData, _ := openssl.Des3ECBEncrypt(src, key, openssl.ZEROS_PADDING)
    encryptBaseData := base64.StdEncoding.EncodeToString(encodeData)
 
    fmt.Println("加密后Base64:", encryptBaseData)
    fmt.Println("加密后Hex:", hex.EncodeToString(encodeData))
 
    //3DES-ECB解密
    decodeBaseData, _ := base64.StdEncoding.DecodeString(encryptBaseData)
    decodeData, _ := openssl.Des3ECBDecrypt(decodeBaseData, key, openssl.ZEROS_PADDING)
 
    fmt.Println("解密后:", hex.EncodeToString(decodeData))
}

包括 Des的加密解密

以下只舉一個(gè)例子

srcData := "L0j+JvbeVM0svSpjIwXdE7yTu78wiEszCmW8rwjXY3vrx2nEaUeJ/Rw/c/IRdlxIH+/ro4pykx6ESOkGU1YwM8ddEuuoTg5uPsqQ9/SuNds="
key := []byte("Ctpsp@884*"[:8])
//3DES-ECB解密
decodeBaseData, _ := base64.StdEncoding.DecodeString(srcData)
decodeData, _ := openssl.DesECBDecrypt(decodeBaseData, key, openssl.PKCS5_PADDING)
fmt.Println("解密后:", string(decodeData))

讀到這里,這篇“Golang中常見加密算法有哪些”文章已經(jīng)介紹完畢,想要掌握這篇文章的知識(shí)點(diǎn)還需要大家自己動(dòng)手實(shí)踐使用過才能領(lǐng)會(huì),如果想了解更多相關(guān)內(nèi)容的文章,歡迎關(guān)注億速云行業(yè)資訊頻道。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI