2015-05-28 26 views
1

我想创建一个功能性的Java聊天应用程序。 因此,我有一个小应用程序,它允许用户通过服务器类进行连接,并通过客户端类相互交谈,并且我已经开始添加加密。我无法在我的Java聊天应用程序中解密来自其他客户端的输出。如何在java中解密(包括我的代码片段)

有人可以帮我吗?

的我的代码段包含如下:

THE CLIENTGUI.JAVA CLASS(加密是一个按钮被点击)

if(o == encrypt) { 

     String change = null; 
     try{ 
      change = tf.getText(); 
      change = FileEncryption.encryptString(change); 
      tf.setText("" + change); 

      return; 
     } catch (Exception e1) { 
      // TODO Auto-generated catch block 
      e1.printStackTrace(); 
     } 
     finally{ 
     } 

THE FILEENCRYPTION.JAVA

public class FileEncryption { 

    //Initial Vector 
    public static final byte[] iv = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };  

    //EncryptAndDecrypt String -> Input : PlainText + Return : CipherText+DecipherText 
    public static String encryptString(String src) throws Exception 
    { 
     String dst=""; 
     //Not Input! 
     if(src == null || src.length()==0) 
      return ""; 

     //Encryption Setting 
     byte[] k="Multimediaproces".getBytes(); 
     SecretKeySpec Key = new SecretKeySpec(k,"AES"); 
     IvParameterSpec ivspec = new IvParameterSpec(iv); 
     Cipher encryptCipher = Cipher.getInstance("AES/CBC/PKCS5Padding"); 
     encryptCipher.init(Cipher.ENCRYPT_MODE,Key,ivspec); 

     ByteArrayOutputStream baos = new ByteArrayOutputStream(); 
     CipherOutputStream cout = new CipherOutputStream(baos,encryptCipher); 
     cout.write(src.getBytes()); 
     cout.flush();    //ByteOutputStream -> Write Encryption Text 
     cout.close();   
    // in encrypt method 
     dst = DatatypeConverter.printHexBinary(baos.toByteArray()); 
     return dst; 
    } 

    //String src -> EncryptedData 
    public static String decryptString(String src) throws Exception 
    { 
     //src value is Encrypted Value! 
     //So, src value -> Not Byte! 
     String dst=""; 
     byte[] encryptedBytes = DatatypeConverter.parseHexBinary(src);;   
     //Not Input! 
     if(src == null || src.length()==0) 
      return "";   
     //Decryption Setting 
     IvParameterSpec ivspec = new IvParameterSpec(iv); 
     byte[] k="Multimediaproces".getBytes(); 
     SecretKeySpec Key = new SecretKeySpec(k,"AES"); 
     Cipher decryptCipher = Cipher.getInstance("AES/CBC/PKCS5Padding"); 
     decryptCipher.init(Cipher.DECRYPT_MODE,Key,ivspec); 

     ByteArrayOutputStream baos = new ByteArrayOutputStream(); 
     ByteArrayInputStream bais = new ByteArrayInputStream(encryptedBytes); 
     CipherInputStream cin = new CipherInputStream(bais,decryptCipher); 
     byte[] buf = new byte[1024]; 
     int read; 
     while((read=cin.read(buf))>=0) //reading encrypted data! 
     { 
      baos.write(buf,0,read);  //writing decrypted data! 
     } 

     // closing streams 
     cin.close(); 
     dst = new String(baos.toByteArray()); 
     return dst; 
    } 
} 

问题是当我试图解密输入以下代码的代码: if(o == decrypt){

  try{ 
       msg = tf.getText(); 
       msg = FileEncryption.decryptString(msg); 
       fop. 
      } catch (Exception e1) { 
       // TODO Auto-generated catch block 
       e1.printStackTrace(); 
      }finally{ 

      } 

目前,它允许我加密输入到文本字段中的内容。

它不允许我解密用户在聊天中所说的内容。我所包含的用于解密的当前代码不起作用。

任何人都可以帮助我吗?或者对我的程序有任何建议可以帮助解密?

感谢

编辑:

currently this is what my application looks like. The window at the bottom is the Server class where it can show who is logged in etc. the top left shows the client chat for me 'Harry'. in the text box shows when i have clicked the encrypt button. however, clicking the decrypt button does not work

+1

'fop.'不是一个有效的Java语句。你的实际代码是什么? – immibis

+0

测试你的加密和解密方法似乎对我来说很好......我想知道是否对当前机器有某种依赖性? – MadProgrammer

+0

啊道歉,fop。不是这个的一部分。解密位有点写我看起来没有用。 –

回答

1

您最好的选择可能是简单地使用SSL插座为您的网络通讯,而不是自己编写的加密代码。虽然你的问题是不完全这样一个的副本,你很可能得到很好的被这里的答案服务:

Secret Key SSL Socket connections in Java

+0

这是假设OP的目的是做些东西供使用,而不是学习一些基本的密码学。 – immibis

+1

TLS/SSL也有一些要求(证书...),如果您不知道密码是如何工作的,那么这些要求就没有意义。 – immibis

+0

确实。如果他正在尝试学习密码技术,学习TLS/SSL将是一个很好的开始。 –

1

我怀疑问题是不是经过2个客户端之间的加密状态。

如果“加密”对象是一个按钮,那么它只是客户端 - 客户端连接的一侧的按钮。您需要将加密状态传递给另一个客户端,以便知道解密该消息。

确认这一点的捷径是在接收端自动显示明文和解密消息。其中之一将永远是胡言乱语,但它应该改变,取决于使用加密按钮。

祝你好运:)

+0

您好格雷戈里,因为我在编程上相当业余,所以我不太明白你的意思。 从我目前的理解,你已经说过,我需要从一边发送加密的消息,并在接收端收到加密的消息。问题是,在接收方,解密仍然不起作用。该按钮不起作用。我已经确认它可以被使用,使它在代码之上更加真实......基本上,我想在文本字段中解密它,如果可能的话,或者任何方式...... –

+0

鉴于其他人似乎已经证实你加密/解密方法的工作,我的建议是“o ==解密”是问题。 我建议删除测试并输出所有消息的纯文本和解密消息,以确保方法正在调用和工作。 –