go language symmetric encryption and decryption algorithms

main Package 

Import ( 
	"bytes" 
	"Crypto / AES" 
	"Crypto / cipher" 
	"Crypto / des" 
	"encoding / Base64" 
	"FMT" 
) 

// main entry function 
FUNC main () { 
	// the DES key 
	key: = " 12345678 "8 bytes // 
	// 3DES key 
	// key =" abcdefgh0123456712345678 "// occupy 24 bytes 
	// key length of the AES key 162 432 128 corresponding to the AES-192-the AES the AES-256 
	// key = "abcdefgh0123456712345678" // accounted for 24 byte 

	data: = "learning technology, change the world" 
	chipherArr, ERR: = SCEncryptString (the Data, Key, "des") 
	IF ERR = nil {! 
		panic (ERR) 
	} 
	fmt.Printf ( " encrypted byte array:% v \ n ",chipherArr) 
	fmt.Printf ( "encrypted hex:% x \ n", chipherArr )

	originalBytes, err := SCDecryptString(chipherArr, key, "des")
	if err != nil {
		panic(err)
	}
	fmt.Println("解密后:", string(originalBytes))
}

// SCEncrypt DES加密
func SCEncrypt(originalBytes, key []byte, scType string) ([]byte, error) {
	// 1、实例化密码器block(参数为密钥)
	var err error
	var block cipher.Block
	switch scType {
	case "des":
		block, err = des.NewCipher(key)
	case "3des":
		block, err = des.NewTripleDESCipher(key)
	case "aes":
		block, err = aes.NewCipher(key)
	}
	if err != nil {
		return nil, err
	}
	blockSize := block.BlockSize()
	fmt.Println ( "blockSize --- ---", blockSize) 
	// 2, plaintext stuffing bytes (byte parameter is the number of slices and the original block code objects) 
	paddingBytes: = PKCSSPadding (originalBytes, blockSize) 
	fmt.Println ( "filled byte slices:", paddingBytes) 
	//. 3, examples of encryption mode (parameters and key cryptographic object) 
	BlockMode: = cipher.NewCBCEncrypter (Block, key [: blockSize]) 
	FMT. println ( "encryption mode:", BlockMode) 
	//. 4, the padding bytes of the plaintext is encrypted (encryption parameter byte stuff byte slices and slice) 
	cipherBytes: = the make ([] byte, len (paddingBytes)) 
	blockMode.CryptBlocks (cipherBytes, paddingBytes) 
	return cipherBytes, nil 
} 

// SCDecrypt bytes decrypted slices, slice bytes returned 
FUNC SCDecrypt (cipherBytes, Key [] byte, scType String) ([] byte, error) { 
	//. 1, examples of Block Encryptor (key parameter) 
	var ERR error 
	var Block cipher.Block
	{scType Switch 
	Case "des": 
		Block, ERR = des.NewCipher (Key) 
	Case "3DES": 
	originalBytes: = PKCSSUnPadding (paddingBytes)
		Block, ERR = des.NewTripleDESCipher (Key) 
	Case "AES": 
		Block, ERR = aes.NewCipher (Key) 
	} 
	! {IF ERR = nil 
		return nil, ERR 
	} 
	blockSize: block.BlockSize = () 
	// 2, Example decryption mode (parameters and key cryptographic object) 
	BlockMode: = cipher.NewCBCDecrypter (Block, key [: blockSize]) 
	// fmt.Println ( "decryption mode:", BlockMode) 
	//. 3, to decrypt the ciphertext (encryption parameter byte stuff byte slices and slice) 
	paddingBytes: = the make ([] byte, len (cipherBytes)) 
	blockMode.CryptBlocks (paddingBytes, cipherBytes) 
	//. 4, the removal of stuffing bytes (slices filling parameters) 
// SCEncryptString SCEncryptString 
FUNC SCEncryptString (originalText, Key, scType String) (String, error) {
	return original bytes, nil 
} 

	chipherArr, err: = SCEncrypt (byte [] (original text), byte [] (key), scType) 
	if err = nil {! 
		panic (err) 
	} 
	base64str: = base64.StdEncoding.EncodeToString (chipherArr) 
	return base64str, nil 
} 

// SCDecryptString SCDecryptString 
func SCDecryptString (chipherText, key, scType string) (string, error) { 
	chipherArr, _: = base64.StdEncoding.DecodeString (chipherText) 
	chipherArr, err: = SCDecrypt (chipherArr, byte [] (key), scType) 
	if err! = nil { 
		panic (err) 
	} 
	return string (chipherArr), nil 
} 

// PKCSSPadding填充字节的函数
func PKCSSPadding (data [] byte, block size int) byte [] {
	padding: = blockSize - len (Data)% blockSize 
	Result: = Data [:( len (Data) - int (unpadding))]
	fmt.Println("要填充的字节:", padding)
	// initialize the padding element is a slice 
	slice1: = [] {byte byte (padding)} 
	slice2: = bytes.Repeat (slice1, padding) 
	return the append (Data, slice2 ...) 
} 

// padding bytes ZeroPadding function 
FUNC ZeroPadding (Data [] byte, blockSize int) [] {byte 
	padding: = blockSize - len (Data)% blockSize 
	fmt.Println ( "bytes to be filled:", padding) 
	// initialize the padding element is a slice 
	slice1: = [] {0} byte 
	slice2: = bytes.Repeat (slice1, padding) 
	return the append (Data, slice2 ...) 
} 

// PKCSSUnPadding stuffing byte removal function 
func PKCSSUnPadding (data [] byte) [ ] {byte 
	unpadding: = Data [len (Data) -1] 
	return Result 
} 

// ZeroUnPadding stuffing byte removal function
func ZeroUnPadding(data []byte) []byte {
	return bytes.TrimRightFunc(data, func(r rune) bool {
		return r == 0
	})
}

  

Guess you like

Origin www.cnblogs.com/Mishell/p/12237479.html