I made a class which has a method to encrypt data using SHA1PRNG and AES algorithm.
public String encrypt(String str, String pw) throws Exception{
byte[] bytes = pw.getBytes();
SecureRandom sr = SecureRandom.getInstance("SHA1PRNG");
sr.setSeed(bytes);
KeyGenerator kgen = KeyGenerator.getInstance("AES");
kgen.init(128,sr);
SecretKey skey = kgen.generateKey();
SecretKeySpec skeySpec = new SecretKeySpec(skey.getEncoded(),"AES");
Cipher c = Cipher.getInstance("AES");
c.init(Cipher.ENCRYPT_MODE, skeySpec);
byte[] encrypted = c.doFinal(str.getBytes());
return Hex.encodeHexString(encrypted);
}
I used this method in my main.
public static void main(String[] args) throws Exception{
Encrytion enc = new Encrytion(); //my class name has a typo :(
enc.encrypt("abcde", "abcdfg");
System.out.println(enc);
}
My result is
com.dsmentoring.kmi.Encrytion@34340fab
just my packagename + class name + and some number ( I'm guessing this is reference address to the actual data?)
I want to see the result of my encryption like 'a13efx34123fdv....... ' like this. What do I need to add in my main method? Any advice?
You're printing the Encryption
object instead of the result of the function call.
You can do this instead:
public static void main(String[] args) throws Exception{
Encrytion enc = new Encrytion(); //my class name has a typo :(
String result = enc.encrypt("abcde", "abcdfg");
System.out.println(result);
}