private Byte[] ComputeOwnerKey(Byte[] userPad, Byte[] ownerPad)
        {
            Byte[] ownerKey = new Byte[32];
            Byte[] digest   = MD5.Digest(ownerPad);
            if (_alg == EncryptAlg.STANDARD_ENCRYPTION_128 || _alg == EncryptAlg.AES_128)
            {
                Byte[] mkey = new Byte[_keyLength / 8];

                for (Int32 k = 0; k < 50; ++k)
                {
                    Array.Copy(MD5.Digest(digest, 0, mkey.Length), 0, digest, 0, mkey.Length);
                }

                Array.Copy(userPad, 0, ownerKey, 0, 32);

                for (Int32 i = 0; i < 20; ++i)
                {
                    for (Int32 j = 0; j < mkey.Length; ++j)
                    {
                        mkey[j] = (Byte)(digest[j] ^ i);
                    }

                    RC4.Encrypt(mkey, ownerKey, 0);
                }
            }
            else
            {
                var rc4 = new RC4(digest, 0, 5);
                rc4.Encrypt(userPad, ownerKey);
            }

            return(ownerKey);
        }
        void SetupUserKey()
        {
            if (_alg == EncryptAlg.STANDARD_ENCRYPTION_128 || _alg == EncryptAlg.AES_128)
            {
                var md5 = new MD5();
                md5.BlockUpdate(_pad);
                md5.BlockUpdate(_documentId);
                Byte[] digest = md5.DoFinal();

                Array.Copy(digest, 0, _userKey, 0, 16);
                for (Int32 k = 16; k < 32; ++k)
                {
                    _userKey[k] = 0;
                }
                for (Int32 i = 0; i < 20; ++i)
                {
                    for (Int32 j = 0; j < _mkey.Length; ++j)
                    {
                        digest[j] = (Byte)(_mkey[j] ^ i);
                    }

                    RC4.Encrypt(digest, _userKey, 0, _mkey.Length, 16);
                }
            }
            else
            {
                RC4.Encrypt(_pad, _userKey, 0);
            }
        }
예제 #3
0
        public static void Encrypt(Byte[] key, Byte[] data, Int32 offset = 0, Int32 keyLength = 0, Int32 dataLength = 0)
        {
            if (dataLength == 0)
            {
                dataLength = data.Length;
            }
            var rc4 = new RC4(key, 0, keyLength);

            rc4.Encrypt(data, 0, dataLength);
        }
        public Byte[] DecryptByteArray(Byte[] b)
        {
            MemoryStream ba = new MemoryStream();

            var rc4 = new RC4(_key);

            Byte[] b2 = rc4.EncryptData(b);

            if (b2 != null)
            {
                ba.Write(b2, 0, b2.Length);
            }

            return(ba.ToArray());
        }