Skip to content

Commit

Permalink
feat: Add Triple DES crypter to store passwords
Browse files Browse the repository at this point in the history
  • Loading branch information
dreamcodestudio committed Oct 22, 2023
1 parent 60eb920 commit 72b9270
Show file tree
Hide file tree
Showing 9 changed files with 130 additions and 10 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# Changelog

## [1.2.0] - 2023-10-22
- Added Triple DES crypter to store passwords

## [1.1.0] - 2022-10-22
- MS coding style
- Improvements in core structure
Expand Down
27 changes: 18 additions & 9 deletions Editor/KeystoreSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,37 +10,46 @@ internal static class KeystoreSettings
public static string AliasName;
public static string AliasPassword;
private const string KeystoreExt = ".keystore";
private static readonly ICrypter _crypter = new TripleDESCrypter(nameof(KeystoreSettings));

public static void Load()
{
var prefsPassword = EditorPrefs.GetString(
$"{PlayerSettings.applicationIdentifier}-{nameof(KeystoreSettings)}-{nameof(Password)}");
var prefsAliasPassword =
EditorPrefs.GetString(
$"{PlayerSettings.applicationIdentifier}-{nameof(KeystoreSettings)}-{nameof(AliasPassword)}");
var decryptedPassword = _crypter.Decrypt(prefsPassword);
var decryptedAliasPassword = _crypter.Decrypt(prefsAliasPassword);

Name = EditorPrefs.GetString(
$"{PlayerSettings.applicationIdentifier}-{nameof(KeystoreSettings)}-{nameof(Name)}");
Password = EditorPrefs.GetString(
$"{PlayerSettings.applicationIdentifier}-{nameof(KeystoreSettings)}-{nameof(Password)}");
Password = decryptedPassword;
AliasName = EditorPrefs.GetString(
$"{PlayerSettings.applicationIdentifier}-{nameof(KeystoreSettings)}-{nameof(AliasName)}");
AliasPassword =
EditorPrefs.GetString(
$"{PlayerSettings.applicationIdentifier}-{nameof(KeystoreSettings)}-{nameof(AliasPassword)}");
AliasPassword = decryptedPassword;
if (EditorUserBuildSettings.activeBuildTarget != BuildTarget.Android)
return;
PlayerSettings.Android.keystoreName = Name + KeystoreExt;
PlayerSettings.Android.keystorePass = Password;
PlayerSettings.Android.keystorePass = decryptedPassword;
PlayerSettings.Android.keyaliasName = AliasName;
PlayerSettings.Android.keyaliasPass = AliasPassword;
PlayerSettings.Android.keyaliasPass = decryptedAliasPassword;
}

public static void Save(string name, string password, string aliasName, string aliasPassword)
{
var encryptedPassword = _crypter.Encrypt(password);
var encryptedAliasPassword = _crypter.Encrypt(aliasPassword);
EditorPrefs.SetString($"{PlayerSettings.applicationIdentifier}-{nameof(KeystoreSettings)}-{nameof(Name)}",
name);
EditorPrefs.SetString(
$"{PlayerSettings.applicationIdentifier}-{nameof(KeystoreSettings)}-{nameof(Password)}", password);
$"{PlayerSettings.applicationIdentifier}-{nameof(KeystoreSettings)}-{nameof(Password)}",
encryptedPassword);
EditorPrefs.SetString(
$"{PlayerSettings.applicationIdentifier}-{nameof(KeystoreSettings)}-{nameof(AliasName)}", aliasName);
EditorPrefs.SetString(
$"{PlayerSettings.applicationIdentifier}-{nameof(KeystoreSettings)}-{nameof(AliasPassword)}",
aliasPassword);
encryptedAliasPassword);
if (EditorUserBuildSettings.activeBuildTarget != BuildTarget.Android)
return;
PlayerSettings.Android.keystoreName = name + KeystoreExt;
Expand Down
8 changes: 8 additions & 0 deletions Editor/Security.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions Editor/Security/Common.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions Editor/Security/Common/ICrypter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace DreamCode.AutoKeystore.Editor
{
public interface ICrypter
{
string Encrypt(string value);
string Decrypt(string base64String);
}
}
11 changes: 11 additions & 0 deletions Editor/Security/Common/ICrypter.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

62 changes: 62 additions & 0 deletions Editor/Security/TripleDESCrypter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
using System;
using System.Security.Cryptography;
using System.Text;
using UnityEngine;

namespace DreamCode.AutoKeystore.Editor
{
public sealed class TripleDESCrypter : ICrypter
{
private readonly string _secretKey;

public TripleDESCrypter(string secretKey)
{
_secretKey = secretKey;
}

public string Encrypt(string value)
{
var inputBytes = Encoding.UTF8.GetBytes(value);
var md5ServiceProvider = new MD5CryptoServiceProvider();
var tripleServiceProvider = new TripleDESCryptoServiceProvider();

tripleServiceProvider.Key = md5ServiceProvider.ComputeHash(Encoding.UTF8.GetBytes(_secretKey));
tripleServiceProvider.Mode = CipherMode.ECB;
var cryptoTransform = tripleServiceProvider.CreateEncryptor();

var finalBlock = cryptoTransform.TransformFinalBlock(inputBytes, 0, inputBytes.Length);
return Convert.ToBase64String(finalBlock);
}

public string Decrypt(string base64String)
{
if (string.IsNullOrEmpty(base64String))
return string.Empty;
var result = string.Empty;
try
{
var inputBytes = Convert.FromBase64String(base64String);
var md5ServiceProvider = new MD5CryptoServiceProvider();
var tripleServiceProvider = new TripleDESCryptoServiceProvider();

tripleServiceProvider.Key = md5ServiceProvider.ComputeHash(Encoding.UTF8.GetBytes(_secretKey));
tripleServiceProvider.Mode = CipherMode.ECB;
var cryptoTransform = tripleServiceProvider.CreateDecryptor();

var finalBlock = cryptoTransform.TransformFinalBlock(inputBytes, 0, inputBytes.Length);
result = Encoding.UTF8.GetString(finalBlock);
}
catch (CryptographicException cryptoEx)
{
Debug.LogWarning($"{nameof(TripleDESCrypter)}-{cryptoEx.Message}");
result = base64String;
}
catch (Exception e)
{
Debug.LogError($"{nameof(TripleDESCrypter)}-{e.Message}");
}

return result;
}
}
}
11 changes: 11 additions & 0 deletions Editor/Security/TripleDESCrypter.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "com.dreamcode.mobile.android-keystore",
"displayName": "AutoKeystore",
"version": "1.1.0",
"version": "1.2.0",
"unity": "2020.3",
"description": "Allows store android keystore between sessions, without manual input everytime.",
"keywords": [
Expand Down

0 comments on commit 72b9270

Please sign in to comment.