如何在java中解密(包括我的代码片段)

时间:2015-05-28 01:09:23

标签: java encryption aes

我想创建一个功能性的Java聊天应用程序。 所以我有一个小应用程序,允许用户通过服务器类连接,并通过客户端类相互交谈,我已经开始添加加密。我在Java聊天应用程序中解密其他客户端的输出时遇到了问题。

有人可以帮助我吗?

我的代码片段包含在下面:

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

2 个答案:

答案 0 :(得分:1)

您最好的选择可能是简单地使用SSL套接字进行网络通信,而不是自己编写加密代码。虽然你的问题并不完全与这个问题重复,但你可能会得到答案:

Secret Key SSL Socket connections in Java

答案 1 :(得分:1)

我怀疑问题是没有在两个客户端之间传递加密状态。

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

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

祝你好运:)