Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sockets/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 使用套接字传输加密文件,并使用DES和MD5解密文件_Java_Sockets_Encryption_Cryptography_Des - Fatal编程技术网

Java 使用套接字传输加密文件,并使用DES和MD5解密文件

Java 使用套接字传输加密文件,并使用DES和MD5解密文件,java,sockets,encryption,cryptography,des,Java,Sockets,Encryption,Cryptography,Des,我正在做一个项目,其中的数据传输应该是安全的。我使用MD5和DES的基于密码的加密来加密文件。 用于加密文件的类: public class FileEncryptor { private static String filename; private static String password; private static FileInputStream inFile; private static FileOutputStream outFile;

我正在做一个项目,其中的数据传输应该是安全的。我使用MD5和DES的基于密码的加密来加密文件。 用于加密文件的类:

public class FileEncryptor {    
    private static String filename;
    private static String password;
    private static FileInputStream inFile;
    private static FileOutputStream outFile;
    public static String tempFilename;
    public static File tempFile;

    public static File encryptFile(File f, String passkey) throws Exception {
        if(f.isDirectory()) {
            JOptionPane.showMessageDialog(null, "file object is a directory");
            return null;
        }
        filename = f.getPath();
        password = passkey;     
        //Need to create a temporary file which is filled with the encrypted data.
        tempFilename = filename + ".des";
        tempFile = new File(tempFilename);      
        inFile = new FileInputStream(f);
        outFile = new FileOutputStream(tempFile);       
        // Use PBEKeySpec to create a key based on a password.
        // The password is passed as a character array.
        PBEKeySpec keySpec = new PBEKeySpec(password.toCharArray());
        SecretKeyFactory sKeyFac = SecretKeyFactory.getInstance("PBEWithMD5AndDES");
        SecretKey sKey = sKeyFac.generateSecret(keySpec);       
        byte[] salt = new byte[8];
        Random rnd = new Random();
        rnd.nextBytes(salt);
        int iterations = 100;       
         //Create the parameter spec for this salt and iteration count
        PBEParameterSpec parameterSpec = new PBEParameterSpec(salt, iterations);
        //Create the cipher and initiate it for encryption
        Cipher c = Cipher.getInstance("PBEWithMD5AndDES");
        c.init(Cipher.ENCRYPT_MODE, sKey, parameterSpec);

        //Need to write the salt into the file. It is required for decryption
        outFile.write(salt);

        //Read the file and encrypt its bytes
        byte[] input = new byte[64];
        int bytesRead;
        while((bytesRead = inFile.read(input)) != -1) {
            byte[] output = c.update(input, 0, bytesRead);
            if(output != null) { outFile.write(output); }           
        }

        byte[] output = c.doFinal();
        if(output != null) { outFile.write(output); }

        //Closing the streams before exiting.
        inFile.close();
        outFile.flush();
        outFile.close();

        return tempFile;
    }

}
用于解密文件的类:

发送逻辑:

接收逻辑:

没有抛出异常,但也没有传输数据。加密部分已成功完成。但是传输和解密没有发生。我检查了无加密传输文件的代码,它正在工作。在不进行文件传输的情况下单独使用时,加密和解密类也可以工作。有人能指出我哪里出了问题吗。我很抱歉,如果代码太长,但必须提供我尝试过的内容

您没有关闭服务器中的操作系统,因此客户端复制循环永远不会终止

当您修复该问题时,您会发现文件太大

fis.read(b);
System.out.println("Sending the encrypted data");
os.write(b);
问题是,您的两个不同的复制过程都是错误的。Java中复制流的规范方法如下:

byte[] buffer = new byte[8192]; // or more
int count;
while ((count = in.read(buffer)) > 0)
{
    out.write(buffer, 0, count);
}

两头都用这个。您不需要文件大小或更大的缓冲区。

启动调试器,您会在瞬间变得更聪明。您是否正在与70、80或90年代的人通信?因为DES和MD5早就被弃用了。您肯定应该重新进行流处理测试。有时你需要一个循环并关闭一个流,也推荐使用资源进行尝试。@MaartenBodewes hahaha抱歉,这是我的大学项目。没问题,但在这种情况下,你的教授似乎陷入了某种统计领域。“我不知道为什么他会教这种技术,而有更多最新的技术,如AES和PBKDF2,同样容易学习。”MaartenBodewes我们提出了这个项目,既然他接受了,我无论如何都必须完成这个项目。我们为自己挖了一个坟墓非常感谢你。希望能有帮助
public static void main(String[] args) {
       // TODO Auto-generated method stub
       File f = new File("D:\\P2PFolder\\ToDec.txt.des");
       FileOutputStream fos;
       InputStream is;
       Socket s;
       int bytesRead = 0;
       int current = 0;
       byte[] rb = new byte[6022386];
       try {
           fos = new FileOutputStream(f);
           System.out.println("Connecting.....");
            s = new Socket(InetAddress.getLocalHost(), 7007);
            System.out.println("Connected!!!");
            is = s.getInputStream();
            do {
                System.out.println("Reading encrypted data from socket");
                bytesRead = is.read(rb, current, (rb.length - current));
                System.out.println(new String(rb) + bytesRead);
                if(bytesRead > 0) {
                    current += bytesRead;
                }
            } while(bytesRead > -1);
           fos.write(rb);
           is.close();
           fos.flush();
           fos.close();
           System.out.println("Decrypting the file");
           FileDecryptor.decryptFile(f, "impetus");
       } catch (Exception e) {
           e.printStackTrace();
       }
   }
fis.read(b);
System.out.println("Sending the encrypted data");
os.write(b);
byte[] buffer = new byte[8192]; // or more
int count;
while ((count = in.read(buffer)) > 0)
{
    out.write(buffer, 0, count);
}