Beispiel #1
0
        protected void testDes()
        {
            //string testStr = "asdfasdf5";
            //byte[] inBytes = System.Text.Encoding.UTF8.GetBytes(testStr);
            //byte[] key = { 0x65, 0xC1, 0x78, 0xB2, 0x84, 0xD1, 0x97, 0xCC };
            byte[] key = { 26, 32, 127, 193, 251, 239, 174, 97 };
            //byte[] inBytes = { 0x3f, 0x79, 0xd5, 0xe2, 0x4a, 0x8c, 0xb6, 0xc1, 0x3f, 0x79, 0xd5, 0xe2, 0x4a, 0x8c, 0xb6, 0xc1 };
            byte[] inBytes  = { 0x0c, 0x00, 0x00, 0x00, 0x03, 0x35, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00 };
            byte[] outBytes = new byte[16];
            uint   inSize   = (uint)inBytes.Length;

            DES_key_schedule des5Key = new DES_key_schedule();     // RC5 key

            Dec.DES_set_key_unchecked(key, des5Key);

            CryptContext cryptContext = new CryptContext();

            cryptContext.m_cryptAlgorithm = CryptAlgorithm.DES;
            cryptContext.setCryptKey(key);

            //Crypt.encryptData(inBytes, 0, 16, ref outBytes, des5Key, CryptAlgorithm.DES);
            //Crypt.decryptData(outBytes, 0, 16, ref inBytes, des5Key, CryptAlgorithm.DES);
            //testStr = System.Text.Encoding.UTF8.GetString(inBytes);
            Crypt.decryptData(inBytes, 0, 16, ref outBytes, cryptContext);
        }
Beispiel #2
0
        public void DES_set_key(DES_cblock key, DES_key_schedule schedule)
        {
            int[] numArray = new int[] { 0, 0, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 0 };
            uint  l        = 0;
            uint  num2     = 0;
            uint  t        = 0;
            uint  num4     = 0;
            uint  a        = 0;

            func_c2l_first(key.arrBlock, ref l);
            func_c2l_second(key.arrBlock, ref num2);
            func_PERM_OP(ref num2, ref l, ref t, 4, 0xf0f0f0f);
            func_HPERM_OP(ref l, ref t, -2, 0xcccc0000);
            func_HPERM_OP(ref num2, ref t, -2, 0xcccc0000);
            func_PERM_OP(ref num2, ref l, ref t, 1, 0x55555555);
            func_PERM_OP(ref l, ref num2, ref t, 8, 0xff00ff);
            func_PERM_OP(ref num2, ref l, ref t, 1, 0x55555555);
            num2 = (uint)(((((num2 & 0xff) << 0x10) | (num2 & 0xff00)) | ((num2 & 0xff0000) >> 0x10)) | ((l & 0xf0000000) >> 4));
            l   &= 0xfffffff;
            for (int i = 0; i < 0x10L; i++)
            {
                if (numArray[i] > 0)
                {
                    l    = (l >> 2) | (l << 0x1a);
                    num2 = (num2 >> 2) | (num2 << 0x1a);
                }
                else
                {
                    l    = (l >> 1) | (l << 0x1b);
                    num2 = (num2 >> 1) | (num2 << 0x1b);
                }
                l    &= 0xfffffff;
                num2 &= 0xfffffff;
                num4  = ((des_skb[0, (int)((IntPtr)(l & 0x3f))] | des_skb[1, (int)((IntPtr)(((l >> 6) & 3) | ((l >> 7) & 60)))]) | des_skb[2, (int)((IntPtr)(((l >> 13) & 15) | ((l >> 14) & 0x30)))]) | des_skb[3, (int)((IntPtr)((((l >> 20) & 1) | ((l >> 0x15) & 6)) | ((l >> 0x16) & 0x38)))];
                t     = ((des_skb[4, (int)((IntPtr)(num2 & 0x3f))] | des_skb[5, (int)((IntPtr)(((num2 >> 7) & 3) | ((num2 >> 8) & 60)))]) | des_skb[6, (int)((IntPtr)((num2 >> 15) & 0x3f))]) | des_skb[7, (int)((IntPtr)(((num2 >> 0x15) & 15) | ((num2 >> 0x16) & 0x30)))];
                a     = ((t << 0x10) | (num4 & 0xffff)) & uint.MaxValue;
                schedule.arrKeys[2 * i] = func_ROTATE(ref a, 30) & uint.MaxValue;
                a = (num4 >> 0x10) | (t & 0xffff0000);
                schedule.arrKeys[(2 * i) + 1] = func_ROTATE(ref a, 0x1a) & uint.MaxValue;
            }
        }
Beispiel #3
0
        public void DES_encrypt1(uint[] data, DES_key_schedule ks1, uint[,] sp, bool enc)
        {
            uint r = 0;
            uint l = 0;
            uint t = 0;
            uint u = 0;
            int  s = 0;

            l = data[0];
            r = data[1];
            func_IP(ref l, ref r);
            l = func_ROTATE(ref l, 0x1d) & uint.MaxValue;
            r = func_ROTATE(ref r, 0x1d) & uint.MaxValue;
            if (enc)
            {
                for (s = 0; s < 0x20; s += 8)
                {
                    func_D_ENCRYPT(ref r, ref l, s, ref u, ref t, ks1.arrKeys, sp);
                    func_D_ENCRYPT(ref l, ref r, s + 2, ref u, ref t, ks1.arrKeys, sp);
                    func_D_ENCRYPT(ref r, ref l, s + 4, ref u, ref t, ks1.arrKeys, sp);
                    func_D_ENCRYPT(ref l, ref r, s + 6, ref u, ref t, ks1.arrKeys, sp);
                }
            }
            else
            {
                for (s = 30; s > 0; s -= 8)
                {
                    func_D_ENCRYPT(ref r, ref l, s, ref u, ref t, ks1.arrKeys, sp);
                    func_D_ENCRYPT(ref l, ref r, s - 2, ref u, ref t, ks1.arrKeys, sp);
                    func_D_ENCRYPT(ref r, ref l, s - 4, ref u, ref t, ks1.arrKeys, sp);
                    func_D_ENCRYPT(ref l, ref r, s - 6, ref u, ref t, ks1.arrKeys, sp);
                }
            }
            r = func_ROTATE(ref r, 3) & uint.MaxValue;
            l = func_ROTATE(ref l, 3) & uint.MaxValue;
            func_FP(ref l, ref r);
            data[0] = r;
            data[1] = l;
            r       = l = t = u = 0;
        }
Beispiel #4
0
        static public bool DES_ECB_Symmetry_Decode_OpenSSL(byte[] decryptByte, uint startPos, uint inLen, ref byte[] outBytes, DES_key_schedule rgbKey, CryptContext cryptContext)
        {
            try
            {
                Dec.DES_ecb_encrypt_one(decryptByte, startPos, inLen, ref outBytes, rgbKey, RC5.RC5_DECRYPT, cryptContext);

                return(true);
            }
            catch
            {
                Ctx.m_instance.m_logSys.error("DES_ecb_encrypt_one error");
                return(false);
            }
        }
Beispiel #5
0
 // rc5 加密后大小不会变化的
 static public bool DES_ECB_Symmetry_Encode_OpenSSL(byte[] encryptByte, uint startPos, uint inLen, ref byte[] outBytes, DES_key_schedule rgbKey, CryptContext cryptContext)
 {
     try
     {
         Dec.DES_ecb_encrypt_one(encryptByte, startPos, inLen, ref outBytes, rgbKey, RC5.RC5_ENCRYPT, cryptContext);
         return true;
     }
     catch
     {
         Ctx.m_instance.m_logSys.error("DES_ECB_Symmetry_Encode_OpenSSL error");
         return false;
     }
 }