22using System . IO ;
33using System . Security . Cryptography ;
44using System . Text ;
5+ using GeneralUpdate . Core . Ipc ;
56
67namespace GeneralUpdate . Core . Configuration ;
78
89/// <summary>
910/// Secure IPC environment variable provider.
1011/// AES-encrypted temp files in a dedicated subdirectory, auto-deleted after read.
12+ /// Encryption is delegated to <see cref="IpcEncryption"/>.
1113/// </summary>
1214public static class Environments
1315{
14- // Fixed key/IV derived from a constant — not crypto-grade, but sufficient for
15- // ephemeral IPC where the file lives < 1 second and is in a per-user directory.
1616 private static readonly byte [ ] _aesKey = SHA256 . Create ( )
1717 . ComputeHash ( Encoding . UTF8 . GetBytes ( "GeneralUpdate.IPC.EnvironmentProvider.v1" ) ) ;
1818 private static readonly byte [ ] _aesIV = new byte [ 16 ] { 0x47 , 0x55 , 0x50 , 0x44 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 } ;
@@ -31,33 +31,13 @@ public static void SetEnvironmentVariable(string key, string value)
3131 {
3232 var filePath = Path . Combine ( IpcDir , $ "{ key } .enc") ;
3333 var plainBytes = Encoding . UTF8 . GetBytes ( value ) ;
34- using var aes = Aes . Create ( ) ;
35- aes . Key = _aesKey ;
36- aes . IV = _aesIV ;
37- using var encryptor = aes . CreateEncryptor ( ) ;
38- var encrypted = encryptor . TransformFinalBlock ( plainBytes , 0 , plainBytes . Length ) ;
39- File . WriteAllBytes ( filePath , encrypted ) ;
34+ IpcEncryption . EncryptToFile ( plainBytes , filePath , _aesKey , _aesIV ) ;
4035 }
4136
4237 public static string GetEnvironmentVariable ( string key )
4338 {
4439 var filePath = Path . Combine ( Path . GetTempPath ( ) , "GeneralUpdate" , "ipc" , $ "{ key } .enc") ;
45- if ( ! File . Exists ( filePath ) )
46- return string . Empty ;
47-
48- try
49- {
50- var encrypted = File . ReadAllBytes ( filePath ) ;
51- using var aes = Aes . Create ( ) ;
52- aes . Key = _aesKey ;
53- aes . IV = _aesIV ;
54- using var decryptor = aes . CreateDecryptor ( ) ;
55- var plainBytes = decryptor . TransformFinalBlock ( encrypted , 0 , encrypted . Length ) ;
56- return Encoding . UTF8 . GetString ( plainBytes ) ;
57- }
58- finally
59- {
60- try { File . Delete ( filePath ) ; } catch { /* best-effort cleanup */ }
61- }
40+ var plainBytes = IpcEncryption . DecryptFromFile ( filePath , _aesKey , _aesIV ) ;
41+ return plainBytes != null ? Encoding . UTF8 . GetString ( plainBytes ) : string . Empty ;
6242 }
6343}
0 commit comments