How to correctly convert a bit key to string for aes 256 encrytion?



  • I am using CryptoJS AES 256 encryption:

    CryptoJS.AES.encrypt(string, secret);
    

    The secret is generated through:

    function generateKey(str) {
      const salt = CryptoJS.lib.WordArray.random(128 / 8);
      const key512Bits = CryptoJS.PBKDF2(str, salt, {
        keySize: 512 / 32,
        iterations: 100000
      });
    
      return key512Bits.toString(<-- base64 or hex -->);
    }
    

    But it seems like the returned secret string doesn't contain any special character if hex or only '/,+' and wierd ending '==' if base64

    Is there any way to make the string much more unpredictable?

    Thanks!



  • ... the returned secret string doesn't contain any special character if hex or only '/,+' and wierd ending '==' if base64. Is there any way to make the string much more unpredictable?

    Base64 and Hex are just different kind of string representations of binary data. These representations (encoding) are used to display binary data to some human, or transport binary data through some medium which is not capable of transporting binary data as it is.

    The "predictability" in the output you describe is a property of the encoding, not a property of the encoded data, i.e. it happens on fully random input data too. Hex (hexadecimal) encoding just means to encode 4 bits as one character between from 0..9a..f and Base64 just means to encode 6 bits in a similar way. And the == at the end of Base64 just comes from the length of the input not being a multiple of 6 bits, i.e. it is padding.

    For more see Hexadecimal and Base64 on Wikipedia.



Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2