Last active
May 21, 2020 19:08
-
-
Save meto4d/7f6e0907be87152d6fab3eea2fd92677 to your computer and use it in GitHub Desktop.
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
using System; | |
using System.IO; | |
using System.Text; | |
using System.Linq; | |
using System.Security.Cryptography; | |
public class OpenTweenEncrypt{ | |
public static void Main(){ | |
string RawTokenSecret = ""; | |
string EncryptTokenSecret = EncryptString(RawTokenSecret); | |
Console.WriteLine(EncryptTokenSecret); | |
} | |
public static string EncryptString(string str) | |
{ | |
if (str.Length == 0) return ""; | |
//文字列をバイト型配列にする | |
var bytesIn = Encoding.UTF8.GetBytes(str); | |
//DESCryptoServiceProviderオブジェクトの作成 | |
var des = new DESCryptoServiceProvider(); | |
//共有キーと初期化ベクタを決定 | |
//パスワードをバイト配列にする | |
var bytesKey = Encoding.UTF8.GetBytes("_tween_encrypt_key_"); | |
//共有キーと初期化ベクタを設定 | |
des.Key = ResizeBytesArray(bytesKey, des.Key.Length); | |
des.IV = ResizeBytesArray(bytesKey, des.IV.Length); | |
MemoryStream msOut; | |
ICryptoTransform desdecrypt; | |
try | |
{ | |
//暗号化されたデータを書き出すためのMemoryStream | |
msOut = new MemoryStream(); | |
//DES暗号化オブジェクトの作成 | |
desdecrypt = des.CreateEncryptor(); | |
//書き込むためのCryptoStreamの作成 | |
var cryptStream = new CryptoStream(msOut, desdecrypt, CryptoStreamMode.Write); | |
//Disposeが重複して呼ばれないようにする | |
var msTmp = msOut; | |
msOut = null; | |
desdecrypt = null; | |
//書き込む | |
cryptStream.Write(bytesIn, 0, bytesIn.Length); | |
cryptStream.FlushFinalBlock(); | |
//暗号化されたデータを取得 | |
var bytesOut = msTmp.ToArray(); | |
//Base64で文字列に変更して結果を返す | |
return Convert.ToBase64String(bytesOut); | |
} finally { | |
} | |
} | |
public static string DecryptString(string str) | |
{ | |
if (str.Length == 0) return ""; | |
//DESCryptoServiceProviderオブジェクトの作成 | |
var des = new DESCryptoServiceProvider(); | |
//共有キーと初期化ベクタを決定 | |
//パスワードをバイト配列にする | |
var bytesKey = Encoding.UTF8.GetBytes("_tween_encrypt_key_"); | |
//共有キーと初期化ベクタを設定 | |
des.Key = ResizeBytesArray(bytesKey, des.Key.Length); | |
des.IV = ResizeBytesArray(bytesKey, des.IV.Length); | |
//Base64で文字列をバイト配列に戻す | |
var bytesIn = Convert.FromBase64String(str); | |
MemoryStream msIn; | |
ICryptoTransform desdecrypt; | |
CryptoStream cryptStreem; | |
try | |
{ | |
//暗号化されたデータを読み込むためのMemoryStream | |
msIn = new MemoryStream(bytesIn); | |
//DES復号化オブジェクトの作成 | |
desdecrypt = des.CreateDecryptor(); | |
//読み込むためのCryptoStreamの作成 | |
cryptStreem = new CryptoStream(msIn, desdecrypt, CryptoStreamMode.Read); | |
//Disposeが重複して呼ばれないようにする | |
msIn = null; | |
desdecrypt = null; | |
//復号化されたデータを取得するためのStreamReader | |
var srOut = new StreamReader(cryptStreem, Encoding.UTF8); | |
//Disposeが重複して呼ばれないようにする | |
cryptStreem = null; | |
//復号化されたデータを取得する | |
var result = srOut.ReadToEnd(); | |
return result; | |
} | |
finally | |
{ | |
} | |
} | |
public static byte[] ResizeBytesArray(byte[] bytes, int newSize) | |
{ | |
var newBytes = new byte[newSize]; | |
if (bytes.Length <= newSize) | |
{ | |
foreach (var i in Enumerable.Range(0, bytes.Length)) | |
{ | |
newBytes[i] = bytes[i]; | |
} | |
} | |
else | |
{ | |
var pos = 0; | |
foreach (var i in Enumerable.Range(0, bytes.Length)) | |
{ | |
newBytes[pos] = unchecked((byte)(newBytes[pos] ^ bytes[i])); | |
pos++; | |
if (pos >= newBytes.Length) | |
{ | |
pos = 0; | |
} | |
} | |
} | |
return newBytes; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment