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.

1 Answer 1

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.

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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .