예제 #1
0
        public static void DES_ecb_encrypt_one(byte[] inBytesTotal, uint startPos, uint inLen, ref byte[] outBytesTotal,
                                               DES_key_schedule ks, int enc, CryptContext cryptContext)
        {
            int len_ = (((int)inLen + 7) / 8) * 8;

            outBytesTotal = new byte[len_];

            byte[] inBytes  = new byte[8];
            byte[] outBytes = new byte[8];

            int idx = 0;

            for (idx = 0; idx <= inLen - 8; idx += 8)
            {
                //if (idx % 16 == 0)  // 如果是 16 的整数倍
                //{
                // 保证 8 个字节
                CryptUtil.get8Byte(inBytesTotal, (int)startPos + idx, inBytes);
                DES_ecb_encrypt(inBytes, outBytes, ks, enc);
                Array.Copy(outBytes, 0, outBytesTotal, idx, 8);
                //}
                //else
                //{
                //    Array.Copy(inBytesTotal, idx, outBytesTotal, idx, 8);
                //}
            }
        }
예제 #2
0
        public static void RC5_32_ecb_encrypt(byte[] inBytes, byte[] outBytes,
                                              RC5_32_KEY ks, int encrypt, CryptContext cryptContext)
        {
            ulong l = 0;

            ulong[] d = new ulong[2];

            int c2l_inBytes_startIdx  = 0;
            int l2c_outBytes_startIdx = 0;

            CryptUtil.c2l(inBytes, ref l, ref c2l_inBytes_startIdx);
            d[0] = l;
            CryptUtil.c2l(inBytes, ref l, ref c2l_inBytes_startIdx);
            d[1] = l;
            if (encrypt > 0)
            {
                RC5_32_encrypt(ref d, ks);
            }
            else
            {
                RC5_32_decrypt(ref d, ks);
            }
            l = d[0];
            CryptUtil.l2c(l, outBytes, ref l2c_outBytes_startIdx);
            l = d[1];
            CryptUtil.l2c(l, outBytes, ref l2c_outBytes_startIdx);
            l = d[0] = d[1] = 0;
        }
예제 #3
0
        // 加密,使用 des 对称数字加密算法,加密8字节补齐,可能会导致变长
        public uint encrypt(CryptContext cryptContext, uint len_ = 0)
        {
#if OBSOLETE
            len_ = (len_ == 0 ? length : len_);

            byte[] retByte = null;
            // 只有 8 个字节的时候才加密
            uint leftCnt  = len_ % 8; // 剩余的数量
            uint cryptCnt = leftCnt;

            if (len_ >= 8)
            {
                Crypt.encryptData(m_dynBuff.buff, position, len_ - leftCnt, ref retByte, cryptKey);
                writeBytes(retByte, 0, (uint)retByte.Length, false);
                cryptCnt += (uint)retByte.Length;
            }

            if (leftCnt > 0) // 如果还有剩余的字节没有加密,还需要增加长度
            {
                position += leftCnt;
            }

            return(cryptCnt);
#endif
            len_ = (len_ == 0 ? length : len_);
            uint alignLen_ = ((len_ + 7) / 8) * 8; // 补齐 8 个字节,因为加密是 8 个字节一次加密,只要是 8 个字节的整数倍,无论多少个都可以任意解压
            uint leftLen_  = alignLen_ - len_;
            if (leftLen_ > 0)
            {
                if (m_padBytes == null)
                {
                    m_padBytes = new byte[8];
                }

                // 保存数据,然后补 0
                Array.Copy(m_dynBuff.buff, position + len_, m_padBytes, 0, leftLen_);
                Array.Clear(m_dynBuff.buff, (int)(position + len_), (int)leftLen_);
            }

            if (len_ == 0)      // 修正之后还等于 0
            {
                return(0);
            }

            if (alignLen_ > m_dynBuff.capacity)   // 如果最后加密(由于补齐)的长度大于原始长度
            {
                length = alignLen_;
            }

            byte[] retByte = null;

            Crypt.encryptData(m_dynBuff.buff, position, alignLen_, ref retByte, cryptContext); // 注意补齐不一定是 0
            Array.Copy(m_padBytes, 0, m_dynBuff.buff, position + len_, leftLen_);              // 拷贝回去
            replace(retByte, 0, alignLen_, position, len_);

            //check();

            return(alignLen_);
        }
예제 #4
0
        // 解密,现在必须 8 字节对齐解密
        public void decrypt(CryptContext cryptContext, uint len_ = 0)
        {
            len_ = (len_ == 0 ? length : len_);

            byte[] retByte = null;

            if (0 == len_)
            {
                return;
            }

            Crypt.decryptData(m_dynBuff.buff, position, len_, ref retByte, cryptContext);
            writeBytes(retByte, 0, (uint)retByte.Length, false);

            //check();
        }
예제 #5
0
        protected void testRc5()
        {
            string testStr = "asdfasdf5";
            //byte[] inBytes = System.Text.Encoding.UTF8.GetBytes(testStr);
            byte[] inBytes = { 0x3f, 0x79, 0xd5, 0xe2, 0x4a, 0x8c, 0xb6, 0xc1, 0x3f, 0x79, 0xd5, 0xe2, 0x4a, 0x8c, 0xb6, 0xc1 };
            byte[] outBytes = new byte[8];
            uint inSize = (uint)inBytes.Length;

            RC5_32_KEY rc5Key = new RC5_32_KEY();     // RC5 key
            RC5.RC5_32_set_key(rc5Key, 16, Crypt.RC5_KEY, RC5.RC5_12_ROUNDS);     // 生成秘钥

            CryptContext cryptContext = new CryptContext();

            Crypt.encryptData(inBytes, 0, 16, ref outBytes, cryptContext);
            Crypt.decryptData(outBytes, 0, 16, ref inBytes, cryptContext);
            testStr = System.Text.Encoding.Default.GetString(inBytes);
        }
예제 #6
0
        public static void RC5_32_ecb_encrypt_one(byte[] inBytesTotal, uint startPos, uint inLen, ref byte[] outBytesTotal,
                                                  RC5_32_KEY ks, int encrypt, CryptContext cryptContext)
        {
            int len_ = (((int)inLen + 7) / 8) * 8;

            outBytesTotal = new byte[len_];

            byte[] inBytes  = new byte[8];
            byte[] outBytes = new byte[8];

            int idx = 0;

            for (idx = 0; idx <= inLen - 8; idx += 8)
            {
                // ±£Ö¤ 8 ¸ö×Ö½Ú
                CryptUtil.get8Byte(inBytesTotal, (int)startPos + idx, inBytes);
                RC5_32_ecb_encrypt(inBytes, outBytes, ks, encrypt, cryptContext);
                Array.Copy(outBytes, 0, outBytesTotal, idx, 8);
            }
        }
예제 #7
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);
        }
예제 #8
0
파일: Des.cs 프로젝트: zhutaorun/unitygame
        public static void DES_ecb_encrypt_one(byte[] inBytesTotal, uint startPos, uint inLen, ref byte[] outBytesTotal,
                     DES_key_schedule ks, int enc, CryptContext cryptContext)
        {
            int len_ = (((int)inLen + 7) / 8) * 8;
            outBytesTotal = new byte[len_];

            byte[] inBytes = new byte[8];
            byte[] outBytes = new byte[8];

            int idx = 0;
            for (idx = 0; idx <= inLen - 8; idx += 8)
            {
                //if (idx % 16 == 0)  // 如果是 16 的整数倍
                //{
                    // 保证 8 个字节
                    CryptUtil.get8Byte(inBytesTotal, (int)startPos + idx, inBytes);
                    DES_ecb_encrypt(inBytes, outBytes, ks, enc);
                    Array.Copy(outBytes, 0, outBytesTotal, idx, 8);
                //}
                //else
                //{
                //    Array.Copy(inBytesTotal, idx, outBytesTotal, idx, 8);
                //}
            }
        }
예제 #9
0
파일: RC5.cs 프로젝트: zhutaorun/unitygame
        public static void RC5_32_ecb_encrypt_one(byte[] inBytesTotal, uint startPos, uint inLen, ref byte[] outBytesTotal,
                RC5_32_KEY ks, int encrypt, CryptContext cryptContext)
        {
            int len_ = (((int)inLen + 7) / 8) * 8;
            outBytesTotal = new byte[len_];

            byte[] inBytes = new byte[8];
            byte[] outBytes = new byte[8];

            int idx = 0;
            for (idx = 0; idx <= inLen - 8; idx += 8)
            {
                // ±£Ö¤ 8 ¸ö×Ö½Ú
                CryptUtil.get8Byte(inBytesTotal, (int)startPos + idx, inBytes);
                RC5_32_ecb_encrypt(inBytes, outBytes, ks, encrypt, cryptContext);
                Array.Copy(outBytes, 0, outBytesTotal, idx, 8);
            }
        }
예제 #10
0
파일: RC5.cs 프로젝트: zhutaorun/unitygame
        public static void RC5_32_ecb_encrypt(byte[] inBytes, byte[] outBytes,
                        RC5_32_KEY ks, int encrypt, CryptContext cryptContext)
        {
            ulong l = 0;
            ulong[] d = new ulong[2];

            int c2l_inBytes_startIdx = 0;
            int l2c_outBytes_startIdx = 0;

            CryptUtil.c2l(inBytes, ref l, ref c2l_inBytes_startIdx);
            d[0] = l;
            CryptUtil.c2l(inBytes, ref l, ref c2l_inBytes_startIdx);
            d[1] = l;
            if (encrypt > 0)
                RC5_32_encrypt(ref d, ks);
            else
                RC5_32_decrypt(ref d, ks);
            l = d[0];
            CryptUtil.l2c(l, outBytes, ref l2c_outBytes_startIdx);
            l = d[1];
            CryptUtil.l2c(l, outBytes, ref l2c_outBytes_startIdx);
            l = d[0] = d[1] = 0;
        }
예제 #11
0
        // 解密,现在必须 8 字节对齐解密
        public void decrypt(CryptContext cryptContext, uint len_ = 0)
        {
            len_ = (len_ == 0 ? length : len_);

            byte[] retByte = null;

            if (0 == len_)
            {
                return;
            }

            Crypt.decryptData(m_dynBuff.buff, position, len_, ref retByte, cryptContext);
            writeBytes(retByte, 0, (uint)retByte.Length, false);

            //check();
        }
예제 #12
0
        // 加密,使用 des 对称数字加密算法,加密8字节补齐,可能会导致变长
        public uint encrypt(CryptContext cryptContext, uint len_ = 0)
        {
#if OBSOLETE
            len_ = (len_ == 0 ? length : len_);

            byte[] retByte = null;
            // 只有 8 个字节的时候才加密
            uint leftCnt = len_ % 8;  // 剩余的数量
            uint cryptCnt = leftCnt;

            if (len_ >= 8)
            {
                Crypt.encryptData(m_dynBuff.buff, position, len_ - leftCnt, ref retByte, cryptKey);
                writeBytes(retByte, 0, (uint)retByte.Length, false);
                cryptCnt += (uint)retByte.Length;
            }

            if (leftCnt > 0) // 如果还有剩余的字节没有加密,还需要增加长度
            {
                position += leftCnt;
            }

            return cryptCnt;
#endif
            len_ = (len_ == 0 ? length : len_);
            uint alignLen_ = ((len_ + 7) / 8) * 8; // 补齐 8 个字节,因为加密是 8 个字节一次加密,只要是 8 个字节的整数倍,无论多少个都可以任意解压
            uint leftLen_ = alignLen_ - len_;
            if(leftLen_ > 0)
            {
                if(m_padBytes == null)
                {
                    m_padBytes = new byte[8];
                }

                // 保存数据,然后补 0
                Array.Copy(m_dynBuff.buff, position + len_, m_padBytes, 0, leftLen_);
                Array.Clear(m_dynBuff.buff, (int)(position + len_), (int)leftLen_);
            }

            if (len_ == 0)      // 修正之后还等于 0 
            {
                return 0;
            }

            if (alignLen_ > m_dynBuff.capacity)   // 如果最后加密(由于补齐)的长度大于原始长度
            {
                length = alignLen_;
            }

            byte[] retByte = null;

            Crypt.encryptData(m_dynBuff.buff, position, alignLen_, ref retByte, cryptContext);  // 注意补齐不一定是 0 
            Array.Copy(m_padBytes, 0, m_dynBuff.buff, position + len_, leftLen_);       // 拷贝回去
            replace(retByte, 0, alignLen_, position, len_);

            //check();

            return alignLen_;
        }