gpt4 book ai didi

java - token 语法错误

转载 作者:行者123 更新时间:2023-12-02 06:29:25 25 4
gpt4 key购买 nike

好吧,我成功了,但它不会用随 secret 钥初始化我的 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);

在我的主类中,代码可以工作,但我希望它在另一个类中。

最佳答案

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 中的类(class)。请参阅https://stackoverflow.com/a/469715/1907906

关于java - token 语法错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20214188/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com