Archived Forum Post

Index of archived forum posts

Question:

Remove extra characters from the decrypted message

Dec 02 '14 at 01:55

I have the latest one month trial chilkat dll version file for encryption. I encrypted the message using AES where key length is 128 using 'EncryptStringENC' function. I was also able to decrypt the message successfully, but I got some extra unreadable characters at the end of decrypted message. I did some test cases and came to know that these extra characters are appended by the 'EncryptStringENC' function. I want to remove these extra characters. Please let me know how can I achieve this.

Dim lastkeylen As Integer
lastkeylen = 128
crypt.Charset = "utf-8"
crypt.CryptAlgorithm = "aes"
crypt.CipherMode = "cbc"
crypt.KeyLength = lastkeylen

'  Generate random bytes and return the random bytes
'  in a hex string:
crypt.EncodingMode = "base64"
Dim randomKey As String
Dim keylen As Integer
keylen = 16
randomKey = crypt.GenRandomBytesENC(keylen)

Text1.Text = Text1.Text & "AES Key: " _
     & randomKey & vbCrLf

'  Set the key.
crypt.SetEncodedKey randomKey, "base64"
txtSecretKey.Text = txtSecretKey.Text & randomKey

    crypt.EncodingMode = "base64"
    Dim encryptedText As String                                                                                                                                                                                                                         
    encryptedText = crypt.EncryptStringENC("000012$1234 5678 9012 3456")
    Text1.Text = Text1.Text & "AES Key: " _
     & randomKey & vbCrLf

    If (success <> 1) Then
        Text1.Text = Text1.Text & crypt.LastErrorText & vbCrLf
        Exit Sub
    End If

And decrypted text message is as follows: decrypted text

Note: The encryption is part is done through a Visual Basic 6.0 application where we use your chilkat libraries, but the decryption part is done through a Java application using java crypto library. My vendor will purchase your encryption dll after this implementation. Please help me ASAP.

And also let me if any chat session available to discuss this. Its very urgent.


Accepted Answer

Amit,

AES is a block encryption algorithm. All block encryption algorithms (AES, Blowfish, Twofish, DES/3DES, etc.) operate on a particular block-size that is specific to the algorithm. AES encrypts/decrypts in 16-byte blocks. This is the "block size" of the algorithm. Input can be any length, but encrypted output will always be a multiple of the block size. It is padded according to the crypt.PaddingScheme property. The default padding scheme is what you are seeing: Each byte (0x0E) indicates how many bytes of padding exists. When decrypting, an AES implementation should also unpad (after decrypting) to return the exact bytes that were encrypted. Whatever you are using to decrypt is not unpadding. You could work around the problem by changing the crypt.PaddingScheme to the value that pads with SPACE chars, and then you could trim the extra SPACE chars from the decrypted string.