Note the implementation of AES encryption and decryption for some slightly larger files. Only the core code is listed here, the rest of the code is not covered, or if you have any questions, please check out my cryptology blog

var key = []byte("1234567812345678")
var iv = []byte("1111111122222222")

/* function name: paddingLastGroup specifies the plainText data to fill in the last block of the CBC encryption algorithm: plainText, plaindata blockSize, CBC blockSize The time and creator of the filled plaintext data: 2021-06-15 Yuan_sr */
func paddingLastGroup(plainText []byte, blockSize int) []byte{
	padNum := blockSize - len(plainText) % blockSize
	char := []byte{byte(padNum)}
	newPlain := bytes.Repeat(char, padNum)
	newText := append(plainText, newPlain...)
	return newText
}

/* Function name: unpaddingLastGroup An encryption algorithm in CBC encryption mode that provides the last block of plainText data to fill in: plainText  2021-06-15 Yuan_sr */
func unpaddingLastGroup(plainText []byte) []byte{
	length := len(plainText)
	lastChar := plainText[length - 1]
	number := int(lastChar)
	return plainText[:length - number]
}

/* Function name: aesEnctrpt Encryption algorithm to implement file encryption parameter: filePathIn, file key to be encrypted, encryption key Returned value: ciphertext file name Error message Created at and by:  2021-06-15 Yuan_sr */
func aesEnctrpt(filePathIn string, key []byte) (string, error){
	inFile, err := os.Open(filePathIn)
	iferr ! =nil {
		return "", err
	}
	defer inFile.Close()

	fileInfo, err := inFile.Stat()
	iferr ! =nil {
		return "", err
	}
	blockNum := fileInfo.Size() / bufferSize
	var num int64

	outFile, err := os.Create(encryptFileName)
	iferr ! =nil {
		return "", err
	}
	defer outFile.Close()

	buf := make([]byte, bufferSize)

	// Initializes an underlying encryption algorithm
	block, err := aes.NewCipher(key)
	iferr ! =nil {
		return "", err
	}
	// Select encryption mode
	//stream := cipher.NewCTR(block, iv)
	blockMode := cipher.NewCBCEncrypter(block, iv)

	for {
		num += 1
		n, err := inFile.Read(buf)
		if err == io.EOF{
			break
		}
		iferr ! =nil&& err ! = io.EOF {return "", err
		}

		// The last segment of data is filled with data
		if num == blockNum + 1{
			groupData := paddingLastGroup(buf[:n], block.BlockSize())
			n = len(groupData)
			buf = make([]byte, n)
			buf = groupData
		}
		cipherText := make([]byte, n)
		//stream.XORKeyStream(cipherText, buf[:n])
		blockMode.CryptBlocks(cipherText, buf[:n])
		_, err = outFile.Write(cipherText)
		iferr ! =nil {
			return "", err
		}
	}
	//outFile.Write(iv)
	return encryptFileName, nil
}

/* Function name: aesDecrypt The AES algorithm decrypts files with parameters: cipherFile, ciphertext file key, and decryption key Returned value: File name Error message After decryption Time and creator: 2021-06-15 Yuan_sr */
func aesDecrypt(cipherFile string, key []byte) (string, error){
	plainFileName = "plainText.file"
	//1. Create an AES low-level password interface
	block, err := aes.NewCipher(key)
	iferr ! =nil {
		return "", err
	}
	//2. Select decryption mode
	blockMode := cipher.NewCBCDecrypter(block, iv)
	/ / 3. Decryption
	fr, err := os.Open(dvOutPath + cipherFile)
	iferr ! =nil {
		return "", err
	}
	defer fr.Close()
	fileInfo, err := fr.Stat()
	iferr ! =nil {
		return "", err
	}
	blockNum := fileInfo.Size() / bufferSize
	var num int64
	fw, err := os.Create(plainFileName)
	iferr ! =nil {
		return "", err
	}
	defer fw.Close()
	buf := make([]byte, bufferSize)
	for {
		num += 1
		n, err := fr.Read(buf)
		if err == io.EOF{
			break
		}
		iferr ! =nil&& err ! = io.EOF {return "", err
		}

		plainText := make([]byte, n)
		blockMode.CryptBlocks(plainText, buf[:n])
		// The last segment of data is filled with data
		if num == blockNum + 1{
			plainText = unpaddingLastGroup(plainText)
			n = len(plainText)
		}
		_, err = fw.Write(plainText[:n])
		iferr ! =nil {
			return "", err
		}
	}
	return plainFileName, nil
}
Copy the code