-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add Triple DES crypter to store passwords
- Loading branch information
dreamcodestudio
committed
Oct 22, 2023
1 parent
60eb920
commit 72b9270
Showing
9 changed files
with
130 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters