【问题标题】:Java : My system for decrypt text doesn't workJava:我的文本解密系统不起作用
【发布时间】:2019-05-29 13:22:39
【问题描述】:

我有一个用于加密文本的系统,但我试图创建一个用于解密文本的系统,但它不起作用。系统是:

  • 将加密文本初始化为字节[]

  • 用加密文本初始化解密文本

他只返回加密的文本,而不是解密的文本。你有调试这个的想法吗?

提前致谢。

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;
}

String getDecrypt(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());

    cipher.init(Cipher.DECRYPT_MODE, aesKey);
    String decrypted = new String(cipher.doFinal(encrypted));

    return decrypted;
}

【问题讨论】:

  • 也分享你使用这些功能的代码。
  • 有一点是错误的:您的getEncrypt 方法返回一个字符串,但加密的文本由不能存储在字符串中的字节组成。不要这样做new String(encrypted)。使方法返回byte[] 而不是String。字符串不是任意字节的合适容器。
  • 可能是 byte[] 到 Base64 字符串。如果我错了,我认为这是正确的做法。

标签: java security encryption


【解决方案1】:

您在 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

测试

【讨论】:

  • 非常感谢您的帮助!
【解决方案2】:

我使用jasypt 来加密密码。方法很简单。

BasicTextEncryptor textEncryptor = new BasicTextEncryptor();
textEncryptor.setPassword("some_Password_Maybe_generated_At_random");
...
String myEncryptedText = textEncryptor.encrypt(myText);
...
String plainText = textEncryptor.decrypt(myEncryptedText);

通常我不会将密码放在中,而是将其保存在用户主目录中的文件中。

加密的强度与用户主目录的安全性和私密性一样。

这个方法很好,因为它返回一个可以放在配置文件中的字符串。

正如我之前所说,它不是非常安全的,只要加密密码是安全的,它就会保护加密文本。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-11-18
    • 2012-09-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-06-04
    • 1970-01-01
    相关资源
    最近更新 更多