【问题标题】:Java : can't decrypt what I encryptJava:无法解密我加密的内容
【发布时间】:2018-01-10 13:56:58
【问题描述】:

对不起,如果我的问题很愚蠢,但我正在学习 java,我正在做一个需要加密和解密的项目,我已经完成了代码,加密按钮功能齐全但是当我尝试解密时(不同的按钮)一直有错误。

加密代码

 path = chooser.getSelectedFile();
             String x = path.toString();

             try {

                Scanner scanner = new Scanner(new File(x));
                content = scanner.useDelimiter("\\Z").next();

            } catch (FileNotFoundException e2) {
                e2.printStackTrace();
            }

                try{

                    KeyGenerator keygenerator = KeyGenerator.getInstance("DES");
                    SecretKey myDesKey = keygenerator.generateKey();

                    Cipher desCipher;
                    desCipher = Cipher.getInstance("DES");


                    byte[] text = content.getBytes("UTF8");


                    desCipher.init(Cipher.ENCRYPT_MODE, myDesKey);
                    byte[] textEncrypted = desCipher.doFinal(text);

                    String s = new String(textEncrypted);

                    try {

                         File statText = new File(x);
                         FileOutputStream is = new FileOutputStream(statText);

                         OutputStreamWriter osw = new OutputStreamWriter(is);  

                         Writer w = new BufferedWriter(osw);
                         w.write(s);
                         w.close();
                        } 
                 catch (IOException e3) {
                     JOptionPane.showMessageDialog(null, "something is wrong with the txt file");
                                    }                      
                }catch(Exception e1)
                {
                    System.out.println("En");
                }

这里是解密部分:

 path = chooser.getSelectedFile();
             String x = path.toString();

             try {

                 Scanner scanner = new Scanner(new File(x));
                content = scanner.useDelimiter("\\Z").next();

            } catch (FileNotFoundException e2) {
                e2.printStackTrace();
            }

             try{
                    KeyGenerator keygenerator = KeyGenerator.getInstance("DES");
                    SecretKey myDesKey = keygenerator.generateKey();

                    Cipher desCipher;
                    desCipher = Cipher.getInstance("DES");


                    byte[] text = content.getBytes("UTF8");
                    byte[] textEncrypted = desCipher.doFinal(text);


                    desCipher.init(Cipher.DECRYPT_MODE, myDesKey);
                    byte[] textDecrypted = desCipher.doFinal(textEncrypted);

                    String s = new String(textDecrypted);
                    System.out.println(s);


                }catch(Exception e1)
                {
                    System.out.println("De"); /* here we go this is my error*/
                }

第二个代码“catch(Exception e1)”的错误

【问题讨论】:

  • 那么你得到了什么错误?
  • 您需要查看异常对象,它会告诉您确切的问题所在。您也不应该使用 DES,因为它不被认为是安全的。
  • 你贴了两个代码块,第一个是加密,第二个是加密和解密。哪个部分出错了?
  • @OcelotcR 第二个代码 Exception e1 at the comment
  • "De" 不是错误,它是一个无意义的字符串。将 System.out.println 替换为 e1.printStackTrace();

标签: java encryption utf-8 cryptography des


【解决方案1】:

我在您的代码中发现了两个问题。在您的第一部分中,您只发送了加密的消息,而接收者不会解密,因为您没有将密钥发送给接收者。第二个问题是你使用String s = new String(textEncrypted);,你应该发送textEncrypted而不是字符串,因为将字节转换为字符串可能会由于编码而丢失一些信息。

在您的第二部分中,您应该在加密之前调用desCipher.init(Cipher.ENCRYPT_MODE, myDesKey);

【讨论】:

  • 密钥是固定的“DESKey@fffe78a6”唯一保持变化的是“desCipher”它始终具有不同的值
  • 你遇到了什么错误?在第一部分还是第二部分?如果报错是初始化错误,看我的回答。
  • @abdulraman:那是不是的关键。那是描述 引用 DESKey 对象的字符串,而不是它的值。
  • @JamesKPolk 我打印了密钥值,我很狡猾地相信它是密钥但是当我删除“UTF 8”并更改一些代码时一切正常,我可以加密文本 4 次并解密4 次,一切都恢复原样。
【解决方案2】:

问题解决了

加密代码:

 path = chooser.getSelectedFile();
             String x = path.toString();

             try {
                 scanner = new Scanner(new File(x));
                content = scanner.useDelimiter("\\Z").next();
            } catch (FileNotFoundException e2) {
                e2.printStackTrace();
            }
                try{
                    String desKey = "0123456789abcdef";   
                    byte[] keyBytes = DatatypeConverter.parseHexBinary(desKey);
                    SecretKeyFactory factory = SecretKeyFactory.getInstance("DES");
                    SecretKey key = factory.generateSecret(new DESKeySpec(keyBytes));



                    desCipher = Cipher.getInstance("DES");



                    byte[] text = content.getBytes();
                    desCipher.init(Cipher.ENCRYPT_MODE, key);
                    byte[] textEncrypted = desCipher.doFinal(text);
                    String s = new String(textEncrypted);
                    try {
                         File statText = new File(x);
                         FileOutputStream is = new FileOutputStream(statText);
                         OutputStreamWriter osw = new OutputStreamWriter(is);    
                         Writer w = new BufferedWriter(osw);
                         w.write(s);
                         w.close();
                        } 
                 catch (IOException e3) {
                     JOptionPane.showMessageDialog(null, "something is wrong with the txt file");
                                    }


                }catch(Exception e1)
                {
                    System.out.println("relax and try again");
                }

解密代码:

 path = chooser.getSelectedFile();
             String x = path.toString();

             try {

                 Scanner scanner = new Scanner(new File(x));
                content = scanner.useDelimiter("\\Z").next();
            } catch (FileNotFoundException e2) {
                e2.printStackTrace();
            }

             try{
                 String desKey = "0123456789abcdef";   
                 byte[] keyBytes = DatatypeConverter.parseHexBinary(desKey);
                 SecretKeyFactory factory = SecretKeyFactory.getInstance("DES");
                 SecretKey key = factory.generateSecret(new DESKeySpec(keyBytes));


                    desCipher = Cipher.getInstance("DES");


                    byte[] text = content.getBytes();




                    desCipher.init(Cipher.DECRYPT_MODE, key);               
                    byte[] textEncrypted = desCipher.doFinal(text);
                    String s = new String(textEncrypted);



                    try {
                         File statText = new File(x);
                         FileOutputStream is = new FileOutputStream(statText);
                         OutputStreamWriter osw = new OutputStreamWriter(is);    
                         Writer w = new BufferedWriter(osw);
                         w.write(s);
                         w.close();
                        } 
                 catch (IOException e3) {
                     JOptionPane.showMessageDialog(null, "something is wrong with the txt file");
                                    }

                }catch(Exception e24)
                {
                    System.out.println("relax and try again");
                }

谢谢大家。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-03-17
    • 2014-03-13
    • 1970-01-01
    • 1970-01-01
    • 2012-06-19
    • 1970-01-01
    • 2018-05-19
    相关资源
    最近更新 更多