Reputation:
Ok, I made it work, but it won't initialise my keyValue with a random key.
This is my AESencrp.java
package encript;
import java.math.BigInteger;
import java.security.Key;
import java.security.NoSuchAlgorithmException;
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;
public class AESencrp {
KeyGenerator gen;
private static final byte[] keyValue = new byte[16] ;
public AESencrp() throws NoSuchAlgorithmException {
this.gen = KeyGenerator.getInstance("AES");
gen.init(128); /* 128-bit AES */
SecretKey secret = gen.generateKey();
byte[] keyValue = secret.getEncoded();
String text = String.format("%032X", new BigInteger(+1, keyValue));
System.out.println(text);
}
private static final String ALGO = "AES";
/*private static final byte[] keyValue = new byte[] { 'T', 'h', 'e', 'B',
'e', 's', 't', 'S', 'e', 'c', 'r', 'e', 't', 'K', 'e', 'y' };*/
private static String toHex(final byte[] data) {
final StringBuilder sb = new StringBuilder(data.length * 2);
for (final byte b : data) {
sb.append(String.format("%02X", b));
}
return sb.toString();
}
public static String encrypt(String Data) throws Exception {
Key key = generateKey();
Cipher c = Cipher.getInstance(ALGO);
c.init(Cipher.ENCRYPT_MODE, key);
byte[] encVal = c.doFinal(Data.getBytes());
String encryptedValue = new BASE64Encoder().encode(encVal);
return encryptedValue;
}
public static String decrypt(String encryptedData) throws Exception {
Key key = generateKey();
Cipher c = Cipher.getInstance(ALGO);
c.init(Cipher.DECRYPT_MODE, key);
byte[] decordedValue = new BASE64Decoder().decodeBuffer(encryptedData);
byte[] decValue = c.doFinal(decordedValue);
String decryptedValue = new String(decValue);
return decryptedValue;
}
private static Key generateKey() throws Exception {
Key key = new SecretKeySpec(keyValue, ALGO);
return key;
}
}
And this is Checker.java
package encript;
public class Checker {
public static void main(String[] args) throws Exception {
String password = "mypassword";
String passwordEnc = AESencrp.encrypt(password);
String passwordDec = AESencrp.decrypt(passwordEnc);
System.out.println("Plain Text : " + password);
System.out.println("Encrypted Text : " + passwordEnc);
System.out.println("Decrypted Text : " + passwordDec);
}
}
If I put
KeyGenerator gen = KeyGenerator.getInstance("AES");
gen.init(128); /* 128-bit AES */
SecretKey secret = gen.generateKey();
byte[] binary = secret.getEncoded();
String text = String.format("%032X", new BigInteger(+1, binary));
System.out.println(text);
In my main class, the code works, but I want it to be in the other class.
Upvotes: 0
Views: 381
Reputation: 36339
Put the code in the constructor of the other class.
The only code you can write in a class outside of a method definition is
static {
System.out.println("in static block.");
}
But from static code you can't access instance attributes. Hence, the constructor is the natural place to do those initializations.
If, OTOH, you really want only static fileds in AESencrp you need to declare it static, and also the initialization code must be in a static block.
Upvotes: 1
Reputation:
Use a constructor in AESencrp
:
KeyGenerator gen;
public AESencrp() throws NoSuchAlgorithmException {
this.gen = KeyGenerator.getInstance("AES");
gen.init(128); /* 128-bit AES */
}
These imports
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;
are a bad idea. The sun.
packages are not meant for direct usage. I recommend the classes from Commons Codec. See https://stackoverflow.com/a/469715/1907906
Upvotes: 1