gpt4 book ai didi

java - 无法成功将 SecretKey 保存为字符串

转载 作者:行者123 更新时间:2023-12-01 12:33:52 24 4
gpt4 key购买 nike

我正在尝试将 key 保存在 XML 文件中,以便稍后解密加密字符串,该软件可以在“Encrypt()”方法中加密并返回我,但在尝试使用“Decrypt( )”方法

代码

 private static Cipher ecipher;
private static Cipher dcipher;

public static String[] encrypt(String str) throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, UnsupportedEncodingException, IllegalBlockSizeException, BadPaddingException {
String Key, res;
SecretKey key;
String[] Return = new String[2];

key = KeyGenerator.getInstance("DES").generateKey();
ecipher = Cipher.getInstance("DES");
ecipher.init(Cipher.ENCRYPT_MODE, key);
byte[] utf8 = str.getBytes("UTF8");
byte[] enc = ecipher.doFinal(utf8);

enc = BASE64EncoderStream.encode(enc);
res = new String(enc);

//Returning values 0 = Encrypted String 1 = Key For Storage in XML
Return[0] = res;
byte[] keyBytes = key.getEncoded();
Key = new String(keyBytes,"UTF8");
Return[1] = Key;

return Return;
}

public static String decrypt(String str, String Key) throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, IllegalBlockSizeException, UnsupportedEncodingException, BadPaddingException {
SecretKey key = new SecretKeySpec(Key.getBytes("UTF8"), "DES");
dcipher = Cipher.getInstance("DES");
dcipher.init(Cipher.DECRYPT_MODE, key);
byte[] dec = BASE64DecoderStream.decode(str.getBytes());
byte[] utf8 = dcipher.doFinal(dec);
return new String(utf8, "UTF8");
}

控制台返回:

run:
Encryped String : EB6uhzsBl08=
Key : �g�uX8p
Sep 07, 2014 6:35:17 PM Software.Software main
SEVERE: null
java.security.InvalidKeyException: Invalid key length: 12 bytes
at com.sun.crypto.provider.DESCipher.engineGetKeySize(DESCipher.java:373)
at javax.crypto.Cipher.passCryptoPermCheck(Cipher.java:1062)
at javax.crypto.Cipher.checkCryptoPerm(Cipher.java:1020)
at javax.crypto.Cipher.implInit(Cipher.java:796)
at javax.crypto.Cipher.chooseProvider(Cipher.java:859)
at javax.crypto.Cipher.init(Cipher.java:1229)
at javax.crypto.Cipher.init(Cipher.java:1166)
at Software.Encryption.decrypt(Encryption.java:51)
at Software.Software.main(main.java:30)

BUILD SUCCESSFUL (total time: 1 second)

最佳答案

您必须以 Base64 形式对 key 进行编码

public static String[] encrypt(String str) throws NoSuchAlgorithmException,
NoSuchPaddingException, InvalidKeyException,
UnsupportedEncodingException, IllegalBlockSizeException,
BadPaddingException {
String Key, res;
SecretKey key;
String[] Return = new String[2];

key = KeyGenerator.getInstance("DES").generateKey();
ecipher = Cipher.getInstance("DES");
ecipher.init(Cipher.ENCRYPT_MODE, key);
byte[] utf8 = str.getBytes("UTF8");
byte[] enc = ecipher.doFinal(utf8);

enc = BASE64EncoderStream.encode(enc);
res = new String(enc);

// Returning values 0 = Encrypted String 1 = Key For Storage in XML
Return[0] = res;
byte[] keyBytes = key.getEncoded();
Key = new String(BASE64EncoderStream.encode(keyBytes), "UTF8");
Return[1] = Key;

return Return;
}

public static String decrypt(String str, String Key)
throws NoSuchAlgorithmException, NoSuchPaddingException,
InvalidKeyException, IllegalBlockSizeException,
UnsupportedEncodingException, BadPaddingException {
SecretKey key = new SecretKeySpec(BASE64DecoderStream.decode(Key.getBytes("UTF8")), "DES");
dcipher = Cipher.getInstance("DES");
dcipher.init(Cipher.DECRYPT_MODE, key);
byte[] dec = BASE64DecoderStream.decode(str.getBytes());
byte[] utf8 = dcipher.doFinal(dec);
return new String(utf8, "UTF8");
}

关于java - 无法成功将 SecretKey 保存为字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25715001/

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