Beispiel #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);
        }
Beispiel #2
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);
        }