@@ -10,109 +10,43 @@ namespace Bcat_Manager
1010{
1111 public class AesCrypto
1212 {
13- public static byte [ ] EncryptAesBlock ( byte [ ] Block , byte [ ] key )
14- {
15- byte [ ] decryptedBuffer = new byte [ 0x10 ] ;
16-
17- Aes aes = Aes . Create ( ) ;
18- aes . Mode = CipherMode . ECB ;
19- aes . BlockSize = 128 ;
20- aes . KeySize = key . Length * 8 ;
21- aes . Key = key ;
22-
23- int transed = 0 ;
24- using ( var transform = aes . CreateEncryptor ( ) )
25- {
26- for ( int i = 0 ; i < 0x10 ; i += transed )
27- {
28- transed = transform . TransformBlock ( Block , i , 0x10 , decryptedBuffer , i ) ;
29- }
30- }
31-
32- return decryptedBuffer ;
33- }
34- //todo : remove or update
35- public static void EncryptAesBlock ( Stream inStream , Stream ouStream , byte [ ] key )
13+ public static byte [ ] PerformAesCTR ( byte [ ] SourceBuffer , int Offset , int Size , byte [ ] key , byte [ ] iv )
3614 {
37- BinaryReader br = new BinaryReader ( inStream ) ;
38- BinaryWriter bw = new BinaryWriter ( ouStream ) ;
39-
40- byte [ ] decryptedBuffer = new byte [ 0x10 ] ;
15+ byte [ ] encCounter = new byte [ 0x10 ] ;
16+ byte [ ] decryptedBuffer = new byte [ Size ] ;
17+ byte [ ] ctr = iv ;
4118
4219 Aes aes = Aes . Create ( ) ;
4320 aes . Mode = CipherMode . ECB ;
4421 aes . BlockSize = 128 ;
4522 aes . KeySize = key . Length * 8 ;
4623 aes . Key = key ;
4724
48- int transed = 0 ;
49- using ( var transform = aes . CreateEncryptor ( ) )
50- {
51- byte [ ] inBlock = br . ReadBytes ( 0x10 ) ;
52- byte [ ] outBlock = new byte [ 0x10 ] ;
53-
54- for ( int i = 0 ; i < 0x10 ; i += transed )
55- {
56- transed = transform . TransformBlock ( inBlock , i , 0x10 , outBlock , i ) ;
57- }
58-
59- bw . Write ( outBlock ) ;
60- }
61- }
62-
63- //todo : optimise the code to be faster
64- public static void PerformAesCtr ( Stream inStream , Stream ouStream , int Size , byte [ ] key , byte [ ] iv )
65- {
66- BinaryReader br = new BinaryReader ( inStream ) ;
67- BinaryWriter bw = new BinaryWriter ( ouStream ) ;
68-
69- for ( var i = inStream . Position ; i < Size ; i += 0x10 )
70- {
71- byte [ ] encCounter = EncryptAesBlock ( iv , key ) ;
72-
73- var rest = Size - i ;
74- if ( rest > 0x10 ) rest = 0x10 ;
75-
76- //xor encrypted counter with ciphertext
77- for ( int j = 0 ; j < rest ; j ++ )
78- {
79- bw . BaseStream . Position = i + j ;
80- br . BaseStream . Position = i + j ;
81- bw . Write ( br . ReadByte ( ) ^ encCounter [ j ] ) ;
82- }
83-
84- //increment counter
85- for ( var j = iv . Length - 1 ; j >= 0 ; j -- )
86- {
87- if ( ++ iv [ j ] != 0 )
88- break ;
89- }
90- }
91- }
92- public static byte [ ] PerformAesCTR ( byte [ ] SourceBuffer , int Offset , int Size , byte [ ] key , byte [ ] iv )
93- {
94- byte [ ] decryptedBuffer = new byte [ Size ] ;
95-
96- byte [ ] counter = iv ;
97-
9825 for ( int i = Offset ; i < Size ; i += 0x10 )
9926 {
100- byte [ ] encCounter = EncryptAesBlock ( counter , key ) ;
101-
102- int rest = Size - i ;
103- if ( rest > 0x10 ) rest = 0x10 ;
104-
105- //xor encrypted counter with ciphertext
106- for ( int j = 0 ; j < rest ; j ++ )
27+ using ( var transform = aes . CreateEncryptor ( ) )
10728 {
108- decryptedBuffer [ i + j ] = ( byte ) ( encCounter [ j ] ^ SourceBuffer [ i + j ] ) ;
109- }
11029
111- //increment counter
112- for ( var j = counter . Length - 1 ; j >= 0 ; j -- )
113- {
114- if ( ++ counter [ j ] != 0 )
115- break ;
30+ int transed = 0 ;
31+ for ( int j = 0 ; j < 0x10 ; j += transed )
32+ {
33+ transed = transform . TransformBlock ( ctr , j , 0x10 , encCounter , j ) ;
34+ }
35+
36+ int rest = Size - i ;
37+ if ( rest > 0x10 ) rest = 0x10 ;
38+
39+ //xor encrypted counter with ciphertext
40+ for ( int j = 0 ; j < rest ; j ++ )
41+ {
42+ decryptedBuffer [ i + j ] = ( byte ) ( encCounter [ j ] ^ SourceBuffer [ i + j ] ) ;
43+ }
44+ //increment counter
45+ for ( var j = ctr . Length - 1 ; j >= 0 ; j -- )
46+ {
47+ if ( ++ ctr [ j ] != 0 )
48+ break ;
49+ }
11650 }
11751 }
11852
0 commit comments