55
66namespace YandexKeyExtractor ;
77
8- public static class Decryptor
8+ internal static class Decryptor
99{
10+ private const int maxStackallocSize = 4096 ;
11+
1012 public static string ? Decrypt ( string encryptedText , string password )
1113 {
1214 var base64Text = NormalizeBase64 ( encryptedText ) ;
1315
14- ReadOnlySpan < byte > textBytes = Convert . FromBase64String ( base64Text ) . AsSpan ( ) ;
16+ var textBytes = Convert . FromBase64String ( base64Text ) ;
1517
1618 const byte SaltLength = 16 ;
17- var textData = textBytes [ ..^ SaltLength ] ;
18- var textSalt = textBytes [ ^ SaltLength ..] ;
19+ var textData = textBytes . AsSpan ( ) [ ..^ SaltLength ] ;
20+ var salt = textBytes [ ^ SaltLength ..] ;
1921
2022 var generatedPassword = SCrypt . ComputeDerivedKey (
21- Encoding . UTF8 . GetBytes ( password ) , textSalt . ToArray ( ) , 32768 , 20 , 1 , null , 32
23+ Encoding . UTF8 . GetBytes ( password ) ,
24+ salt ,
25+ cost : 32768 ,
26+ blockSize : 20 ,
27+ parallel : 1 ,
28+ maxThreads : null ,
29+ derivedKeyLength : 32
2230 ) ;
2331
2432 using XSalsa20Poly1305 secureBox = new ( generatedPassword ) ;
@@ -27,8 +35,9 @@ public static class Decryptor
2735 var nonce = textData [ ..NonceLength ] ;
2836 var dataWithMac = textData [ NonceLength ..] ;
2937
30-
31- var message = dataWithMac . Length <= 4096 ? stackalloc byte [ dataWithMac . Length ] : new byte [ dataWithMac . Length ] ;
38+ var message = dataWithMac . Length <= maxStackallocSize
39+ ? stackalloc byte [ dataWithMac . Length ]
40+ : new byte [ dataWithMac . Length ] ;
3241
3342 const byte MacLength = 16 ;
3443 var data = dataWithMac [ MacLength ..] ;
@@ -41,11 +50,27 @@ public static class Decryptor
4150
4251 private static string NormalizeBase64 ( string encryptedText )
4352 {
44- return encryptedText . Replace ( '-' , '+' ) . Replace ( '_' , '/' ) + ( encryptedText . Length % 4 ) switch
53+ var suffixLength = ( encryptedText . Length % 4 ) switch
4554 {
46- 2 => "==" ,
47- 3 => "=" ,
48- _ => ""
55+ 2 => 2 ,
56+ 3 => 1 ,
57+ _ => 0
4958 } ;
59+
60+ var newLength = encryptedText . Length + suffixLength ;
61+ var normalized = newLength <= maxStackallocSize / sizeof ( char )
62+ ? stackalloc char [ newLength ]
63+ : new char [ newLength ] ;
64+
65+ encryptedText . CopyTo ( normalized ) ;
66+ normalized . Replace ( '-' , '+' ) ;
67+ normalized . Replace ( '_' , '/' ) ;
68+
69+ if ( suffixLength > 0 )
70+ {
71+ normalized [ ^ suffixLength ..] . Fill ( '=' ) ;
72+ }
73+
74+ return new string ( normalized ) ;
5075 }
5176}
0 commit comments