This is the seventh day of my participation in the August More text Challenge. For details, see: August More Text Challenge

This paper will explain the PREVIOUS AES algorithm based on Golang language. Due to the low security of ECB working mode compared with other working modes (not recommended), this paper will not show it. If you really need to use it in work, please implement it by yourself.

Due to space constraints, all of the demo code will show only the key steps, if you want to see the full process, please go to github.com/aurthurxlc/…

CBC working mode

In this working mode, the last block of plaintext data needs to be aligned, so the corresponding padding method should be used. This paper only demonstrates PKCS5Padding and PKCS7Padding modes.

In this working mode, you need to prepare the following parameters: key, iv, and paddingMode.

The key steps of encryption are as follows:

func (a *CryptoCBC) EncryptWithIV(plainText []byte, iv []byte) []byte {
	switch a.paddingMode {
	case Pkcs5Padding:
		plainText = __pkcs5Padding(plainText)
	case Pkcs7Padding:
		plainText = __pkcs7Padding(plainText, a.block.BlockSize())
	}

	cipherText := make([]byte.len(plainText))
	crypto := cipher.NewCBCEncrypter(a.block, iv)
	crypto.CryptBlocks(cipherText, plainText)

	return cipherText
}
Copy the code

The key steps of decryption are as follows (don’t forget to remove the populated data after decryption) :

func (a *CryptoCBC) DecryptWithIV(cipherText []byte, iv []byte) []byte {
	plainText := make([]byte.len(cipherText))
	crypto := cipher.NewCBCDecrypter(a.block, iv)
	crypto.CryptBlocks(plainText, cipherText)
	plainText = __pkcsUnPadding(plainText)

	return plainText
}
Copy the code

CFB working mode

In this mode, you need to prepare the following parameters: key and iv.

The key steps of encryption/decryption are as follows:

func (a *CryptoCFB) EncryptWithIV(plainText []byte, iv []byte) []byte {
	cipherText := make([]byte.len(plainText))
	crypto := cipher.NewCFBEncrypter(a.block, iv)
	crypto.XORKeyStream(cipherText, plainText)
	return cipherText
}

func (a *CryptoCFB) DecryptWithIV(cipherText []byte, iv []byte) []byte {
	plainText := make([]byte.len(cipherText))
	crypto := cipher.NewCFBDecrypter(a.block, iv)
	crypto.XORKeyStream(plainText, cipherText)

	return plainText
}
Copy the code

OFB working mode

In this mode, you need to prepare the following parameters: key and iv.

The key steps of encryption/decryption are as follows:

func (a *CryptoOFB) EncryptWithIV(plainText []byte, iv []byte) []byte {
	cipherText := make([]byte.len(plainText))
	crypto := cipher.NewOFB(a.block, iv)
	crypto.XORKeyStream(cipherText, plainText)
	return cipherText
}

func (a *CryptoOFB) DecryptWithIV(cipherText []byte, iv []byte) []byte {
	plainText := make([]byte.len(cipherText))
	crypto := cipher.NewOFB(a.block, iv)
	crypto.XORKeyStream(plainText, cipherText)
	return plainText
}
Copy the code

CTR working mode

In this mode, you need to prepare the following parameters: key and iv.

The key steps of encryption/decryption are as follows:

func (a *CryptoCTR) EncryptWithIV(plainText []byte, iv []byte) []byte {
	cipherText := make([]byte.len(plainText))
	crypto := cipher.NewCTR(a.block, iv)
	crypto.XORKeyStream(cipherText, plainText)
	return cipherText
}

func (a *CryptoCTR) DecryptWithIV(cipherText []byte, iv []byte) []byte {
	plainText := make([]byte.len(cipherText))
	crypto := cipher.NewCTR(a.block, iv)
	crypto.XORKeyStream(plainText, cipherText)

	return plainText
}
Copy the code

GCM working mode

In this mode, you need to prepare the following parameters: key and iv.

The key steps of encryption/decryption are as follows:

func (a *CryptoGCM) EncryptWithIV(plainText []byte, iv []byte) []byte {
	crypto, err := cipher.NewGCMWithNonceSize(a.block, len(iv))
	iferr ! =nil {
		panic(err.Error())
	}

	cipherText := crypto.Seal(nil, iv, plainText, nil)
	return cipherText
}

func (a *CryptoGCM) DecryptWithIV(cipherText []byte, iv []byte) []byte {
	crypto, err := cipher.NewGCMWithNonceSize(a.block, len(iv))
	iferr ! =nil {
		panic(err.Error())
	}
	plainText, err := crypto.Open(nil, iv, cipherText, nil)
	iferr ! =nil {
		panic(err.Error())
	}
	return plainText
}
Copy the code