|
1 | | -/* |
2 | | - * Copyright 2010 Srikanth Reddy Lingala |
3 | | - * |
4 | | - * Licensed under the Apache License, Version 2.0 (the "License"); |
5 | | - * you may not use this file except in compliance with the License. |
6 | | - * You may obtain a copy of the License at |
7 | | - * |
8 | | - * http://www.apache.org/licenses/LICENSE-2.0 |
9 | | - * |
10 | | - * Unless required by applicable law or agreed to in writing, |
11 | | - * software distributed under the License is distributed on an "AS IS" BASIS, |
12 | | - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
13 | | - * See the License for the specific language governing permissions and |
14 | | - * limitations under the License. |
15 | | - */ |
16 | | - |
17 | | -package net.lingala.zip4j.crypto.engine; |
18 | | - |
19 | | -public class ZipCryptoEngine { |
20 | | - |
21 | | - private final int keys[] = new int[3]; |
22 | | - private static final int[] CRC_TABLE = new int[256]; |
23 | | - |
24 | | - static { |
25 | | - for (int i = 0; i < 256; i++) { |
26 | | - int r = i; |
27 | | - for (int j = 0; j < 8; j++) { |
28 | | - if ((r & 1) == 1) { |
29 | | - r = (r >>> 1) ^ 0xedb88320; |
30 | | - } else { |
31 | | - r >>>= 1; |
32 | | - } |
33 | | - } |
34 | | - CRC_TABLE[i] = r; |
35 | | - } |
36 | | - } |
37 | | - |
38 | | - public void initKeys(char[] password) { |
39 | | - keys[0] = 305419896; |
40 | | - keys[1] = 591751049; |
41 | | - keys[2] = 878082192; |
42 | | - for (int i = 0; i < password.length; i++) { |
43 | | - updateKeys((byte) (password[i] & 0xff)); |
44 | | - } |
45 | | - } |
46 | | - |
47 | | - public void updateKeys(byte charAt) { |
48 | | - keys[0] = crc32(keys[0], charAt); |
49 | | - keys[1] += keys[0] & 0xff; |
50 | | - keys[1] = keys[1] * 134775813 + 1; |
51 | | - keys[2] = crc32(keys[2], (byte) (keys[1] >> 24)); |
52 | | - } |
53 | | - |
54 | | - private int crc32(int oldCrc, byte charAt) { |
55 | | - return ((oldCrc >>> 8) ^ CRC_TABLE[(oldCrc ^ charAt) & 0xff]); |
56 | | - } |
57 | | - |
58 | | - public byte decryptByte() { |
59 | | - int temp = keys[2] | 2; |
60 | | - return (byte) ((temp * (temp ^ 1)) >>> 8); |
61 | | - } |
62 | | -} |
| 1 | +/* |
| 2 | + * Copyright 2010 Srikanth Reddy Lingala |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, |
| 11 | + * software distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package net.lingala.zip4j.crypto.engine; |
| 18 | + |
| 19 | +import static net.lingala.zip4j.util.Zip4jUtil.convertCharArrayToByteArray; |
| 20 | + |
| 21 | +public class ZipCryptoEngine { |
| 22 | + |
| 23 | + private final int[] keys = new int[3]; |
| 24 | + private static final int[] CRC_TABLE = new int[256]; |
| 25 | + |
| 26 | + static { |
| 27 | + for (int i = 0; i < 256; i++) { |
| 28 | + int r = i; |
| 29 | + for (int j = 0; j < 8; j++) { |
| 30 | + if ((r & 1) == 1) { |
| 31 | + r = (r >>> 1) ^ 0xedb88320; |
| 32 | + } else { |
| 33 | + r >>>= 1; |
| 34 | + } |
| 35 | + } |
| 36 | + CRC_TABLE[i] = r; |
| 37 | + } |
| 38 | + } |
| 39 | + |
| 40 | + public void initKeys(char[] password) { |
| 41 | + keys[0] = 305419896; |
| 42 | + keys[1] = 591751049; |
| 43 | + keys[2] = 878082192; |
| 44 | + byte[] bytes = convertCharArrayToByteArray(password); |
| 45 | + for (byte b : bytes) { |
| 46 | + updateKeys((byte) (b & 0xff)); |
| 47 | + } |
| 48 | + } |
| 49 | + |
| 50 | + public void updateKeys(byte charAt) { |
| 51 | + keys[0] = crc32(keys[0], charAt); |
| 52 | + keys[1] += keys[0] & 0xff; |
| 53 | + keys[1] = keys[1] * 134775813 + 1; |
| 54 | + keys[2] = crc32(keys[2], (byte) (keys[1] >> 24)); |
| 55 | + } |
| 56 | + |
| 57 | + private int crc32(int oldCrc, byte charAt) { |
| 58 | + return ((oldCrc >>> 8) ^ CRC_TABLE[(oldCrc ^ charAt) & 0xff]); |
| 59 | + } |
| 60 | + |
| 61 | + public byte decryptByte() { |
| 62 | + int temp = keys[2] | 2; |
| 63 | + return (byte) ((temp * (temp ^ 1)) >>> 8); |
| 64 | + } |
| 65 | +} |
0 commit comments