Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/image/5.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_Java_Hash_Md5_Dex - Fatal编程技术网

散列将不同的值返回到同一个文件输入-Java

散列将不同的值返回到同一个文件输入-Java,java,hash,md5,dex,Java,Hash,Md5,Dex,我在java中散列.dex文件,并且我机器中的代码与其他机器中具有相同文件的相同代码之间的输出不同。在线散列生成器也存在差异。 我尝试过改变编码,但那不是问题所在 Java代码: public static String MD5(String md5) { try { java.security.MessageDigest md = java.security.MessageDigest.getInstance("SHA-1"); b

我在java中散列.dex文件,并且我机器中的代码与其他机器中具有相同文件的相同代码之间的输出不同。在线散列生成器也存在差异。 我尝试过改变编码,但那不是问题所在

Java代码:

public static String MD5(String md5) {
       try {
            java.security.MessageDigest md = java.security.MessageDigest.getInstance("SHA-1");
            byte[] array = null;
            try {
                array = md.digest(md5.getBytes());
            } catch (UnsupportedEncodingException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            StringBuffer sb = new StringBuffer();
            for (int i = 0; i < array.length; ++i) {
              sb.append(Integer.toHexString((array[i] & 0xFF) | 0x100).substring(1,3));
           }
            return sb.toString();
        } catch (java.security.NoSuchAlgorithmException e) {
        }
        return null;
    }
    public static byte[] readExe(Path path)
    {
        byte[] fileByte = null;
        try {
            fileByte = Files.readAllBytes(path);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return fileByte ;

    }
    public static  void write_signature_to_textFile(String location, String data)
    {
        String signLocation = "d:\\Users\\user-pc\\Desktop\\magshimim\\year 3\\signs.txt";
        File file =new File(signLocation);
        //if file doesn't exists, then create it
        if(!file.exists()){
            try {
                file.createNewFile();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }

        FileWriter fw = null;
        try {
            fw = new FileWriter(file,true);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        BufferedWriter bufferWritter = new BufferedWriter(fw);
        try {
            bufferWritter.write(location);
            bufferWritter.write(":");
            bufferWritter.newLine();
            bufferWritter.write(data);
            bufferWritter.newLine();
            bufferWritter.newLine();
            bufferWritter.newLine();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        try {
            bufferWritter.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    public void write_signature_to_binaryFile(String location, byte[] data)
    {
        FileOutputStream out = null;
        try {
            out = new FileOutputStream(location);
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        try {
            out.write(data);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        try {
            out.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }


    public static void create_list_of_dexFiles(String pathToScan, String ending)
    {
        String target_file = "";
        File folderToScan = new File(pathToScan);
        File[] listOfFiles = folderToScan.listFiles();
        for (int i = 0; i < listOfFiles.length; i++)
        {
             if (listOfFiles[i].isFile())
             {
                 target_file = listOfFiles[i].getAbsolutePath();
                 if (target_file.endsWith("."+ending))
                 {
                     //check whether trget_file product path with / or // . if / write script to change to //
                        byte[] exe_file_bytes = null;
                        String exe_location = target_file ;
                        Path exe_path = Paths.get(exe_location);
                        exe_file_bytes = readExe(exe_path);
                        String string_exe_file_bytes = new String(exe_file_bytes);
                        String result = MD5(string_exe_file_bytes);
                        System.out.println(result);
                        write_signature_to_textFile(exe_location, result);

                 }  
             }
             else if(listOfFiles[i].isDirectory())
             {
                 create_list_of_dexFiles(listOfFiles[i].getAbsolutePath(), ending);
             }
        }
        return;
    }

    public static void main(String[] args) 
    {

        String pathToScan = "d:\\Users\\user-pc\\Desktop\\magshimim\\year 3\\Android Viruses";
        create_list_of_dexFiles(pathToScan, "dex");
公共静态字符串MD5(字符串MD5){
试一试{
java.security.MessageDigest md=java.security.MessageDigest.getInstance(“SHA-1”);
字节[]数组=null;
试一试{
array=md.digest(md5.getBytes());
}捕获(不支持的编码异常e){
//TODO自动生成的捕捉块
e、 printStackTrace();
}
StringBuffer sb=新的StringBuffer();
对于(int i=0;i
您确定要对相同的文件进行哈希处理吗?换行符可能已损坏,等等。在运行程序之前,您可以尝试在这些文件上手动运行现有的md5摘要实用程序,并比较计算机之间以及实用程序与现有实用程序之间的输出。@JesusFreke我尝试过了。不同每种方式的输出。有什么不同的输出?我的3个输出,来自在线哈希生成器和另一台机器,都是彼此不同的。如果您得到的文件本身的哈希不同,请在两个位置使用“已知良好”工具(即不使用您的代码),则文件本身不同。不能期望不同的文件散列到相同的值。