Created
September 28, 2014 15:28
-
-
Save misodengaku/0e2c00fce1e7082b95ea to your computer and use it in GitHub Desktop.
Commercial Application! writeup
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.Collections.Generic; | |
using System.IO; | |
using System.Linq; | |
using System.Security.Cryptography; | |
using System.Text; | |
using System.Threading.Tasks; | |
namespace commercial_app | |
{ | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
var cipher = StringToByteArray("29a002d9340fc4bd54492f327269f3e051619b889dc8da723e135ce486965d84"); | |
var key = StringToByteArray("37eaae0141f1a3adf8a1dee655853714"); | |
var iv = Encoding.UTF8.GetBytes("a5efdbd57b84ca36"); | |
var serial = DecryptDataAES(cipher, key, iv); | |
foreach(var d in Encoding.UTF8.GetBytes(serial)) | |
{ | |
Console.Write(Convert.ToString(d, 16)); | |
Console.Write(" "); | |
} | |
Console.WriteLine("\nEncode test"); | |
var encoded = EncryptDataAES(Encoding.UTF8.GetBytes(serial), key, iv); | |
foreach (var d in cipher) | |
{ | |
Console.Write(Convert.ToString(d, 16)); | |
} | |
Console.WriteLine(""); | |
foreach (var d in encoded) | |
{ | |
Console.Write(Convert.ToString(d, 16)); | |
} | |
Console.WriteLine(""); | |
Console.WriteLine(serial); | |
} | |
//この辺 | |
// http://stackoverflow.com/questions/10424334/encryption-in-net-and-decryption-in-android-throws-badpaddingexception-pad-blo | |
public static byte[] EncryptDataAES(byte[] toEncrypt, byte[] key, byte[] iv) | |
{ | |
byte[] encryptedData; | |
using (SymmetricAlgorithm aes = SymmetricAlgorithm.Create()) | |
{ | |
aes.Mode = CipherMode.CBC; | |
aes.BlockSize = 128; | |
aes.KeySize = 128; | |
aes.Key = key; | |
aes.IV = iv; | |
aes.Padding = PaddingMode.PKCS7; | |
ICryptoTransform encryptor = aes.CreateEncryptor(); | |
using (MemoryStream mStream = new MemoryStream()) | |
{ | |
using (CryptoStream cStream = new CryptoStream(mStream, encryptor, CryptoStreamMode.Write)) | |
{ | |
cStream.Write(toEncrypt, 0, toEncrypt.Length); | |
cStream.FlushFinalBlock(); | |
encryptedData = mStream.ToArray(); | |
} | |
} | |
} | |
return encryptedData; | |
} | |
public static string DecryptDataAES(byte[] cipherText, byte[] key, byte[] iv) | |
{ | |
string plaintext = null; | |
using (var aes = new AesManaged()) | |
{ | |
aes.BlockSize = 128; | |
aes.KeySize = 128; | |
aes.Padding = PaddingMode.PKCS7; | |
aes.Key = key; | |
aes.IV = iv; | |
ICryptoTransform decryptor = aes.CreateDecryptor(aes.Key, aes.IV); | |
// Create the streams used for decryption. | |
using (MemoryStream msDecrypt = new MemoryStream(cipherText)) | |
{ | |
using (CryptoStream csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read)) | |
{ | |
using (StreamReader srDecrypt = new StreamReader(csDecrypt)) | |
{ | |
plaintext = srDecrypt.ReadToEnd(); | |
} | |
} | |
} | |
} | |
return plaintext; | |
} | |
public static byte[] StringToByteArray(String hex) | |
{ | |
int NumberChars = hex.Length / 2; | |
byte[] bytes = new byte[NumberChars]; | |
using (var sr = new StringReader(hex)) | |
{ | |
for (int i = 0; i < NumberChars; i++) | |
bytes[i] = | |
Convert.ToByte(new string(new char[2] { (char)sr.Read(), (char)sr.Read() }), 16); | |
} | |
return bytes; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment