0

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 ?

Mudit
  • 199
  • 2
  • 21

1 Answers1

1

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.

Community
  • 1
  • 1
weston
  • 54,145
  • 21
  • 145
  • 203