Created
December 1, 2013 13:03
-
-
Save satomacoto/7733315 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 UnityEngine; | |
using System.Collections; | |
using System; | |
using System.IO; | |
using System.Text; | |
using System.Security.Cryptography; | |
public class Encrypt : ScriptableObject | |
{ | |
public static byte[] EncryptBytes(byte[] message, byte[] rgbKey, byte[] rgbIV) | |
{ | |
if ((message == null) || (message.Length == 0)) | |
return null; | |
using (var stream = new MemoryStream()) | |
using (var encrypter = new RijndaelManaged().CreateEncryptor(rgbKey, rgbIV)) | |
using (var encrypt = new CryptoStream(stream, encrypter, CryptoStreamMode.Write)) | |
{ | |
encrypt.Write(message, 0, message.Length); | |
encrypt.FlushFinalBlock(); | |
return stream.ToArray(); | |
} | |
} | |
public static byte[] DecryptBytes(byte[] encrypted, byte[] rgbKey, byte[] rgbIV) | |
{ | |
if ((encrypted == null) || (encrypted.Length == 0)) | |
return null; | |
using (var stream = new MemoryStream()) | |
using (var decrypter = new RijndaelManaged().CreateDecryptor(rgbKey, rgbIV)) | |
using (var encrypt = new CryptoStream(stream, decrypter, CryptoStreamMode.Write)) | |
{ | |
encrypt.Write(encrypted, 0, encrypted.Length); | |
encrypt.FlushFinalBlock(); | |
return stream.ToArray(); | |
} | |
} | |
} |
This file contains hidden or 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
# -*- coding: utf-8 -*- | |
import base64 | |
import Crypto.Cipher.AES as AES | |
iv = base64.b64decode("zlW3vcM3s06ZC1uVJ/ZieQ==") | |
dec = AES.new("abcdefghijklmnopqrstuvwxyz012345", AES.MODE_CBC, iv) | |
print dec.decrypt(base64.b64decode("EUmkubX1UQ/z/R93L/JRRw==")) |
This file contains hidden or 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.Text; | |
using System.Security.Cryptography; | |
using NUnit.Framework; | |
namespace AssemblyCSharp | |
{ | |
[TestFixture()] | |
public class EncryptTest | |
{ | |
[Test()] | |
public void TestCase () | |
{ | |
string expected = "sample"; | |
string seed = "abcdefghijklmnopqrstuvwxyz012345"; | |
byte[] message = Encoding.UTF8.GetBytes(expected); | |
using (RijndaelManaged myRijndael = new RijndaelManaged()) | |
{ | |
myRijndael.Key = Encoding.UTF8.GetBytes(seed); | |
myRijndael.GenerateIV(); | |
Console.WriteLine(Convert.ToBase64String(myRijndael.IV)); | |
Console.WriteLine(myRijndael.Mode); | |
byte[] encrypted = Setting.EncryptBytes(message, myRijndael.Key, myRijndael.IV); | |
byte[] decrytped = Setting.DecryptBytes(encrypted, myRijndael.Key, myRijndael.IV); | |
var actual = Encoding.UTF8.GetString(decrytped); | |
Console.WriteLine(Convert.ToBase64String(encrypted)); | |
Assert.AreEqual(expected, actual); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment