Implement superior ProtectedDataForCurrentUser encryption

This commit is contained in:
JustArchi
2016-06-28 05:24:30 +02:00
parent d5514422b6
commit a4383cdb89
4 changed files with 52 additions and 3 deletions

View File

@@ -89,6 +89,7 @@
</Reference>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Security" />
<Reference Include="System.ServiceModel" />
<Reference Include="Microsoft.CSharp" />
<Reference Include="System.Net.Http" />

View File

@@ -548,7 +548,9 @@ namespace ArchiSteamFarm {
return "Can't encrypt null password!";
}
return CryptoHelper.ECryptoMethod.AES + "-encrypted password: " + CryptoHelper.Encrypt(CryptoHelper.ECryptoMethod.AES, BotConfig.SteamPassword);
return Environment.NewLine +
"[" + CryptoHelper.ECryptoMethod.AES + "] password: " + CryptoHelper.Encrypt(CryptoHelper.ECryptoMethod.AES, BotConfig.SteamPassword) + Environment.NewLine +
"[" + CryptoHelper.ECryptoMethod.ProtectedDataForCurrentUser + "] password: " + CryptoHelper.Encrypt(CryptoHelper.ECryptoMethod.ProtectedDataForCurrentUser, BotConfig.SteamPassword);
}
private static string ResponsePassword(ulong steamID, string botName) {

View File

@@ -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;
}
}
}
}

View File

@@ -37,7 +37,8 @@ namespace ConfigGenerator {
internal sealed class BotConfig : ASFConfig {
internal enum ECryptoMethod : byte {
PlainText,
AES
AES,
ProtectedDataForCurrentUser
}
[JsonProperty(Required = Required.DisallowNull)]