在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
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个字节 11 arr := "千锋教育" 12 fmt.Println("------------DES加密解密字节数组") 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加密字节数组,返回字节数组 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解密字节数组,返回字节数组 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 // 去掉最后一个字节 unpadding 次 75 unpadding := int(origData[length-1]) 76 return origData[:(length - unpadding)] 77 } 运行结果如图所示。
图4.1 运行结果 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个字节 11 fmt.Println("------------3DES加密解密字节数组") 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加密字节数组,返回字节数组 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解密字节数组,返回字节数组 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 // 去掉最后一个字节 unpadding 次 85 unpadding := int(origData[length-1]) 86 return origData[:(length - unpadding)] 87 } 运行结果如图所示。 图4.2 运行结果 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 := "区块链很有趣" 13 fmt.Println("------------AES加密解密字节数组") 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加密字节数组,返回字节数组 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解密字节数组,返回字节数组 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加密文本,返回对加密后字节数组进行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 // 去掉最后一个字节 unpadding 次 76 unpadding := int(origData[length-1]) 77 return origData[:(length - unpadding)] 78 } 运行结果如图所示。
图4.3 运行结果 |
请发表评论