田源
2024-03-07 4b4083fd73dc27ece42f4835483565eef0e4f608
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
package com.vci.server.volume.uitls;
 
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Arrays;
import java.util.zip.CRC32;
 
import org.apache.axis.utils.StringUtils;
 
public class EncryFileUtil {
    public static int ENCRYHEADERSIZE = 13;
    
    public static boolean isEncryFile(String fileName) {
        File file = new File(fileName);
        if (!file.exists())
            return false;
        
        if (file.length() < ENCRYHEADERSIZE)
            return false;
        
        try {
            FileInputStream in = new FileInputStream(file);
            byte[] data = new byte[ENCRYHEADERSIZE];
            in.read(data, 0, ENCRYHEADERSIZE);
            in.close();
            
            byte[] header = getEncryHeader(fileName);
            return Arrays.equals(data, header);
            
        } catch (IOException e) {
            return false;
        }
    }
 
    public static boolean isEncryFile(File file) {
        if (!file.exists())
            return false;
        
        if (file.length() < ENCRYHEADERSIZE)
            return false;
        
        try {
            FileInputStream in = new FileInputStream(file);
            byte[] data = new byte[ENCRYHEADERSIZE];
            in.read(data, 0, ENCRYHEADERSIZE);
            in.close();
            
            
            byte[] header = getEncryHeader(file.getPath());
            return Arrays.equals(data, header);
            
        } catch (IOException e) {
            return false;
        }
    }
 
    public static byte[] getEncryHeader(String fileName) {
        if (StringUtils.isEmpty(fileName))
            return null;
        
        long crc = 0;
        File file = new File(fileName);
        String name = file.getName().toLowerCase();
        crc = getCRC(name);
        
        // 先写入“VEH1#”标识
        byte[] data = new byte[ENCRYHEADERSIZE];
        data[0] = (byte)'V';
        data[1] = (byte)'E';
        data[2] = (byte)'H';
        data[3] = (byte)'1';
        data[4] = (byte)'#';
        
        long temp = crc;
        for (int i = 0; i < 8; i++) { 
            data[i + 5] = new Long(temp & 0xff).byteValue();// 
            temp = temp >> 8; // 向右移8位 
        }
        
        
        return data;
    }
    
    /**
     * 计算CRC16校验码
     *
     * @param bytes
     * @return
     */
    private static long getCRC(String text) {
        byte[] b = text.getBytes();
        CRC32 c = new CRC32();
        c.reset();//Resets CRC-32 to initial value.
        c.update(b, 0, b.length);//将数据丢入CRC32解码器
        long value = (int) c.getValue();//获取CRC32 的值  默认返回值类型为long 用于保证返回值是一个正数
        
        return value;
    }
}