好的,我让它工作了,但它不会用随机密钥初始化我的keyValue。
这是我的 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;
}
}
这是 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);
}
}
如果我把
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);
在我的主要课程中,代码有效,但我希望它在另一个类中。
答案 0 :(得分:1)
在AESencrp
中使用构造函数:
KeyGenerator gen;
public AESencrp() throws NoSuchAlgorithmException {
this.gen = KeyGenerator.getInstance("AES");
gen.init(128); /* 128-bit AES */
}
这些进口
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;
是个坏主意。 sun.
包不适合直接使用。我推荐Commons Codec的课程。见https://stackoverflow.com/a/469715/1907906
答案 1 :(得分:1)
将代码放在另一个类的构造函数中。
您可以在方法定义之外的类中编写的唯一代码是
static {
System.out.println("in static block.");
}
但是从静态代码中,您无法访问实例属性。因此,构造函数是进行这些初始化的自然场所。
如果,OTOH,你真的只想在AESencrp中使用静态文件,你需要将它声明为静态,并且初始化代码也必须在静态块中。