示例#1
0
        public byte[] Decrypt(byte[] input, out byte[] paramac)
        {
            byte[] block  = new byte[16];
            byte[] tblock = new byte[16];
            byte[] tctr   = new byte[16];
            paramac = new byte[16];
            int size = input.Length - 0x20;

            byte[] outblock = new byte[size];

            int i = 0;

            Array.Copy(input, i, tblock, 0, 16);
            while (size > 16)
            {
                byte[] oblock = Decrypt_Block(tblock, ref paramac);
                Array.Copy(oblock, 0, outblock, i, 16);
                size -= 16;
                i    += 16;
                Array.Copy(input, i, tblock, 0, 16);
            }
            DSi_CTR cryptoctx = new DSi_CTR()
            {
                _Key = Key,
                _Ctr = ctr.Clone() as byte[]
            };

            block = cryptoctx.Crypt(block);
            Array.Copy(input, i, block, 0, size);

            block = Decrypt_Block(block, ref paramac);
            Array.Copy(block, 0, outblock, i, size);

            return(outblock);
        }
示例#2
0
        public byte[] Decrypt_Block(byte[] input, ref byte[] paramac)
        {
            DSi_CTR cryptoctx = new DSi_CTR()
            {
                _Key = Key,
                _Ctr = ctr
            };

            byte[] output = cryptoctx.Crypt(input);
            for (int i = 0; i < 16; i++)
            {
                mac[i] ^= output[15 - i];
            }

            AesManaged aesAlg = new AesManaged
            {
                Mode = CipherMode.ECB
            };
            ICryptoTransform encryptor = aesAlg.CreateEncryptor(Key, null);

            mac = encryptor.TransformFinalBlock(mac, 0, 16);
            for (int i = 0; i < 16; i++)
            {
                paramac[i] = Convert.ToByte(mac[15 - i] ^ S0[i]);
            }
            return(output);
        }
示例#3
0
        public DSi_CCM(byte[] key, int maclength, int payloadlength, int assoclength, byte[] nonce)
        {
            this.Key    = key;
            this.maclen = maclength;

            maclength     = (maclength - 2) / 2;
            payloadlength = (payloadlength + 15) & ~15;

            mac[0] = Convert.ToByte((maclength << 3) | 2);
            if (assoclength != 0)
            {
                mac[0] |= (1 << 6);
            }
            for (int i = 0; i < 12; i++)
            {
                mac[1 + i] = nonce[11 - i];
            }
            mac[13] = Convert.ToByte(payloadlength >> 16);
            mac[14] = Convert.ToByte(payloadlength >> 8);
            mac[15] = Convert.ToByte((payloadlength >> 0) % 256);

            AesManaged aesAlg = new AesManaged
            {
                Mode = CipherMode.ECB
            };
            ICryptoTransform encryptor = aesAlg.CreateEncryptor(key, null);

            mac = encryptor.TransformFinalBlock(mac, 0, 16);

            ctr[0] = 2;
            for (int i = 0; i < 12; i++)
            {
                ctr[1 + i] = nonce[11 - i];
            }
            ctr[13] = 0;
            ctr[14] = 0;
            ctr[15] = 0;
            DSi_CTR cryptoctx = new DSi_CTR()
            {
                _Key = key,
                _Ctr = ctr
            };

            S0 = cryptoctx.Crypt(null);
        }
示例#4
0
        public byte[] Decrypt(byte[] buffer, byte[] metablock)
        {
            byte[] ctr        = new byte[16];
            byte[] dnonce     = new byte[12];
            byte[] scratchpad = new byte[16];
            byte[] chkmac     = new byte[16];
            byte[] genmac     = new byte[16];
            byte[] blockcrypt = new byte[16];
            int    chksize;

            Array.Copy(metablock, chkmac, 16);
            Array.Copy(metablock, 16, ctr, 0, 16);
            Array.Copy(metablock, 16, blockcrypt, 0, 16);
            ctr[0]  = 0;
            ctr[13] = 0;
            ctr[14] = 0;
            ctr[15] = 0;

            DSi_CTR cryptoctx = new DSi_CTR()
            {
                Key = Key,
                Ctr = ctr
            };

            scratchpad = cryptoctx.Crypt(blockcrypt);
            chksize    = (scratchpad[13] << 16) | (scratchpad[14] << 8) | (scratchpad[15] << 0);

            if (scratchpad[0] != 0x3A || chksize != buffer.Length - 0x20)
            {
                return(null);
            }
            Array.Copy(metablock, 17, dnonce, 0, 12);

            DSi_CCM crypt = new DSi_CCM(Key, 16, buffer.Length - 0x20, 0, dnonce);

            buffer = crypt.Decrypt(buffer, out genmac);

            return(buffer);
        }