mirror of
https://github.com/JustArchiNET/ArchiSteamFarm.git
synced 2026-01-16 08:25:28 +00:00
Implement superior ProtectedDataForCurrentUser encryption
This commit is contained in:
@@ -30,7 +30,8 @@ namespace ArchiSteamFarm {
|
||||
internal static class CryptoHelper {
|
||||
internal enum ECryptoMethod : byte {
|
||||
PlainText,
|
||||
AES
|
||||
AES,
|
||||
ProtectedDataForCurrentUser
|
||||
}
|
||||
|
||||
private static readonly byte[] EncryptionKey = Encoding.UTF8.GetBytes("ArchiSteamFarm");
|
||||
@@ -46,6 +47,8 @@ namespace ArchiSteamFarm {
|
||||
return decrypted;
|
||||
case ECryptoMethod.AES:
|
||||
return EncryptAES(decrypted);
|
||||
case ECryptoMethod.ProtectedDataForCurrentUser:
|
||||
return EncryptProtectedDataForCurrentUser(decrypted);
|
||||
default:
|
||||
return null;
|
||||
}
|
||||
@@ -62,6 +65,8 @@ namespace ArchiSteamFarm {
|
||||
return encrypted;
|
||||
case ECryptoMethod.AES:
|
||||
return DecryptAES(encrypted);
|
||||
case ECryptoMethod.ProtectedDataForCurrentUser:
|
||||
return DecryptProtectedDataForCurrentUser(encrypted);
|
||||
default:
|
||||
return null;
|
||||
}
|
||||
@@ -108,5 +113,45 @@ namespace ArchiSteamFarm {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
private static string EncryptProtectedDataForCurrentUser(string decrypted) {
|
||||
if (string.IsNullOrEmpty(decrypted)) {
|
||||
Logging.LogNullError(nameof(decrypted));
|
||||
return null;
|
||||
}
|
||||
|
||||
try {
|
||||
byte[] encryptedData = ProtectedData.Protect(
|
||||
Encoding.UTF8.GetBytes(decrypted),
|
||||
EncryptionKey, // This is used as salt only
|
||||
DataProtectionScope.CurrentUser
|
||||
);
|
||||
|
||||
return Convert.ToBase64String(encryptedData);
|
||||
} catch (Exception e) {
|
||||
Logging.LogGenericException(e);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
private static string DecryptProtectedDataForCurrentUser(string encrypted) {
|
||||
if (string.IsNullOrEmpty(encrypted)) {
|
||||
Logging.LogNullError(nameof(encrypted));
|
||||
return null;
|
||||
}
|
||||
|
||||
try {
|
||||
byte[] decryptedData = ProtectedData.Unprotect(
|
||||
Convert.FromBase64String(encrypted),
|
||||
EncryptionKey, // This is used as salt only
|
||||
DataProtectionScope.CurrentUser
|
||||
);
|
||||
|
||||
return Encoding.UTF8.GetString(decryptedData);
|
||||
} catch (Exception e) {
|
||||
Logging.LogGenericException(e);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user