// DecryptAes128Cbc decrypts the input using AES-128 in ECB mode, with // the given key and initialization vector, assuming it was padded // according to PKCS#7. func DecryptAes128Cbc(input []byte, key []byte, iv []byte) []byte { output := make([]byte, 0, len(input)) prevBlock := iv for i := 0; i < len(input); i += aes.BlockSize { // XOR with previous block (or IV) after decrypting inputBlock := input[i : i+aes.BlockSize] decryptedBlock := set1.DecryptAes128Ecb(inputBlock, key) for i := 0; i < len(decryptedBlock); i++ { decryptedBlock[i] ^= prevBlock[i] } output = append(output, decryptedBlock...) prevBlock = inputBlock } return Pkcs7Unpad(output) }
func (s *MySuite) TestEncryptAes128Ecb(c *C) { key := []byte("YELLOW SUBMARINE") text := []byte("This is a test!!") // key length be at least 16 c.Assert(func() { EncryptAes128EcbWholeBlocks([]byte(text), key[:15]) }, PanicMatches, ".*") // input length must be whole number of blocks c.Assert(func() { EncryptAes128EcbWholeBlocks(text[:15], key) }, PanicMatches, ".*") c.Assert(func() { EncryptAes128EcbWholeBlocks(append(text, byte(1)), key) }, PanicMatches, ".*") c.Assert(EncryptAes128EcbWholeBlocks([]byte{}, key), DeepEquals, []byte{}) doubleText := append(text, text...) c.Assert(set1.DecryptAes128Ecb(EncryptAes128EcbWholeBlocks(doubleText, key), key), DeepEquals, doubleText) }