33
44namespace UAssetAPI
55{
6- public class AC7XorKey
7- {
8- public int NameKey ;
9- public int Offset ;
10- public int pk1 ;
11- public int pk2 ;
6+ public class AC7XorKey
7+ {
8+ public int NameKey ;
9+ public int Offset ;
10+ public int pk1 ;
11+ public int pk2 ;
1212
13- public void SkipCount ( int count )
14- {
15- int num = count % 217 ;
16- pk1 += num ;
17- if ( pk1 >= 217 )
18- {
19- pk1 -= 217 ;
20- }
21- int num2 = count % 1024 ;
22- pk2 += num2 ;
23- if ( pk2 >= 1024 )
24- {
25- pk2 -= 1024 ;
26- }
27- }
13+ public void SkipCount ( int count )
14+ {
15+ int num = count % 217 ;
16+ pk1 += num ;
17+ if ( pk1 >= 217 )
18+ {
19+ pk1 -= 217 ;
20+ }
21+ int num2 = count % 1024 ;
22+ pk2 += num2 ;
23+ if ( pk2 >= 1024 )
24+ {
25+ pk2 -= 1024 ;
26+ }
27+ }
2828
2929 public AC7XorKey ( int nameKey , int offset )
3030 {
@@ -33,178 +33,178 @@ public AC7XorKey(int nameKey, int offset)
3333 }
3434 }
3535
36- public class AC7Decrypt
36+ public class AC7Decrypt
3737 {
38- private static byte [ ] AC7FullKey = new byte [ 0 ] ;
38+ private static byte [ ] AC7FullKey = new byte [ 0 ] ;
3939
40- public AC7Decrypt ( )
40+ public AC7Decrypt ( )
4141 {
42- if ( AC7FullKey . Length == 0 ) AC7FullKey = Properties . Resources . AC7Key ;
42+ if ( AC7FullKey . Length == 0 ) AC7FullKey = Properties . Resources . AC7Key ;
4343 }
4444
45- public void Decrypt ( string input , string output )
45+ public void Decrypt ( string input , string output )
4646 {
47- AC7XorKey xorKey = GetXorKey ( Path . GetFileNameWithoutExtension ( input ) ) ;
48- byte [ ] doneData = DecryptUAssetBytes ( File . ReadAllBytes ( input ) , xorKey ) ;
49- File . WriteAllBytes ( output , doneData ) ;
50- try
51- {
52- byte [ ] doneData2 = DecryptUexpBytes ( File . ReadAllBytes ( Path . ChangeExtension ( input , "uexp" ) ) , xorKey ) ;
53- File . WriteAllBytes ( Path . ChangeExtension ( output , "uexp" ) , doneData2 ) ;
54- }
55- catch { }
56- }
47+ AC7XorKey xorKey = GetXorKey ( Path . GetFileNameWithoutExtension ( input ) ) ;
48+ byte [ ] doneData = DecryptUAssetBytes ( File . ReadAllBytes ( input ) , xorKey ) ;
49+ File . WriteAllBytes ( output , doneData ) ;
50+ try
51+ {
52+ byte [ ] doneData2 = DecryptUexpBytes ( File . ReadAllBytes ( Path . ChangeExtension ( input , "uexp" ) ) , xorKey ) ;
53+ File . WriteAllBytes ( Path . ChangeExtension ( output , "uexp" ) , doneData2 ) ;
54+ }
55+ catch { }
56+ }
5757
58- public void Encrypt ( string input , string output )
58+ public void Encrypt ( string input , string output )
5959 {
60- AC7XorKey xorKey = GetXorKey ( Path . GetFileNameWithoutExtension ( input ) ) ;
61- byte [ ] doneData = EncryptUAssetBytes ( File . ReadAllBytes ( input ) , xorKey ) ;
62- File . WriteAllBytes ( output , doneData ) ;
63- try
64- {
65- byte [ ] doneData2 = EncryptUexpBytes ( File . ReadAllBytes ( Path . ChangeExtension ( input , "uexp" ) ) , xorKey ) ;
66- File . WriteAllBytes ( Path . ChangeExtension ( output , "uexp" ) , doneData2 ) ;
67- }
68- catch { }
69- }
60+ AC7XorKey xorKey = GetXorKey ( Path . GetFileNameWithoutExtension ( input ) ) ;
61+ byte [ ] doneData = EncryptUAssetBytes ( File . ReadAllBytes ( input ) , xorKey ) ;
62+ File . WriteAllBytes ( output , doneData ) ;
63+ try
64+ {
65+ byte [ ] doneData2 = EncryptUexpBytes ( File . ReadAllBytes ( Path . ChangeExtension ( input , "uexp" ) ) , xorKey ) ;
66+ File . WriteAllBytes ( Path . ChangeExtension ( output , "uexp" ) , doneData2 ) ;
67+ }
68+ catch { }
69+ }
7070
71- public byte [ ] DecryptUAssetBytes ( byte [ ] uasset , AC7XorKey xorkey )
72- {
73- uint num = BitConverter . ToUInt32 ( uasset , 0 ) ;
74- if ( num != UAsset . ACE7_MAGIC )
75- {
76- return uasset ;
77- }
78- byte [ ] array = new byte [ uasset . Length ] ;
79- array [ 0 ] = 193 ;
80- array [ 1 ] = 131 ;
81- array [ 2 ] = 42 ;
82- array [ 3 ] = 158 ;
83- for ( int i = 4 ; i < array . Length ; i ++ )
84- {
85- array [ i ] = GetXorByte ( uasset [ i ] , ref xorkey ) ;
86- }
87- return array ;
88- }
71+ public byte [ ] DecryptUAssetBytes ( byte [ ] uasset , AC7XorKey xorkey )
72+ {
73+ uint num = BitConverter . ToUInt32 ( uasset , 0 ) ;
74+ if ( num != UAsset . ACE7_MAGIC )
75+ {
76+ return uasset ;
77+ }
78+ byte [ ] array = new byte [ uasset . Length ] ;
79+ array [ 0 ] = 193 ;
80+ array [ 1 ] = 131 ;
81+ array [ 2 ] = 42 ;
82+ array [ 3 ] = 158 ;
83+ for ( int i = 4 ; i < array . Length ; i ++ )
84+ {
85+ array [ i ] = GetXorByte ( uasset [ i ] , ref xorkey ) ;
86+ }
87+ return array ;
88+ }
8989
90- // Unsure if this works or not, feel free to modify & PR
91- public byte [ ] EncryptUAssetBytes ( byte [ ] uasset , AC7XorKey xorkey )
92- {
93- uint num = BitConverter . ToUInt32 ( uasset , 0 ) ;
94- if ( num != UAsset . UASSET_MAGIC )
95- {
96- return uasset ;
97- }
98- byte [ ] array = new byte [ uasset . Length ] ;
99- array [ 0 ] = 65 ;
100- array [ 1 ] = 67 ;
101- array [ 2 ] = 69 ;
102- array [ 3 ] = 55 ;
103- for ( int i = 4 ; i < array . Length ; i ++ )
104- {
105- array [ i ] = GetXorByte ( uasset [ i ] , ref xorkey ) ;
106- }
107- return array ;
108- }
90+ // Unsure if this works or not, feel free to modify & PR
91+ public byte [ ] EncryptUAssetBytes ( byte [ ] uasset , AC7XorKey xorkey )
92+ {
93+ uint num = BitConverter . ToUInt32 ( uasset , 0 ) ;
94+ if ( num != UAsset . UASSET_MAGIC )
95+ {
96+ return uasset ;
97+ }
98+ byte [ ] array = new byte [ uasset . Length ] ;
99+ array [ 0 ] = 65 ;
100+ array [ 1 ] = 67 ;
101+ array [ 2 ] = 69 ;
102+ array [ 3 ] = 55 ;
103+ for ( int i = 4 ; i < array . Length ; i ++ )
104+ {
105+ array [ i ] = GetXorByte ( uasset [ i ] , ref xorkey ) ;
106+ }
107+ return array ;
108+ }
109109
110- public byte [ ] DecryptUexpBytes ( byte [ ] uexp , AC7XorKey xorkey )
111- {
112- if ( xorkey == null )
113- {
114- return uexp ;
115- }
116- byte [ ] array = new byte [ uexp . Length ] ;
117- for ( int i = 0 ; i < array . Length ; i ++ )
118- {
119- array [ i ] = GetXorByte ( uexp [ i ] , ref xorkey ) ;
120- }
121- array [ array . Length - 4 ] = 193 ;
122- array [ array . Length - 3 ] = 131 ;
123- array [ array . Length - 2 ] = 42 ;
124- array [ array . Length - 1 ] = 158 ;
125- return array ;
126- }
110+ public byte [ ] DecryptUexpBytes ( byte [ ] uexp , AC7XorKey xorkey )
111+ {
112+ if ( xorkey == null )
113+ {
114+ return uexp ;
115+ }
116+ byte [ ] array = new byte [ uexp . Length ] ;
117+ for ( int i = 0 ; i < array . Length ; i ++ )
118+ {
119+ array [ i ] = GetXorByte ( uexp [ i ] , ref xorkey ) ;
120+ }
121+ array [ array . Length - 4 ] = 193 ;
122+ array [ array . Length - 3 ] = 131 ;
123+ array [ array . Length - 2 ] = 42 ;
124+ array [ array . Length - 1 ] = 158 ;
125+ return array ;
126+ }
127127
128- // Unsure if this works or not, feel free to modify & PR
129- public byte [ ] EncryptUexpBytes ( byte [ ] uexp , AC7XorKey xorkey )
130- {
131- byte [ ] array = new byte [ uexp . Length + 4 ] ;
132- for ( int i = 0 ; i < uexp . Length ; i ++ )
133- {
134- array [ i ] = GetXorByte ( uexp [ i ] , ref xorkey ) ;
135- }
136- for ( int j = uexp . Length ; j < array . Length ; j ++ )
137- {
138- array [ j ] = GetXorByte ( 0 , ref xorkey ) ;
139- }
140- return array ;
141- }
128+ // Unsure if this works or not, feel free to modify & PR
129+ public byte [ ] EncryptUexpBytes ( byte [ ] uexp , AC7XorKey xorkey )
130+ {
131+ byte [ ] array = new byte [ uexp . Length + 4 ] ;
132+ for ( int i = 0 ; i < uexp . Length ; i ++ )
133+ {
134+ array [ i ] = GetXorByte ( uexp [ i ] , ref xorkey ) ;
135+ }
136+ for ( int j = uexp . Length ; j < array . Length ; j ++ )
137+ {
138+ array [ j ] = GetXorByte ( 0 , ref xorkey ) ;
139+ }
140+ return array ;
141+ }
142142
143- public static AC7XorKey GetXorKey ( string fname )
144- {
145- AC7XorKey key = new AC7XorKey ( CalcNameKey ( fname ) , 4 ) ;
146- CalcPKeyFromNKey ( key . NameKey , key . Offset , out key . pk1 , out key . pk2 ) ;
147- return key ;
148- }
143+ public static AC7XorKey GetXorKey ( string fname )
144+ {
145+ AC7XorKey key = new AC7XorKey ( CalcNameKey ( fname ) , 4 ) ;
146+ CalcPKeyFromNKey ( key . NameKey , key . Offset , out key . pk1 , out key . pk2 ) ;
147+ return key ;
148+ }
149149
150- private static int CalcNameKey ( string fname )
151- {
152- fname = fname . ToUpper ( ) ;
153- int num = 0 ;
154- for ( int i = 0 ; i < fname . Length ; i ++ )
155- {
156- int num2 = ( byte ) fname [ i ] ;
157- num ^= num2 ;
158- num2 = num * 8 ;
159- num2 ^= num ;
160- int num3 = num + num ;
161- num2 = ~ num2 ;
162- num2 = ( num2 >> 7 ) & 1 ;
163- num = num2 | num3 ;
164- }
165- return num ;
166- }
150+ private static int CalcNameKey ( string fname )
151+ {
152+ fname = fname . ToUpper ( ) ;
153+ int num = 0 ;
154+ for ( int i = 0 ; i < fname . Length ; i ++ )
155+ {
156+ int num2 = ( byte ) fname [ i ] ;
157+ num ^= num2 ;
158+ num2 = num * 8 ;
159+ num2 ^= num ;
160+ int num3 = num + num ;
161+ num2 = ~ num2 ;
162+ num2 = ( num2 >> 7 ) & 1 ;
163+ num = num2 | num3 ;
164+ }
165+ return num ;
166+ }
167167
168- private static void CalcPKeyFromNKey ( int nkey , int dataoffset , out int pk1 , out int pk2 )
169- {
170- long num = ( uint ) ( ( long ) nkey * 7L ) ;
171- System . Numerics . BigInteger bigInteger = new System . Numerics . BigInteger ( 5440514381186227205L ) ;
172- num += dataoffset ;
173- System . Numerics . BigInteger bigInteger2 = bigInteger * num ;
174- long num2 = ( long ) ( bigInteger2 >> 70 ) ;
175- long num3 = num2 >> 63 ;
176- num2 += num3 ;
177- num3 = num2 * 217 ;
178- num -= num3 ;
179- pk1 = ( int ) ( num & 0xFFFFFFFFu ) ;
180- long num4 = ( uint ) ( ( long ) nkey * 11L ) ;
181- num4 += dataoffset ;
182- num2 = 0L ;
183- num2 &= 0x3FF ;
184- num4 += num2 ;
185- num4 &= 0x3FF ;
186- long num5 = num4 - num2 ;
187- pk2 = ( int ) ( num5 & 0xFFFFFFFFu ) ;
188- }
168+ private static void CalcPKeyFromNKey ( int nkey , int dataoffset , out int pk1 , out int pk2 )
169+ {
170+ long num = ( uint ) ( ( long ) nkey * 7L ) ;
171+ System . Numerics . BigInteger bigInteger = new System . Numerics . BigInteger ( 5440514381186227205L ) ;
172+ num += dataoffset ;
173+ System . Numerics . BigInteger bigInteger2 = bigInteger * num ;
174+ long num2 = ( long ) ( bigInteger2 >> 70 ) ;
175+ long num3 = num2 >> 63 ;
176+ num2 += num3 ;
177+ num3 = num2 * 217 ;
178+ num -= num3 ;
179+ pk1 = ( int ) ( num & 0xFFFFFFFFu ) ;
180+ long num4 = ( uint ) ( ( long ) nkey * 11L ) ;
181+ num4 += dataoffset ;
182+ num2 = 0L ;
183+ num2 &= 0x3FF ;
184+ num4 += num2 ;
185+ num4 &= 0x3FF ;
186+ long num5 = num4 - num2 ;
187+ pk2 = ( int ) ( num5 & 0xFFFFFFFFu ) ;
188+ }
189189
190- private static byte GetXorByte ( byte tagb , ref AC7XorKey xorkey )
191- {
192- if ( xorkey == null )
193- {
194- return tagb ;
195- }
196- tagb = ( byte ) ( ( uint ) ( tagb ^ AC7FullKey [ xorkey . pk1 * 1024 + xorkey . pk2 ] ) ^ 0x77u ) ;
197- xorkey . pk1 ++ ;
198- xorkey . pk2 ++ ;
199- if ( xorkey . pk1 >= 217 )
200- {
201- xorkey . pk1 = 0 ;
202- }
203- if ( xorkey . pk2 >= 1024 )
204- {
205- xorkey . pk2 = 0 ;
206- }
207- return tagb ;
208- }
209- }
190+ private static byte GetXorByte ( byte tagb , ref AC7XorKey xorkey )
191+ {
192+ if ( xorkey == null )
193+ {
194+ return tagb ;
195+ }
196+ tagb = ( byte ) ( ( uint ) ( tagb ^ AC7FullKey [ xorkey . pk1 * 1024 + xorkey . pk2 ] ) ^ 0x77u ) ;
197+ xorkey . pk1 ++ ;
198+ xorkey . pk2 ++ ;
199+ if ( xorkey . pk1 >= 217 )
200+ {
201+ xorkey . pk1 = 0 ;
202+ }
203+ if ( xorkey . pk2 >= 1024 )
204+ {
205+ xorkey . pk2 = 0 ;
206+ }
207+ return tagb ;
208+ }
209+ }
210210}
0 commit comments