• GoLang使用AES CBC加密解密


    AES CBC实现:

    package ciphers
    
    import (
    	"bytes"
    	"crypto/aes"
    	"crypto/cipher"
    )
    
    type AesCrypt struct {
    	Key []byte
    	Iv  []byte
    }
    
    func (a *AesCrypt) Encrypt(data []byte) ([]byte, error) {
    	aesBlockEncrypt, err := aes.NewCipher(a.Key)
    	if err != nil {
    		println(err.Error())
    		return nil, err
    	}
    
    	content := pKCS5Padding(data, aesBlockEncrypt.BlockSize())
    	cipherBytes := make([]byte, len(content))
    	aesEncrypt := cipher.NewCBCEncrypter(aesBlockEncrypt, a.Iv)
    	aesEncrypt.CryptBlocks(cipherBytes, content)
    	return cipherBytes, nil
    }
    
    func (a *AesCrypt) Decrypt(src []byte) (data []byte, err error) {
    	decrypted := make([]byte, len(src))
    	var aesBlockDecrypt cipher.Block
    	aesBlockDecrypt, err = aes.NewCipher(a.Key)
    	if err != nil {
    		println(err.Error())
    		return nil, err
    	}
    	aesDecrypt := cipher.NewCBCDecrypter(aesBlockDecrypt, a.Iv)
    	aesDecrypt.CryptBlocks(decrypted, src)
    	return pKCS5Trimming(decrypted), 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 pKCS5Trimming(encrypt []byte) []byte {
    	padding := encrypt[len(encrypt)-1]
    	return encrypt[:len(encrypt)-int(padding)]
    }
    View Code

    测试代码:

    package ciphers
    
    import (
    	"encoding/base64"
    	"fmt"
    	"testing"
    )
    
    func TestAesCrypt_Encrypt(t *testing.T) {
    	var aesCrypt = AesCrypt{
    		Key: []byte("ABCDEFGHIJKLMNOP"),
    		Iv:  []byte{0x12, 0x34, 0x56, 0x78, 0x9A, 0xBC, 0xDE, 0xF0, 0x12, 0x34, 0x56, 0x78, 0x9A, 0xBC, 0xDE, 0xF0},
    	}
    
    	var text = "areful1997"
    	result, err := aesCrypt.Encrypt([]byte(text))
    	if err != nil {
    		fmt.Println(err)
    		return
    	}
    
    	pass64 := base64.StdEncoding.EncodeToString(result)
    	fmt.Println(pass64)
    }
    
    func TestAesCrypt_Decrypt(t *testing.T) {
    	var aesCrypt = AesCrypt{
    		Key: []byte("ABCDEFGHIJKLMNOP"),
    		Iv:  []byte{0x12, 0x34, 0x56, 0x78, 0x9A, 0xBC, 0xDE, 0xF0, 0x12, 0x34, 0x56, 0x78, 0x9A, 0xBC, 0xDE, 0xF0},
    	}
    
    	pass64 := "8oAbgUpjro+xwDuxiGDFTQ=="
    	bytesPass, err := base64.StdEncoding.DecodeString(pass64)
    	if err != nil {
    		fmt.Println(err)
    		return
    	}
    
    	plainText, err := aesCrypt.Decrypt(bytesPass)
    	if err != nil {
    		fmt.Println(err)
    		return
    	}
    
    	fmt.Println(string(plainText))
    }
    View Code
  • 相关阅读:
    查看进程,按内存从大到小 ,查看进程,按CPU利用率从大到小排序
    haproxy 安装 各个参数的测试
    ps -C nginx --no-header |wc -l
    lsof -ntP -i:端口取出 动行程序的PID 然后xargs kill -9 这个进程
    linux 普通用户切换成root免密码
    inter x86 emulator accelerator(HAXM installer) not compatible with windows
    react-native 相关问题
    SQLCE数据工具(Flyhoward Ltd SDF Viewer)
    Android APK反编译就这么简单 详解(附图)
    react.js 教程之 Installation 安装
  • 原文地址:https://www.cnblogs.com/areful/p/11107119.html
Copyright © 2020-2023  润新知