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 ?
This
new String(aa)
is highly likely to be incorrect. It's directly converting thebyte[]
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.