Example #1
0
        private static byte[] encryptSize(int size, int counter)
        {
            int i;

            byte[] encrypted  = new byte[64];
            byte[] tempData   = new byte[64];
            byte[] plaintext  = new byte[64];
            byte[] encryptKey = Encoding.UTF8.GetBytes("----KEY_HERE----");
            byte[] encryptIV  = Encoding.UTF8.GetBytes("-----IV_HERE----");

            tempData = Encoding.UTF8.GetBytes(size.ToString());
            //Do we need to pad the data?
            if (tempData.Length != 64)
            {
                Array.Copy(tempData, plaintext, tempData.Length);
                for (i = tempData.Length; i < (plaintext.Length - tempData.Length); i++)
                {
                    plaintext[i] = 0x00;
                }
            }
            else
            {
                plaintext = tempData.ToArray();
            }
            encryptIV[(counter % 16)]++;

            Aes128CounterMode am;
            ICryptoTransform  ict;

            am  = new Aes128CounterMode(encryptIV);
            ict = am.CreateEncryptor(encryptKey, null);
            ict.TransformBlock(plaintext, 0, 64, encrypted, 0);
            return(encrypted);
        }
Example #2
0
        private static int decryptStatus(byte[] status, int counter)
        {
            int    ret = -1;
            string s;

            byte[] tempData   = new byte[64];
            byte[] decryptKey = Encoding.UTF8.GetBytes("----KEY_HERE----");
            byte[] decryptIV  = Encoding.UTF8.GetBytes("-----IV_HERE----");
            int    neg        = -1;

            decryptIV[(counter % 16)]++;

            Aes128CounterMode amD;
            ICryptoTransform  ictD;

            amD  = new Aes128CounterMode(decryptIV);
            ictD = amD.CreateDecryptor(decryptKey, null);


            ictD.TransformBlock(status, 0, 64, tempData, 0);
            s   = System.Text.Encoding.UTF8.GetString(tempData);
            neg = s.IndexOf("-");
            if (neg != -1)
            {
                Int32.TryParse(s.Substring(neg), out ret);
            }
            else
            {
                Int32.TryParse(s, out ret);
            }
            return(ret);
        }
Example #3
0
        //ENCRYPTION FOR CTR MODE
        private static byte[] encryptData(byte[] data, int counter)
        {
            byte[] encrypted = new byte[1024];
            byte[] tempdata  = new byte[64];
            byte[] plaintext = new byte[1024];
            byte[] tempPlain = new byte[64];
            int    i         = 0;

            byte[] encryptKey = Encoding.UTF8.GetBytes("----KEY_HERE----");
            byte[] encryptIV  = Encoding.UTF8.GetBytes("-----IV_HERE----");
            //Do we need to pad the data?
            if (data.Length != 1024)
            {
                Array.Copy(data, plaintext, data.Length);
                for (i = data.Length; i < (plaintext.Length - data.Length); i++)
                {
                    plaintext[i] = 0x00;
                }
            }
            else
            {
                plaintext = data.ToArray();
            }

            encryptIV[(counter % 16)]++;

            Aes128CounterMode am;
            ICryptoTransform  ict;

            am  = new Aes128CounterMode(encryptIV);
            ict = am.CreateEncryptor(encryptKey, null);
            ict.TransformBlock(plaintext, 0, 1024, encrypted, 0);
            return(encrypted);
        }
Example #4
0
        private static byte[] decryptData(byte[] data, int counter)
        {
            string s;

            byte[] decrypted  = new byte[1024];
            byte[] decryptKey = Encoding.UTF8.GetBytes("----KEY_HERE----");
            byte[] decryptIV  = Encoding.UTF8.GetBytes("-----IV_HERE----");

            decryptIV[(counter % 16)]++;

            Aes128CounterMode amD;
            ICryptoTransform  ict;

            amD = new Aes128CounterMode(decryptIV);
            ict = amD.CreateDecryptor(decryptKey, null);
            ict.TransformBlock(data, 0, 1024, decrypted, 0);
            s = System.Text.Encoding.UTF8.GetString(decrypted);
            return(decrypted);
        }