Example #1
0
    public static void luajitEncode(string path)
    {
        string basePath = Application.dataPath;

        path = path.Replace("Assets/", "/");
        string dir     = Path.GetDirectoryName(path);
        string fname   = Path.GetFileNameWithoutExtension(path);
        string fext    = Path.GetExtension(path);
        string outPath = basePath + dir + "/" + fname + fext;

        outPath = outPath.Replace("/upgradeRes4Dev", "/upgradeRes4Publish");
        Directory.CreateDirectory(Path.GetDirectoryName(outPath));
                #if UNITY_IOS
        byte[] bytes = XXTEA.Encrypt(File.ReadAllBytes(basePath + path), XXTEA.defaultKey);
        File.WriteAllBytes(outPath, bytes);
                #elif UNITY_ANDROID
        //		string exeName = "/usr/local/bin/luajit";
        string exeName = "/usr/local/bin/luajit-2.0.4";
        //		string exeName = "/usr/local/bin/luajit-2.0.3";
        //		string exeName = "/usr/local/bin/luajit-2.1.0-alpha";
        if (!File.Exists(exeName))
        {
            UnityEngine.Debug.LogError("Ecode lua failed. file not fou" + exeName);
            return;
        }
        string arguments = "-b " + basePath + path + " " + outPath;
        Process.Start(exeName, arguments);
        //#else
        //		byte[] bytes = XXTEA.Encrypt(File.ReadAllBytes(basePath + path), XXTEA.defaultKey);
        //		File.WriteAllBytes(outPath, bytes);
                #endif
    }
Example #2
0
        public static StorageMetadata CreateStorageFile(Stream dataStream, uint archiveNumber, ulong?profileKey)
        {
            StorageMetadata metadata = new StorageMetadata();

            metadata.ArchiveNumber = archiveNumber + 2;

            byte[] data = null;
            using (BinaryReader reader = new BinaryReader(dataStream))
            {
                data = reader.ReadBytes((int)reader.BaseStream.Length);
            }

            Array.Resize(ref data, data.Length + 1); // 1 byte null padding

            // Generate Spooky hash of data for later
            SpookyHash sh = new SpookyHash(0x155af93ac304200, 0x8ac7230489e7ffff);

            sh.Update(new SHA256Managed().ComputeHash(data));
            sh.Update(data);
            sh.Final(out metadata.Key[0], out metadata.Key[1]);

            var compressedData = Compress(data);
            var key            = GenerateKey(metadata, profileKey, true);

            metadata.StorageRaw       = XXTEA.Encrypt(compressedData, key);
            metadata.CompressedSize   = (uint)compressedData.Length;
            metadata.DecompressedSize = (uint)data.Length;

            return(metadata);
        }
Example #3
0
    static void Encode()
    {
        string url   = Application.dataPath + "/txt.bytes";
        var    bytes = XXTEA.Encrypt(File.ReadAllBytes(url));

        File.WriteAllBytes(url.Replace("txt.bytes", "txt-encode.bytes"), bytes);
    }
Example #4
0
        private void btnEncrypt_Click(object sender, EventArgs e)
        {
            var sd = openFileDialog1.ShowDialog();

            if (sd == DialogResult.OK)
            {
                String key = "1234567890";
                using (var fs = openFileDialog1.OpenFile())
                {
                    byte[] buffer = new byte[fs.Length];
                    fs.Read(buffer, 0, buffer.Length);
                    var    data  = XXTEA.Encrypt(buffer, key);
                    var    file  = new FileInfo(openFileDialog1.FileName);
                    string fname = Path.Combine(file.DirectoryName,
                                                Path.GetFileNameWithoutExtension(openFileDialog1.FileName) + "_secure" +
                                                Path.GetExtension(openFileDialog1.FileName));
                    if (File.Exists(fname))
                    {
                        File.Delete(fname);
                    }
                    using (var fs2 = File.Create(fname))
                    {
                        fs2.Write(data, 0, data.Length);
                    }
                }
            }
        }
Example #5
0
 private Byte[] Encrypt(Byte[] data, Byte level, Byte encryptMode)
 {
     if (key != null && encryptMode >= level)
     {
         data = XXTEA.Encrypt(data, key);
     }
     return(data);
 }
Example #6
0
        internal static byte[] RsaEncryptBase(MemoryStream stream, RSACryptoServiceProvider rsa, byte[] encryptKey, HashAlgorithm hashAlgorithm)
        {
            var signData = rsa.SignData(stream.ToArray(), hashAlgorithm);

            stream.Write(signData, 0, signData.Length);
            stream.WriteByte(Convert.ToByte(signData.Length));
            return(XXTEA.Encrypt(stream.ToArray(), encryptKey));
        }
Example #7
0
 private Byte[] EncryptString(Byte[] data, Byte level)
 {
     if (encryptMode >= level)
     {
         data = XXTEA.Encrypt(data, key);
     }
     return(data);
 }
Example #8
0
 private void makeExportFile(Stream s, string data)
 {
     byte[] buff = Encoding.UTF8.GetBytes(data);
     byte[] md5  = Helper.GetMD5(buff);
     buff = XXTEA.Encrypt(buff, KEY_CODE);
     s.Write(FILE_MARK, 0, FILE_MARK.Length);
     s.Write(md5, 0, md5.Length);
     s.Write(buff, 0, buff.Length);
     s.Close();
 }
        public IActionResult Indexfile(IFormFile File, string key)

        {
            fileclass  op  = new fileclass();
            Encryption op2 = new Encryption();

            if (File == null || File.Length == 0)
            {
                return(Content("file not selected"));
            }
            //get path
            string path_Root     = _hosting.WebRootPath;
            string path_to_files = path_Root + "\\files\\" + File.FileName;

            //copyfiles
            using (var stream = new FileStream(path_to_files, FileMode.Create))
            {
                File.CopyTo(stream);
            }



            // read file
            FileStream fileStream = new FileStream(path_to_files, FileMode.Open);

            using (StreamReader reader = new StreamReader(fileStream))
            {
                // string line = reader.ReadLine();
                string line = reader.ReadToEnd();
                //  op.Encryptfile = line;
                string str = XXTEA.Encrypt(line, key);
                op.Encryptfile = op2.convert_to_RNA(op2.convert_to_dna(op2.split_binary(op2.StringToBinary(str))));
            }
            downloadtext = op.Encryptfile;
            //delete file
            FileInfo fi = new FileInfo(path_to_files);

            if (fi != null)
            {
                System.IO.File.Delete(path_to_files);
                fi.Delete();
            }
            ///////////////////
            //create file or put Encrypt message in file
            //path which file put in
            string docPath = path_Root + "\\files\\";

            // Write the specified text asynchronously to a new file named "WriteTextAsync.txt".
            using (StreamWriter outputFile = new StreamWriter(Path.Combine(docPath, "Encryptmessage.txt")))
            {
                outputFile.Write(downloadtext);
            }

            return(View("Indexfile", op));
        }
Example #10
0
        internal static byte[] Encrypt(MemoryStream stream, byte[] validateKey, byte[] encryptKey, HashAlgorithm hashAlgorithm)
        {
            var length = stream.Length;

            stream.Write(validateKey, 0, validateKey.Length);
            var hash = hashAlgorithm.ComputeHash(stream.ToArray());

            stream.SetLength(length);
            stream.Write(hash, 0, hash.Length);
            return(XXTEA.Encrypt(stream.ToArray(), encryptKey));
        }
Example #11
0
 internal static byte[] Encrypt(byte[] data, byte[] validateKey, byte[] encryptKey, HashAlgorithm hashAlgorithm)
 {
     using (var stream = new MemoryStream()) {
         var length = data.Length;
         stream.Write(data, 0, length);
         stream.Write(validateKey, 0, validateKey.Length);
         var hash = hashAlgorithm.ComputeHash(stream.ToArray());
         stream.SetLength(length);
         stream.Write(hash, 0, hash.Length);
         return(XXTEA.Encrypt(stream.ToArray(), encryptKey));
     }
 }
Example #12
0
        protected override byte[] SerializeMessage(IScsMessage message)
        {
            var bytes = base.SerializeMessage(message);

            if (compress)
            {
                bytes = CompressionManager.CompressGZip(bytes);
            }
            if (encrypt)
            {
                bytes = XXTEA.Encrypt(bytes, keys);
            }

            return(bytes);
        }
Example #13
0
 private void SendOutput()
 {
     if (encryptMode >= 3)
     {
         buffer.Append("phprpc_output=\"");
         buffer.Append(EncodeString(XXTEA.Encrypt(encoding.GetBytes(output), key)));
         buffer.Append("\";\r\n");
     }
     else
     {
         buffer.Append("phprpc_output=\"");
         buffer.Append(EncodeString(output));
         buffer.Append("\";\r\n");
     }
 }
Example #14
0
    public static void xxteaEncode(string path)
    {
        string basePath = Application.dataPath;

        path = path.Replace("Assets/", "/");
        string dir     = Path.GetDirectoryName(path);
        string fname   = Path.GetFileNameWithoutExtension(path);
        string fext    = Path.GetExtension(path);
        string outPath = basePath + dir + "/" + fname + fext;

        outPath = outPath.Replace("/upgradeRes4Dev", "/upgradeRes4Publish");
        Directory.CreateDirectory(Path.GetDirectoryName(outPath));
        byte[] bytes = XXTEA.Encrypt(System.Text.Encoding.UTF8.GetBytes(File.ReadAllText(basePath + path)), XXTEA.defaultKey);
        File.WriteAllBytes(outPath, bytes);
    }
Example #15
0
            private void SolveLoginPuzzle(uint[] v, uint[] k, byte unknown)
            {
                for (uint guess = 0; guess < uint.MaxValue; guess++)
                {
                    k[3] = guess;
                    uint[] vClone = (uint[])v.Clone();
                    XXTEA.Encrypt(vClone, k, 6);
                    uint solution = vClone[1];

                    if (solution >> (32 - unknown) == 0 && (solution & (0x80000000 >> unknown)) != 0)
                    {
                        return;
                    }
                }
                throw new Exception("Failed to solve login puzzle");
            }
        static void TeaTest()
        {
            var    key       = Encoding.UTF8.GetBytes("288A339E65244CC995154A686C651B1E");
            var    encrypted = XXTEA.Encrypt(key, Encoding.UTF8.GetBytes(original));
            string roundtrip = Encoding.UTF8.GetString(XXTEA.Decrypt(key, encrypted));
            string hex       = Epic.Converters.HexString.Encode(encrypted);
            string base64    = Convert.ToBase64String(encrypted);

            //Display the original data and the decrypted data.
            Console.WriteLine($"Tea:");
            Console.WriteLine("Key:   {0}", key.Length);
            Console.WriteLine("Byte:   {0}", encrypted.Length);
            Console.WriteLine("Hex:   {0}, {1}", hex.Length, hex);
            Console.WriteLine("Base64:   {0}, {1}", base64.Length, base64);
            Console.WriteLine("Decode: {0}", roundtrip);
            Console.WriteLine();
        }
Example #17
0
        private void SaveLogonName(string loginName)
        {
            AppConfigs ac = new AppConfigs();

            //如果在appconfig中还不存在该用户,则保存该用户登陆名
            if (!ac.valueExistInAppSettings(loginName))
            {
                ac.addAppSettings("user", loginName);
                //如果选择了记住密码,则保存对应密码
                if (chb_remberPassword.IsChecked.Value)
                {
                    System.Text.Encoding encoder = System.Text.Encoding.UTF8;
                    Byte[] data     = XXTEA.Encrypt(encoder.GetBytes(pwdPassword.Password), encoder.GetBytes("1234567890abcdef"));
                    string password = System.Convert.ToBase64String(data);
                    ac.addAppSettings(loginName, password);
                }
            }
            //如果该用户已经存在,则判断是否选择保存密码
            else
            {
                if (chb_remberPassword.IsChecked.Value)
                {
                    //如果保存密码,则察看该用户密码是否已经存在,如果不存在则保存密码
                    System.Text.Encoding encoder = System.Text.Encoding.UTF8;
                    Byte[] data     = XXTEA.Encrypt(encoder.GetBytes(pwdPassword.Password), encoder.GetBytes("1234567890abcdef"));
                    string password = System.Convert.ToBase64String(data);
                    if (!ac.keyExistInAppSettings(loginName))
                    {
                        ac.addAppSettings(loginName, password);
                    }
                    else
                    {
                        ac.updateAppSettings(loginName, password);
                    }
                }
                else
                {
                    //如果不保存密码,则察看该用户密码是否已经存在,如果存在则删除该密码
                    if (ac.keyExistInAppSettings(loginName))
                    {
                        ac.delAppSettings(loginName);
                    }
                }
            }
        }
Example #18
0
 /// <summary>
 /// 压缩加密处理
 /// </summary>
 /// <param name="inputPath">未加密文件path</param>
 /// <param name="outputPath">加密和压缩后的path</param>
 public void CompressAndEncrypt(string inputPath, string outputPath)
 {
     using (MemoryStream inputStream = new MemoryStream(File.ReadAllBytes(inputPath)))
     {
         SevenZip.Compression.LZMA.Encoder encoder = new SevenZip.Compression.LZMA.Encoder();
         using (MemoryStream compressStream = new MemoryStream())
         {
             encoder.WriteCoderProperties(compressStream);
             compressStream.Write(BitConverter.GetBytes(inputStream.Length), 0, 8);
             encoder.Code(inputStream, compressStream, inputStream.Length, -1, null);
             if (string.IsNullOrEmpty(AppConst.PwKey))
             {
                 File.WriteAllBytes(outputPath, compressStream.ToArray());
             }
             else
             {
                 File.WriteAllBytes(outputPath, XXTEA.Encrypt(compressStream.ToArray(), AppConst.PwKey));
             }
         }
     }
 }
Example #19
0
 public void CompressAndEncryptLZMA(string inputPath, string outputPath)
 {
     using (MemoryStream inputStream = new MemoryStream(File.ReadAllBytes(inputPath)))
     {
         Encoder coder = new Encoder();
         using (MemoryStream compressStream = new MemoryStream())
         {
             coder.WriteCoderProperties(compressStream);
             compressStream.Write(BitConverter.GetBytes(inputStream.Length), 0, 8);
             coder.Code(inputStream, compressStream, inputStream.Length, -1, null);
             if (string.IsNullOrEmpty(Config.password))
             {
                 File.WriteAllBytes(outputPath, compressStream.ToArray());
             }
             else
             {
                 File.WriteAllBytes(outputPath, XXTEA.Encrypt(compressStream.ToArray(), Config.password));
             }
         }
     }
 }
Example #20
0
 public static byte[] GetCompressAndEncryptLZMA(byte[] memory, string password)
 {
     byte[] value;
     using (MemoryStream inputStream = new MemoryStream(memory)) {
         Encoder coder = new Encoder();
         using (MemoryStream compressStream = new MemoryStream()) {
             coder.WriteCoderProperties(compressStream);
             compressStream.Write(BitConverter.GetBytes(inputStream.Length), 0, 8);
             coder.Code(inputStream, compressStream, inputStream.Length, -1, null);
             if (string.IsNullOrEmpty(password))
             {
                 value = compressStream.ToArray();
             }
             else
             {
                 value = XXTEA.Encrypt(compressStream.ToArray(), password);
             }
         }
     }
     return(value);
 }
Example #21
0
        static void EnIni()
        {
            var file = File.ReadAllBytes(@"2.ini");
            var data = Copy(file, 0);

            byte[] keys = { 0x4A, 0x89, 0xF1, 0xF1,
                            0xFB, 0xB2, 0x46, 0x23,
                            0xC2, 0x1C,    7, 0x8E,
                            0xF6, 0xEC, 0xCD, 0xF9 };

            var newdata = XXTEA.Encrypt(data, keys);

            File.WriteAllBytes("3.enc", newdata);

            //newdata = CopyLua(newdata);

            //var newdata2 = XXTEA.Decrypt(newdata, keys);


            // File.WriteAllBytes("1.lua", newdata2);
        }
Example #22
0
            private void SolveLoginPuzzle(uint[] v, uint[] k, byte unknown)
            {
                // v is the 8 byte thing form the login puzzle, k is the 3 other uints + an unknown number
                // k[3] will be updated to the correct number after this is done.

                for (uint guess = 0; guess < uint.MaxValue; guess++)
                {
                    k[3] = guess;

                    uint[] vClone = (uint[])v.Clone();
                    XXTEA.Encrypt(vClone, k, 6);
                    uint solution = vClone[1];

                    if (solution >> (32 - unknown) == 0 && (solution & (0x80000000 >> unknown)) != 0)
                    {
                        return;
                    }
                }

                throw new Exception("Failed to solve login puzzle");
            }
        public IActionResult Text(string orignalmsg, string key)
        {// Encryptclass ecrypt = new Encryptclass();
            myclass    op  = new myclass();
            Encryption op2 = new Encryption();

            op.orignalmsg = orignalmsg;
            string str = XXTEA.Encrypt(op.orignalmsg, key);

            op.cryptomsg = op2.convert_to_RNA(op2.convert_to_dna(op2.split_binary(op2.StringToBinary(str))));
            string document = op.cryptomsg;
            //decryption
            // op.cryptomsg = XXTEA.Decrypt(orignalmsg, key);
            //create file or put Encrypt message in file
            //path which file put in
            string path_Root = _hosting.WebRootPath;
            string docPath   = path_Root + "\\files\\";

            // Write the specified text asynchronously to a new file named "WriteTextAsync.txt".
            using (StreamWriter outputFile = new StreamWriter(Path.Combine(docPath, "Encryptmessage.txt")))
            {
                outputFile.Write(document);
            }
            return(View("Text", op));
        }
Example #24
0
        /// <summary>
        /// Computes and shows the crypto results.
        /// </summary>
        private void TestAllAlgorithms()
        {
            /**
             * Input length is 17 chars but 19 bytes.
             */
            String input = "Hello to € World!";

            /**
             * Arrays for padding testing.
             */
            Byte[] pb = new Byte[12] {
                1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12
            };
            Byte[] nb = new Byte[12] {
                1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12
            };

            /**
             * Test PKCS#7 padding.
             */
            Byte[] pp = PKCS7.Pad(pb, 8);
            Byte[] pu = PKCS7.Unpad(pp);
            //
            this.outputBox.Text += "PKCS#7 padded: " + BytesToString(pp) + "\n";
            this.outputBox.Text += "PKCS#7 unpadded: " + BytesToString(pu) + "\n";
            this.outputBox.Text += "\n";

            /**
             * Test zero byte padding.
             */
            Byte[] np = ZEROS.Pad(nb, 8);
            Byte[] nu = ZEROS.Unpad(np);
            //
            this.outputBox.Text += "Zero byte padded: " + BytesToString(np) + "\n";
            this.outputBox.Text += "Zero byte unpadded: " + BytesToString(nu) + "\n";
            this.outputBox.Text += "\n";

            /**
             * Text to bytes conversion from input.
             */
            Byte[] utf8Bytes = Encoding.UTF8.GetBytes(input);
            Byte[] ubeBytes  = Encoding.BigEndianUnicode.GetBytes(input);
            Byte[] uleBytes  = Encoding.Unicode.GetBytes(input);
            //
            this.outputBox.Text += "UTF-16 BE bytes: " + BytesToString(ubeBytes) + "\n";
            this.outputBox.Text += "UTF-16 LE bytes: " + BytesToString(uleBytes) + "\n";
            this.outputBox.Text += "UTF-8 bytes: " + BytesToString(utf8Bytes) + "\n";
            this.outputBox.Text += "\n";

            /**
             * Test base16 encoding.
             */
            String b16e = Base16.Encode(utf8Bytes);

            Byte[] b16d = Base16.Decode(b16e);
            //
            this.outputBox.Text += "Base16 encoded in UTF-8: " + b16e + "\n";
            this.outputBox.Text += "Base16 decoded in UTF-8: " + Encoding.UTF8.GetString(b16d) + "\n";
            this.outputBox.Text += "\n";

            /**
             * Test base64 encoding.
             */
            String b64e = Base64.Encode(utf8Bytes);

            Byte[] b64d = Base64.Decode(b64e);
            //
            this.outputBox.Text += "Base64 encoded in UTF-8: " + b64e + "\n";
            this.outputBox.Text += "Base64 decoded in UTF-8: " + Encoding.UTF8.GetString(b64d) + "\n";
            this.outputBox.Text += "\n";

            /**
             * Test generating GUID's.
             */
            String guid1 = GUID.Create();
            String guid2 = GUID.Create();
            String guid3 = GUID.Create();

            //
            this.outputBox.Text += "Generated GUID 1: " + guid1 + "\n";
            this.outputBox.Text += "Generated GUID 2: " + guid2 + "\n";
            this.outputBox.Text += "Generated GUID 3: " + guid3 + "\n";
            this.outputBox.Text += "\n";

            /**
             * Test ROT13 encoding.
             */
            Byte[] r13e = ROT13.Encode(utf8Bytes);
            Byte[] r13d = ROT13.Decode(r13e);
            //
            this.outputBox.Text += "ROT13 encrypted in UTF-8: " + Encoding.UTF8.GetString(r13e) + "\n";
            this.outputBox.Text += "ROT13 decrypted in UTF-8: " + Encoding.UTF8.GetString(r13d) + "\n";
            this.outputBox.Text += "\n";

            /**
             * Test MD5 with one official test vector and custom input.
             * Vectors from: http://www.febooti.com/products/filetweak/members/hash-and-crc/test-vectors/
             */
            Byte[] md5tv   = MD5.Compute(new Byte[0]);
            Byte[] md5utf8 = MD5.Compute(utf8Bytes);
            Byte[] md5key  = Encoding.UTF8.GetBytes("1234567890123456");
            Byte[] md5hmac = MD5.ComputeHMAC(md5key, utf8Bytes);
            //
            this.outputBox.Text += "MD5 from otv is ok: " + (Base16.Encode(md5tv) == "d41d8cd98f00b204e9800998ecf8427e").ToString() + "\n";
            this.outputBox.Text += "MD5 HMAC in UTF-8: " + Base16.Encode(md5hmac) + "\n";
            this.outputBox.Text += "MD5 in UTF-8: " + Base16.Encode(md5utf8) + "\n";
            this.outputBox.Text += "\n";

            /**
             * Test RIPEMD-160 with one official test vector and custom input.
             * Vectors from: http://www.febooti.com/products/filetweak/members/hash-and-crc/test-vectors/
             */
            Byte[] rmd160tv   = RMD160.Compute(new Byte[0]);
            Byte[] rmd160utf8 = RMD160.Compute(utf8Bytes);
            Byte[] rmd160key  = Encoding.UTF8.GetBytes("1234567890123456");
            Byte[] rmd160hmac = RMD160.ComputeHMAC(rmd160key, utf8Bytes);
            //
            this.outputBox.Text += "RIPEMD-160 from otv is ok: " + (Base16.Encode(rmd160tv) == "9c1185a5c5e9fc54612808977ee8f548b2258d31").ToString() + "\n";
            this.outputBox.Text += "RIPEMD-160 HMAC in UTF-8: " + Base16.Encode(rmd160hmac) + "\n";
            this.outputBox.Text += "RIPEMD-160 in UTF-8: " + Base16.Encode(rmd160utf8) + "\n";
            this.outputBox.Text += "\n";

            /**
             * Test SHA-1 with one official test vector and custom input.
             * Vectors from: http://www.febooti.com/products/filetweak/members/hash-and-crc/test-vectors/
             */
            Byte[] sha1tv   = SHA1.Compute(new Byte[0]);
            Byte[] sha1utf8 = SHA1.Compute(utf8Bytes);
            Byte[] sha1key  = Encoding.UTF8.GetBytes("1234567890123456");
            Byte[] sha1hmac = SHA1.ComputeHMAC(sha1key, utf8Bytes);
            //
            this.outputBox.Text += "SHA-1 from otv is ok: " + (Base16.Encode(sha1tv) == "da39a3ee5e6b4b0d3255bfef95601890afd80709").ToString() + "\n";
            this.outputBox.Text += "SHA-1 HMAC in UTF-8: " + Base16.Encode(sha1hmac) + "\n";
            this.outputBox.Text += "SHA-1 in UTF-8: " + Base16.Encode(sha1utf8) + "\n";
            this.outputBox.Text += "\n";

            /**
             * Test SHA-256 with one official test vector and custom input.
             * Vectors from: http://www.febooti.com/products/filetweak/members/hash-and-crc/test-vectors/
             */
            Byte[] sha256tv   = SHA256.Compute(new Byte[0]);
            Byte[] sha256utf8 = SHA256.Compute(utf8Bytes);
            Byte[] sha256key  = Encoding.UTF8.GetBytes("1234567890123456");
            Byte[] sha256hmac = SHA256.ComputeHMAC(sha256key, utf8Bytes);
            //
            this.outputBox.Text += "SHA-256 from otv is ok: " + (Base16.Encode(sha256tv) == "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855").ToString() + "\n";
            this.outputBox.Text += "SHA-256 HMAC in UTF-8: " + Base16.Encode(sha256hmac) + "\n";
            this.outputBox.Text += "SHA-256 in UTF-8: " + Base16.Encode(sha256utf8) + "\n";
            this.outputBox.Text += "\n";

            /**
             * Test ARC4 with one official test vector and custom input.
             * Vectors from: http://reikon.us/arc4
             */
            Byte[] arc4tvk = Base16.Decode("0123456789abcdef");
            Byte[] arc4tvt = Base16.Decode("0123456789abcdef");
            Byte[] arc4tve = ARC4.Encrypt(arc4tvk, arc4tvt);
            Byte[] arc4tvd = ARC4.Decrypt(arc4tvk, arc4tve);
            //
            Byte[] arc4k = Encoding.UTF8.GetBytes("1234567890123456");
            Byte[] arc4e = ARC4.Encrypt(arc4k, utf8Bytes);
            Byte[] arc4d = ARC4.Decrypt(arc4k, arc4e);
            //
            this.outputBox.Text += "ARC4 otv encrypted is ok: " + (Base16.Encode(arc4tve) == "75b7878099e0c596") + "\n";
            this.outputBox.Text += "ARC4 otv decrypted is ok: " + (Base16.Encode(arc4tvd) == "0123456789abcdef") + "\n";
            this.outputBox.Text += "ARC4 encrypted in UTF-8: " + Base16.Encode(arc4e) + "\n";
            this.outputBox.Text += "ARC4 decrypted in UTF-8: " + Encoding.UTF8.GetString(arc4d) + "\n";
            this.outputBox.Text += "\n";

            /**
             * Test XXTEA with one official test vector and custom input.
             * Vectors from: http://www.crypt.co.za/post/27
             */
            Byte[] xxttvk = Base16.Decode("9e3779b99b9773e9b979379e6b695156");
            Byte[] xxttvt = Base16.Decode("0102040810204080fffefcf8f0e0c080");
            Byte[] xxttve = XXTEA.Encrypt(xxttvk, xxttvt);
            Byte[] xxttvd = XXTEA.Decrypt(xxttvk, xxttve);
            //
            Byte[] xxteak = Encoding.UTF8.GetBytes("1234567890123456");
            Byte[] xxteae = XXTEA.Encrypt(xxteak, PKCS7.Pad(utf8Bytes, 4));
            Byte[] xxtead = PKCS7.Unpad(XXTEA.Decrypt(xxteak, xxteae));
            //
            this.outputBox.Text += "XXTEA otv encrypted is ok: " + (Base16.Encode(xxttve) == "01b815fd2e4894d13555da434c9d868a") + "\n";
            this.outputBox.Text += "XXTEA otv decrypted is ok: " + (Base16.Encode(xxttvd) == "0102040810204080fffefcf8f0e0c080") + "\n";
            this.outputBox.Text += "XXTEA encrypted in UTF-8: " + Base16.Encode(xxteae) + "\n";
            this.outputBox.Text += "XXTEA decrypted in UTF-8: " + Encoding.UTF8.GetString(xxtead) + "\n";
            this.outputBox.Text += "\n";

            /**
             * Test AES-128 with one official test vector and custom input.
             * Vectors from: http://www.csrc.nist.gov/publications/fips/fips197/fips-197.pdf
             */
            Byte[] aes128tvk = new Byte[16] {
                0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f
            };
            Byte[] aes128tvt = new Byte[16] {
                0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff
            };
            Byte[] aes128tve = AES.Encrypt(aes128tvk, aes128tvt, OperationMode.ECB, null); // No padding needed.
            Byte[] aes128tvd = AES.Decrypt(aes128tvk, aes128tve, OperationMode.ECB, null); // No padding needed.
            //
            Byte[] aes128k = Encoding.UTF8.GetBytes("1234567890123456");
            Byte[] aes128e = AES.Encrypt(aes128k, PKCS7.Pad(utf8Bytes, 16), OperationMode.ECB, null);
            Byte[] aes128d = PKCS7.Unpad(AES.Decrypt(aes128k, aes128e, OperationMode.ECB, null));
            //
            this.outputBox.Text += "AES-128 otv encrypted is ok: " + (Base16.Encode(aes128tve) == "69c4e0d86a7b0430d8cdb78070b4c55a") + "\n";
            this.outputBox.Text += "AES-128 otv decrypted is ok: " + (Base16.Encode(aes128tvd) == "00112233445566778899aabbccddeeff") + "\n";
            this.outputBox.Text += "AES-128 (ECB mode) encrypted in UTF-8: " + Base16.Encode(aes128e) + "\n";
            this.outputBox.Text += "AES-128 (ECB mode) decrypted in UTF-8: " + Encoding.UTF8.GetString(aes128d) + "\n";
            this.outputBox.Text += "\n";

            /**
             * Test AES-192 with one official test vector and custom input.
             * Vectors from: http://www.csrc.nist.gov/publications/fips/fips197/fips-197.pdf
             */
            Byte[] aes192tvk = new Byte[24] {
                0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17
            };
            Byte[] aes192tvt = new Byte[16] {
                0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff
            };
            Byte[] aes192tve = AES.Encrypt(aes192tvk, aes192tvt, OperationMode.ECB, null); // No padding needed.
            Byte[] aes192tvd = AES.Decrypt(aes192tvk, aes192tve, OperationMode.ECB, null); // No padding needed.
            //
            Byte[] aes192i = Encoding.UTF8.GetBytes("1234567890123456");
            Byte[] aes192k = Encoding.UTF8.GetBytes("123456789012345678901234");
            Byte[] aes192e = AES.Encrypt(aes192k, PKCS7.Pad(utf8Bytes, 16), OperationMode.CBC, aes192i);
            Byte[] aes192d = PKCS7.Unpad(AES.Decrypt(aes192k, aes192e, OperationMode.CBC, aes192i));
            //
            this.outputBox.Text += "AES-192 otv encrypted is ok: " + (Base16.Encode(aes192tve) == "dda97ca4864cdfe06eaf70a0ec0d7191") + "\n";
            this.outputBox.Text += "AES-192 otv decrypted is ok: " + (Base16.Encode(aes192tvd) == "00112233445566778899aabbccddeeff") + "\n";
            this.outputBox.Text += "AES-192 (CBC mode) encrypted in UTF-8: " + Base16.Encode(aes192e) + "\n";
            this.outputBox.Text += "AES-192 (CBC mode) decrypted in UTF-8: " + Encoding.UTF8.GetString(aes192d) + "\n";
            this.outputBox.Text += "\n";

            /**
             * Test AES-256 with one official test vector and custom input.
             * Vectors from: http://www.csrc.nist.gov/publications/fips/fips197/fips-197.pdf
             */
            Byte[] aes256tvk = new Byte[32] {
                0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f
            };
            Byte[] aes256tvt = new Byte[16] {
                0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff
            };
            Byte[] aes256tve = AES.Encrypt(aes256tvk, aes256tvt, OperationMode.ECB, null); // No padding needed.
            Byte[] aes256tvd = AES.Decrypt(aes256tvk, aes256tve, OperationMode.ECB, null); // No padding needed.
            //
            Byte[] aes256i = Encoding.UTF8.GetBytes("1234567890123456");
            Byte[] aes256k = Encoding.UTF8.GetBytes("12345678901234561234567890123456");
            Byte[] aes256e = AES.Encrypt(aes256k, PKCS7.Pad(utf8Bytes, 16), OperationMode.CBC, aes256i);
            Byte[] aes256d = PKCS7.Unpad(AES.Decrypt(aes256k, aes256e, OperationMode.CBC, aes256i));
            //
            this.outputBox.Text += "AES-256 otv encrypted is ok: " + (Base16.Encode(aes256tve) == "8ea2b7ca516745bfeafc49904b496089") + "\n";
            this.outputBox.Text += "AES-256 otv decrypted is ok: " + (Base16.Encode(aes256tvd) == "00112233445566778899aabbccddeeff") + "\n";
            this.outputBox.Text += "AES-256 (CBC mode) encrypted in UTF-8: " + Base16.Encode(aes256e) + "\n";
            this.outputBox.Text += "AES-256 (CBC mode) decrypted in UTF-8: " + Encoding.UTF8.GetString(aes256d) + "\n";
            this.outputBox.Text += "\n";
        }
Example #25
0
 static byte[] XXTEAEncrypt(byte[] Data, byte[] Key)
 {
     return(XXTEA.Encrypt(Data, Key));
 }
Example #26
0
 /// <summary>
 /// 解密
 /// </summary>
 /// <param name="data"></param>
 /// <param name="key"></param>
 /// <returns></returns>
 public Byte[] Decrypt(Byte[] data, String key)
 {
     return(XXTEA.Encrypt(data, key));
 }