Java:我的文本解密系统没有';行不通
我有一个加密文本的系统,但我试图创建一个解密文本的系统,但它不起作用。该系统是:Java:我的文本解密系统没有';行不通,java,security,encryption,Java,Security,Encryption,我有一个加密文本的系统,但我试图创建一个解密文本的系统,但它不起作用。该系统是: 将加密文本初始化为字节[] 用加密文本初始化解密文本 他只返回加密文本,但不返回解密文本。你有办法调试这个吗 提前谢谢 byte[]getEncrypt(字符串文本)抛出NoSuchPaddingException、NoSuchAlgorithmException、InvalidKeyException、BadPaddingException、IllegalBlockSizeException { String
- 将加密文本初始化为字节[]
- 用加密文本初始化解密文本
byte[]getEncrypt(字符串文本)抛出NoSuchPaddingException、NoSuchAlgorithmException、InvalidKeyException、BadPaddingException、IllegalBlockSizeException
{
String key=“Bép12345Taruy'(”;
Key aesKey=新的SecretKeySpec(Key.getBytes(),“AES”);
Cipher Cipher=Cipher.getInstance(“AES”);
cipher.init(cipher.ENCRYPT_模式,aesKey);
byte[]encrypted=cipher.doFinal(text.getBytes());
返回加密;
}
String getDecrypt(字符串文本)抛出NoSuchPaddingException、NoSuchAlgorithmException、InvalidKeyException、BadPaddingException、IllegalBlockSizeException
{
String key=“Bép12345Taruy'(”;
Key aesKey=新的SecretKeySpec(Key.getBytes(),“AES”);
Cipher Cipher=Cipher.getInstance(“AES”);
cipher.init(cipher.ENCRYPT_模式,aesKey);
byte[]encrypted=cipher.doFinal(text.getBytes());
cipher.init(cipher.DECRYPT_模式,aesKey);
字符串解密=新字符串(cipher.doFinal(加密));
返回解密;
}
您在getDecrypt(…)
方法中加密加密文本。还是要用一种方法再次加密和解密
一种解决方案是以下代码:
package test;
import java.security.InvalidKeyException;
import java.security.Key;
import java.security.NoSuchAlgorithmException;
import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.spec.SecretKeySpec;
public class Test2{
public static void main(String[] args) throws InvalidKeyException, NoSuchPaddingException, NoSuchAlgorithmException, BadPaddingException, IllegalBlockSizeException {
System.out.println(new String(getEncrypt("test")));
System.out.println(new String(getDecrypt(getEncrypt("test"))));
}
public static byte[] getEncrypt(String text) throws NoSuchPaddingException, NoSuchAlgorithmException, InvalidKeyException, BadPaddingException, IllegalBlockSizeException {
String key = "Bép12345Taruy'(";
Key aesKey = new SecretKeySpec(key.getBytes(), "AES");
Cipher cipher = Cipher.getInstance("AES");
cipher.init(Cipher.ENCRYPT_MODE, aesKey);
byte[] encrypted = cipher.doFinal(text.getBytes());
return encrypted;
}
public static byte [] getDecrypt(byte[] encrypted) throws NoSuchPaddingException, NoSuchAlgorithmException, InvalidKeyException, BadPaddingException, IllegalBlockSizeException {
String key = "Bép12345Taruy'(";
Key aesKey = new SecretKeySpec(key.getBytes(), "AES");
Cipher cipher = Cipher.getInstance("AES");
cipher.init(Cipher.DECRYPT_MODE, aesKey);
return cipher.doFinal(encrypted);
}
}
输出:
�'���+~�@��@w
试验
我用密码加密,方法很简单
BasicTextEncryptor textEncryptor = new BasicTextEncryptor();
textEncryptor.setPassword("some_Password_Maybe_generated_At_random");
...
String myEncryptedText = textEncryptor.encrypt(myText);
...
String plainText = textEncryptor.decrypt(myEncryptedText);
通常我不会将密码放入源代码中,但会将其保存在用户home dir的文件中
加密与用户home dir的安全性和私密性一样强大
这个方法很好,因为它返回一个可以放入配置文件中的字符串
正如我之前所说,它不是很安全,只要加密密码是安全的,它就可以保护加密文本。还可以在使用这些函数的地方共享代码。错误的一点是:您的
getEncrypt
方法返回一个字符串,但加密文本由无法存储在字符串中的字节组成。不要这样做不要使用新字符串(加密)
。使方法返回字节[]
而不是字符串
。字符串不是任意字节的合适容器。可能是字节[]到Base64字符串。如果我错了,我想这是正确的做法。非常感谢您的帮助!