0

I am trying to generate a AES 128 Key. If i debug the size of the Key it shows as 16 bytes, however, if i debug the key it shows 24 bytes. I am Using below Code to generate key.

    Blob key = Crypto.generateAesKey(128);
    System.debug('Key-->'+Encodingutil.base64Encode(key)); //rFc3IlMSCgEIZ4k8VepCKQ==
    System.debug('key.Size-->'+key.size()); //16

I need 16 bytes of key. Can anybody suggest.

2

You are debugging Base64, which encodes binary data into printable characters. Base64 digits do not map 1:1 to bytes. From Wikipedia,

Three 8-bit bytes (i.e., a total of 24 bits) can [...] be represented by four 6-bit Base64 digits.

The size of your blob is the size of the key. You should not expect raw encryption keys to be printable.

| improve this answer | |
  • Hi David, Thanks, but i need to store the key in metadata so i need the actual key. How can i get that value? – Umesh Rathi Apr 28 at 16:19
  • You can store a Base64 value and decode it to produce binary data. – David Reed Apr 28 at 16:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.