for Push.
This commit is contained in:
@@ -0,0 +1,96 @@
|
||||
using DocumentFormat.OpenXml.Wordprocessing;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Security.Cryptography;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace KssSmaPlaLib.Encryption
|
||||
{
|
||||
public static class SimpleEncryptUxer
|
||||
{
|
||||
public static readonly string FixedPassword = "KssSmaPlaDxer!";
|
||||
private static readonly byte[] FixedIV = new byte[16];
|
||||
|
||||
public static bool TuckIntoMain(string[] args)
|
||||
{
|
||||
if (args.Length != 2) return false;
|
||||
|
||||
if (args[0].ToLower() == "/enc")
|
||||
{
|
||||
if (SimpleEncryptUxer.Encrypt(args[1], out var resultText, out var resultMessage))
|
||||
{
|
||||
Console.WriteLine($"暗号化結果[{resultText}]");
|
||||
}
|
||||
else
|
||||
{
|
||||
Console.WriteLine(resultMessage);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
else if (args[0].ToLower() == "/dec")
|
||||
{
|
||||
if (SimpleEncryptUxer.Decrypt(args[1], out var resultText, out var resultMessage))
|
||||
{
|
||||
Console.WriteLine($"複合化結果[{resultText}]");
|
||||
}
|
||||
else
|
||||
{
|
||||
Console.WriteLine(resultMessage);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public static bool Encrypt(string plainText, out string resultText, out string resultMessage, string password = "")
|
||||
{
|
||||
resultText = string.Empty;
|
||||
resultMessage = string.Empty;
|
||||
password = string.IsNullOrEmpty(password) ? FixedPassword : password;
|
||||
try
|
||||
{
|
||||
byte[] key = SHA256.Create().ComputeHash(Encoding.UTF8.GetBytes(password));
|
||||
// 固定IV(簡易仕様とする)※IVはオールゼロ
|
||||
using (var aes = new AesManaged { Key = key, IV = FixedIV })
|
||||
using (var encryptor = aes.CreateEncryptor())
|
||||
{
|
||||
byte[] inputBytes = Encoding.UTF8.GetBytes(plainText);
|
||||
byte[] encryptedBytes = encryptor.TransformFinalBlock(inputBytes, 0, inputBytes.Length);
|
||||
resultText = Convert.ToBase64String(encryptedBytes);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
resultMessage = $"暗号化に失敗しました。[エラー内容:{ex.Message}]";
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public static bool Decrypt(string encryptedText, out string resultText, out string resultMessage, string password = "")
|
||||
{
|
||||
resultText = string.Empty;
|
||||
resultMessage = string.Empty;
|
||||
password = string.IsNullOrEmpty(password) ? FixedPassword : password;
|
||||
try
|
||||
{
|
||||
byte[] key = SHA256.Create().ComputeHash(Encoding.UTF8.GetBytes(password));
|
||||
byte[] encryptedBytes = Convert.FromBase64String(encryptedText);
|
||||
using (var aes = new AesManaged { Key = key, IV = FixedIV })
|
||||
using (var decryptor = aes.CreateDecryptor())
|
||||
{
|
||||
byte[] decryptedBytes = decryptor.TransformFinalBlock(encryptedBytes, 0, encryptedBytes.Length);
|
||||
resultText = Encoding.UTF8.GetString(decryptedBytes);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
resultMessage = $"複合化に失敗しました。[エラー内容:{ex.Message}]";
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user