Issue in generating session key

1.1k Views Asked by At

I amusing following code to generate sessionkeu

  public byte[] generateSessionKey() throws NoSuchAlgorithmException, NoSuchProviderException 
          {
    KeyGenerator kgen = KeyGenerator.getInstance("AES","BC");
    kgen.init(256);
    SecretKey key = kgen.generateKey();
    byte[] symmKey = key.getEncoded();
    return symmKey;
        }

and printing it here

  byte[] aa=encryptor.generateSessionKey();
  String s1=new String(aa);
  System.out.println(s1);

and output is

 Çɤđy3F:¦ïó‹AOÜYu•{Öç„>r?Ô–

am I doing it right ?

1

There are 1 best solutions below

0
On BEST ANSWER

This new String(aa) is highly likely to be incorrect. It's directly converting the byte[] into a string using the platform's default character set. This means it's potentially different for each platform this runs on.

Common ways of displaying, transmitting or storing byte[] as strings include base16 (hexadecimal) representation, and base64.