Go语言使用对称加密的示例详解

2022-06-06 14:49:50
目录
介绍AES 算法实践总结

介绍

在项目开发中,我们经常会遇到需要使用对称密钥加密的场景,比如客户端调用接口时,参数包含手机号、身份证号或银行卡号等。

对称密钥加密是一种加密方式,其中只有一个密钥用于加密和解密数据。通过对称加密进行通信的实体必须共享该密钥,以便可以在解密过程中使用它。这种加密方法与非对称加密不同,非对称加密使用一对密钥(一个公钥和一个私钥)来加密和解密数据。

AES>

常见的对称密钥加密算法有 AES (Advanced Encryption Standard),DES (Data Encryption Standard) 等,它们都属于分组密码。

因为基于目前计算机的处理能力,可以很快破解 DES 算法,所以 DES 目前已经很少被使用。

AES 是目前最常用的对称密钥加密算法,最初称为 Rijndael。AES 密码每个分组大小是 128 bits,但是它具有三种密钥长度,分别是 AES-128、AES-192 和 AES-256。需要注意的是,在 Golang 标准库提供的接口中,仅支持 AES-128(16 byte),实际上 AES-128 的加密强度已经足够安全。

本文我们主要介绍 Golang 中怎么使用 AES 算法的对称密钥加密。

实践

AES>

需要注意的是,ECB 和 CBC 分组模式的最后一个分组,需要填充满 16 byte,关于填充模式,限于篇幅,本文不展开介绍,但会提供填充数据和取消填充数据的代码。

Golang 实现 AES 对称加密算法主要分为以下几个步骤:

加密步骤:

    创建一个新的加密块。获取加密块的大小。填充数据。初始化向量。指定加密块的分组模式。进行加密多个块。

    示例代码:

    func AESCbcEncrypt(secretKey, src string) string {
     key := []byte(secretKey)
     if len(key) > 16 {
      key = key[:16]
     }
     plaintext := []byte(src)
     block, err := aes.NewCipher(key)
     if err != nil {
      panic(err)
     }
     blockSize := block.BlockSize()
     plaintext = Padding(plaintext, blockSize)
     if len(plaintext)%aes.BlockSize != 0 {
      panic("plaintext is not a multiple of the block size")
     }
     ciphertext := make([]byte, aes.BlockSize+len(plaintext))
     iv := ciphertext[:aes.BlockSize]
     if _, err := io.ReadFull(rand.Reader, iv); err != nil {
      panic(err)
     }
     mode := cipher.NewCBCEncrypter(block, iv)
     mode.CryptBlocks(ciphertext[aes.BlockSize:], plaintext)
     return base64.StdEncoding.EncodeToString(ciphertext)
    }
    

    解密步骤:

      创建一个新的加密块。初始化向量。指定解密块的分组模式。进行解密多个块。取消填充数据。

      示例代码:

      func AESCbcDecrypt(secretKey, src string) string {
       key := []byte(secretKey)
       ciphertext, _ := base64.StdEncoding.DecodeString(src)
       block, err := aes.NewCipher(key)
       if err != nil {
        panic(err)
       }
       if len(ciphertext) < aes.BlockSize {
        panic("ciphertext too short")
       }
       iv := ciphertext[:aes.BlockSize]
       ciphertext = ciphertext[aes.BlockSize:]
       if len(ciphertext)%aes.BlockSize != 0 {
        panic("ciphertext is not a multiple of the block size")
       }
       mode := cipher.NewCBCDecrypter(block, iv)
       mode.CryptBlocks(ciphertext, ciphertext)
       ciphertext = UnPadding(ciphertext)
       return string(ciphertext)
      }
      

      填充示例代码:

      func Padding(plainText []byte, blockSize int) []byte {
       padding := blockSize - len(plainText)%blockSize
       char := []byte{byte(padding)}
       newPlain := bytes.Repeat(char, padding)
       return append(plainText, newPlain...)
      }
      

      取消填充示例代码:

      func UnPadding(plainText []byte) []byte {
       length := len(plainText)
       lastChar := plainText[length-1]
       padding := int(lastChar)
       return plainText[:length-padding]
      }
      

      需要注意的是,初始化向量(IV)是随机的,细心的读者朋友们可能已经发现,使用随机 IV ,同一份明文,每次加密得到的密文也都不同。但是,加密和解密使用的 IV 必须相同。

      总结

      我们介绍了对称密钥加密的概念,并简单介绍了>

      到此这篇关于Go语言使用对称加密的示例详解的文章就介绍到这了,更多相关Go语言对称加密内容请搜索易采站长站以前的文章或继续浏览下面的相关文章希望大家以后多多支持易采站长站!