go語言教程哪里有?Go從入門到精通系列視頻4.1?對稱加密算法

?4.1.1核心代碼

DES的加密案例,如下例所示。

例1-1?DES

1?package main

2?import (

3????"bytes"

4????"crypto/cipher"

5????"crypto/des"

6????"encoding/base64"

7????"fmt"

8?)

9?func main(){

10????key := []byte("00000000") //秘鑰只占8個字節(jié)

11????arr := "千鋒教育"

12????fmt.Println("------------DES加密解密字節(jié)數(shù)組")

13????fmt.Println("加密前:",arr)

14????resultArr, _ := DesEncrypt([]byte(arr), key)

15????fmt.Printf("加密后:%x\n", resultArr)

16????resultArr, _ = DesDecrypt(resultArr, key)

17????fmt.Println("解密后:", string(resultArr))

18????fmt.Println("------------DES加密解密字符串")

19????cipherText, _ := DesEncryptString(arr, key)

20????fmt.Println("加密后:" , cipherText)

21????originalText, _ := DesDecryptString(cipherText, key)

22????fmt.Println("解密后:", originalText)

23?

24?}

25?//DES加密字節(jié)數(shù)組,返回字節(jié)數(shù)組

26?func DesEncrypt(originalBytes, key []byte) ([]byte, error) {

27????block, err := des.NewCipher(key)

28????if err != nil {

29???????return nil, err

30????}

31????originalBytes = PKCS5Padding(originalBytes, block.BlockSize())

32????blockMode := cipher.NewCBCEncrypter(block, key)

33????cipherArr := make([]byte, len(originalBytes))

34????blockMode.CryptBlocks(cipherArr, originalBytes)

35????return cipherArr, nil

36?}

37?//DES解密字節(jié)數(shù)組,返回字節(jié)數(shù)組

38?func DesDecrypt(cipherBytes, key []byte) ([]byte, error) {

39????block, err := des.NewCipher(key)

40????if err != nil {

41???????return nil, err

42????}

43????blockMode := cipher.NewCBCDecrypter(block, key)

44????originalText := make([]byte, len(cipherBytes))

45????blockMode.CryptBlocks(originalText, cipherBytes)

46????originalText = PKCS5UnPadding(originalText)

47????return originalText, nil

48?}

49?//DES加密文本,返回加密后文本

50?func DesEncryptString(originalText string, key []byte) (string, error) {

51????cipherArr, err := DesEncrypt([]byte(originalText), key)

52????if err != nil {

53???????return "", err

54????}

55????base64str := base64.StdEncoding.EncodeToString(cipherArr)

56????return base64str, nil

57?}

58?//對加密文本進行DES解密,返回解密后明文

59?func DesDecryptString(cipherText string, key []byte) (string, error) {

60????cipherArr, _ := base64.StdEncoding.DecodeString(cipherText)

61????cipherArr, err := DesDecrypt(cipherArr, key)

62????if err != nil {

63???????return "", err

64????}

65????return string(cipherArr), nil

66?}

67?func PKCS5Padding(ciphertext []byte, blockSize int) []byte {

68? padding := blockSize - len(ciphertext)%blockSize

69? padtext := bytes.Repeat([]byte{byte(padding)}, padding)

70? return append(ciphertext, padtext...)

71?}

72?func PKCS5UnPadding(origData []byte) []byte {

73? length := len(origData)

74? //?去掉最后一個字節(jié)?unpadding?次

75? unpadding := int(origData[length-1])

76? return origData[:(length - unpadding)]

77?}

運行結(jié)果如圖所示。

圖4.1?運行結(jié)果

3DES加密解密的案例,如下所示。

例1-2?3DES

1?package main

2?import (

3? "bytes"

4? "crypto/cipher"

5? "crypto/des"

6? "encoding/base64"

7? "fmt"

8?)

9?func main() {

10? key := []byte("abcdefghijklmnopqrstuvwx") //秘鑰占24個字節(jié)

11? fmt.Println("------------3DES加密解密字節(jié)數(shù)組")

12? str := "我愛Go語言"

13? result, _ := TripleDesEncrypt([]byte(str), key)

14? fmt.Printf("加密后:%x\n", result)

15? origData, _ := TripleDesDecrypt(result, key)

16? fmt.Println("解密后:", string(origData))

17? fmt.Println("------------3DES加密解密字符串")

18? cipherText, _ := TripleDesEncrypt2Str(str, key)

19? fmt.Println("加密后:", cipherText)

20? originalText, _ := TripleDesDecrypt2Str(cipherText, key)

21? fmt.Println("解密后:", originalText)

22?}

23?// 3DES加密字節(jié)數(shù)組,返回字節(jié)數(shù)組

24?func TripleDesEncrypt(originalBytes, key []byte) ([]byte, error) {

25????block, err := des.NewTripleDESCipher(key)

26????if err != nil {

27???????return nil, err

28????}

29????originalBytes = PKCS5Padding(originalBytes, block.BlockSize())

30????// originalBytes = ZeroPadding(originalBytes, block.BlockSize())

31????blockMode := cipher.NewCBCEncrypter(block, key[:8])

32????cipherArr := make([]byte, len(originalBytes))

33????blockMode.CryptBlocks(cipherArr, originalBytes)

34????return cipherArr, nil

35?}

36?// 3DES解密字節(jié)數(shù)組,返回字節(jié)數(shù)組

37?func TripleDesDecrypt(cipherBytes, key []byte) ([]byte, error) {

38????block, err := des.NewTripleDESCipher(key)

39????if err != nil {

40???????return nil, err

41????}

42????blockMode := cipher.NewCBCDecrypter(block, key[:8])

43????originalArr := make([]byte, len(cipherBytes))

44????blockMode.CryptBlocks(originalArr, cipherBytes)

45????originalArr = PKCS5UnPadding(originalArr)

46????// origData = ZeroUnPadding(origData)

47????return originalArr, nil

48?}

49?// 3DES加密字符串,返回base64處理后字符串

50?func TripleDesEncrypt2Str(originalText string, key []byte) (string, error) {

51????block, err := des.NewTripleDESCipher(key)

52????if err != nil {

53???????return "", err

54????}

55????originalData := PKCS5Padding([]byte(originalText), block.BlockSize())

56????// originalData = ZeroPadding(originalData, block.BlockSize())

57????blockMode := cipher.NewCBCEncrypter(block, key[:8])

58????cipherArr := make([]byte, len(originalData))

59????blockMode.CryptBlocks(cipherArr, originalData)

60????cipherText := base64.StdEncoding.EncodeToString(cipherArr)

61????return cipherText, nil

62?}

63?// 3DES解密base64處理后的加密字符串,返回明文字符串

64?func TripleDesDecrypt2Str(cipherText string, key []byte) (string, error) {

65????cipherArr, _ := base64.StdEncoding.DecodeString(cipherText)

66????block, err := des.NewTripleDESCipher(key)

67????if err != nil {

68???????return "", err

69????}

70????blockMode := cipher.NewCBCDecrypter(block, key[:8])

71????originalArr := make([]byte, len(cipherArr))

72????blockMode.CryptBlocks(originalArr, cipherArr)

73????originalArr = PKCS5UnPadding(originalArr)

74????// origData = ZeroUnPadding(origData)

75????return string(originalArr), nil

76?}

77?func PKCS5Padding(ciphertext []byte, blockSize int) []byte {

78? padding := blockSize - len(ciphertext)%blockSize

79? padtext := bytes.Repeat([]byte{byte(padding)}, padding)

80? return append(ciphertext, padtext...)

81?}

82?func PKCS5UnPadding(origData []byte) []byte {

83? length := len(origData)

84? //?去掉最后一個字節(jié)?unpadding?次

85? unpadding := int(origData[length-1])

86? return origData[:(length - unpadding)]

87?}

運行結(jié)果如圖所示。

圖4.2?運行結(jié)果

AES加密解密的案例,如例所示。

例1-3?AES

1?package main

2?import (

3? "bytes"

4? "crypto/aes"

5? "crypto/cipher"

6? "encoding/base64"

7? "fmt"

8?)

9?func main() {

10? // AES-128。key長度:16, 24, 32 bytes?對應?AES-128, AES-192, AES-256

11? key := []byte("1234567890abcdefghijklmnopqrstuv")

12? str := "區(qū)塊鏈很有趣"

13? fmt.Println("------------AES加密解密字節(jié)數(shù)組")

14? resultArr, _ := AesEncrypt([]byte(str), key)

15? fmt.Printf("加密后:%x\n", resultArr)

16? resultArr, _ = AesDecrypt(resultArr, key)

17? fmt.Println("解密后:", string(resultArr))

18? fmt.Println("------------AES加密解密字符串")

19? cipherText, _ := AesEncryptString(str, key)

20? fmt.Println("加密后:", cipherText)

21? originalText, _ := AesDecryptString(cipherText, key)

22? fmt.Println("解密后:", originalText)

23?}

24?//AES加密字節(jié)數(shù)組,返回字節(jié)數(shù)組

25?func AesEncrypt(originalBytes, key []byte) ([]byte, error) {

26????block, err := aes.NewCipher(key)

27????if err != nil {

28???????return nil, err

29????}

30????blockSize := block.BlockSize()

31????originalBytes = PKCS5Padding(originalBytes, blockSize)

32????blockMode := cipher.NewCBCEncrypter(block, key[:blockSize])

33????cipherBytes := make([]byte, len(originalBytes))

34????blockMode.CryptBlocks(cipherBytes, originalBytes)

35????return cipherBytes, nil

36?}

37?//AES解密字節(jié)數(shù)組,返回字節(jié)數(shù)組

38?func AesDecrypt(cipherBytes, key []byte) ([]byte, error) {

39????block, err := aes.NewCipher(key)

40????if err != nil {

41???????return nil, err

42????}

43????blockSize := block.BlockSize()

44????blockMode := cipher.NewCBCDecrypter(block, key[:blockSize])

45????originalBytes := make([]byte, len(cipherBytes))

46????blockMode.CryptBlocks(originalBytes, cipherBytes)

47????originalBytes = PKCS5UnPadding(originalBytes)

48????return originalBytes, nil

49?}

50?//AES加密文本,返回對加密后字節(jié)數(shù)組進行base64處理后字符串

51?func AesEncryptString(originalText string, key []byte) (string, error) {

52????cipherBytes, err := AesEncrypt([]byte(originalText), key)

53????if err != nil {

54???????return "", err

55????}

56????base64str := base64.StdEncoding.EncodeToString(cipherBytes)

57????return base64str, nil

58?}

59?//對Base64處理后的加密文本進行DES解密,返回解密后明文

60?func AesDecryptString(cipherText string, key []byte) (string, error) {

61????cipherBytes, _ := base64.StdEncoding.DecodeString(cipherText)

62????cipherBytes, err := AesDecrypt(cipherBytes, key)

63????if err != nil {

64???????return "", err

65????}

66????return string(cipherBytes), nil

67?}

68?func PKCS5Padding(ciphertext []byte, blockSize int) []byte {

69? padding := blockSize - len(ciphertext)%blockSize

70? padtext := bytes.Repeat([]byte{byte(padding)}, padding)

71? return append(ciphertext, padtext...)

72?}

73?func PKCS5UnPadding(origData []byte) []byte {

74? length := len(origData)

75? //?去掉最后一個字節(jié)?unpadding?次

76? unpadding := int(origData[length-1])

77? return origData[:(length - unpadding)]

78?}

運行結(jié)果如圖所示。

圖4.3?運行結(jié)果

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容