2
func AESEncryptWithNopadding(origData []byte,key []byte,iv []byte) (string, error) {
    block, err := aes.NewCipher(key)
    if err != nil {
        return "", err
    }
    blockMode := cipher.NewCBCEncrypter(block, iv)
    crypted := make([]byte, len(origData))

    blockMode.CryptBlocks(crypted, origData)
    return base64.StdEncoding.EncodeToString(crypted), nil
}

I try to Aes encript with nopadding but

OUTPUT: panic: crypto/cipher: input not full blocks

Jonathan Hall
  • 75,165
  • 16
  • 143
  • 189
ChengXiao
  • 21
  • 1
  • 3
    The length of the plaintext for AES in [CBC](https://en.wikipedia.org/wiki/Block_cipher_mode_of_operation#Cipher_block_chaining_(CBC)) mode must be an integer multiple of the block size (16 bytes for AES). Otherwise, padding is necessary, which ensures that this condition is met. Is the condition for your plaintext without padding fulfilled? There are also stream cipher modes that do not require padding, e.g. [GCM](https://en.wikipedia.org/wiki/Galois/Counter_Mode), which provides authentication in addition to confidentiality. – Topaco Sep 14 '20 at 06:35
  • But in Java there seems have a function call **AES/CBC/NoPadding**How did they do it? – ChengXiao Sep 15 '20 at 07:24
  • For encryption a plaintext is expected which fulfills the above condition (otherwise a corresponding exception is thrown: _IllegalBlockSizeException: Input length not multiple of 16 bytes_). For decryption a possible padding from encryption is not removed. – Topaco Sep 15 '20 at 07:58

0 Answers0