예제 #1
0
 private void InputData()
 {
     /*numericUpDown1.Value =20;
     textBox5.Text = "2";
     textBox2.Text = "1023";
     textBox3.Text = "32";
     textBox4.Text = "0";*/
     ulong x0 = Convert.ToUInt64(textBox5.Text);
     ulong m = Convert.ToUInt64(textBox2.Text);
     ulong a = Convert.ToUInt64(textBox3.Text);
     ulong c = Convert.ToUInt64(textBox4.Text);
     _myRandom = new MyRandom(x0, m, a, c);
 }
        private void EncryptStream(Stream inputStream, Stream outputStream)
        {
            byte[] key = _mKey;
            var s = GenerateSubKeys(key);
            ushort a0 = 0, b0 = 0, a, b;
            MyRandom myRandom = new MyRandom(31, 2147483647, 16807, 17711);
            a0 = (ushort) myRandom.random();//generate random a and b
            b0 = (ushort) myRandom.random();
            int w2 = (_mW << 1);
            var buffer = new byte[w2];//buffer of a and b
            inputStream.Position = 0;
            outputStream.Position = 0;
            int paddingLength = 0;
            int readBytesCount;
            do
            {//main cycle
                readBytesCount = inputStream.Read(buffer, 0, w2);
                if (readBytesCount == 0)
                    break;
                a = BitConverter.ToUInt16(buffer, 0);//get a
                b = BitConverter.ToUInt16(buffer, _mW);//get b
                EncryptBlock(ref a0, ref b0, ref a, ref b, s);
                outputStream.Write(BitConverter.GetBytes(a0), 0, _mW);
                outputStream.Write(BitConverter.GetBytes(b0), 0, _mW);
                if (readBytesCount < w2)//if the last block < w2
                    paddingLength = w2 - readBytesCount;//fill padding by random bytes

                if (OnProgressChanged != null)//calculate progress
                    OnProgressChanged((int)(inputStream.Position / (float)inputStream.Length * 100));
            } while (true);
            int countOfPaddingBlocks = _mKey.Length + 1;//space on md5 + length of prev padding
            countOfPaddingBlocks = countOfPaddingBlocks / w2 + (countOfPaddingBlocks / w2 > 0 ? 1 : 0);
            buffer = new byte[w2 * countOfPaddingBlocks];
            _mKey.CopyTo(buffer, 0);//password md5 padding
            buffer[buffer.Length - 1] = (byte)paddingLength;//padding length
            for (int i = 0; i < countOfPaddingBlocks; ++i)//padding filler
            {
                a = BitConverter.ToUInt16(buffer, i * w2);
                b = BitConverter.ToUInt16(buffer, i * w2 + _mW);
                EncryptBlock(ref a0, ref b0, ref a, ref b, s);
                outputStream.Write(BitConverter.GetBytes(a), 0, _mW);
                outputStream.Write(BitConverter.GetBytes(b), 0, _mW);
            }
            if (OnProgressChanged != null)
                OnProgressChanged(100);
        }
예제 #3
0
 public MyRC5(uint numberOfRounds, KeyLength keyLength, uint wordLength)
 {
     _numberOfRounds = (int) numberOfRounds;
     //_keyLength = keyLength;
     _wordLength = wordLength;
     switch (keyLength)
     {
         case KeyLength.KEY_LENGTH_16:
             _pw = P_16;
             _qw = Q_16;
             break;
         case KeyLength.KEY_LENGTH_32:
             _pw = P_32;
             _qw = Q_32;
             break;
         case KeyLength.KEY_LENGTH_64:
             _pw = P_64;
             _qw = Q_64;
             break;
     }
     _myRandom = new MyRandom(31, 2147483647, 16807, 17711);
     _myMd5 = new MyMD5();
 }
        private bool DecryptStream(Stream inputStream, Stream outputStream)
        {
            byte[] key = _mKey; //key in byte format
            var s = GenerateSubKeys(key);
            ushort a0 = 0, b0 = 0, a, b;
            MyRandom myRandom = new MyRandom(31, 2147483647, 16807, 17711);
            a0 = (ushort) myRandom.random();//generate random a and b
            b0 = (ushort) myRandom.random();
            int w2 = _mW << 1, i, l = _mKey.Length + 1;
            var buffer = new byte[w2];
            inputStream.Position = 0;
            outputStream.Position = 0;
            l = l / w2 + (l / w2 > 0 ? 1 : 0);//number of padding blocks
            do
            {
                inputStream.Read(buffer, 0, w2);
                a = BitConverter.ToUInt16(buffer, 0);
                b = BitConverter.ToUInt16(buffer, _mW);
                DecryptBlock(ref a0, ref b0, ref a, ref b, s);
                outputStream.Write(BitConverter.GetBytes(a), 0, _mW);
                outputStream.Write(BitConverter.GetBytes(b), 0, _mW);

                if (OnProgressChanged != null)//calculate progress
                    OnProgressChanged((int)(inputStream.Position / (float)inputStream.Length * 100));
            } while (inputStream.Position < inputStream.Length - l * w2 - w2);
            byte[] padding = new byte[l * w2 + w2];
            MemoryStream paddingStream = new MemoryStream(padding);
            paddingStream.Position = 0;
            do
            {
                inputStream.Read(buffer, 0, w2); //padding
                a = BitConverter.ToUInt16(buffer, 0);
                b = BitConverter.ToUInt16(buffer, _mW);
                DecryptBlock(ref a0, ref b0, ref a, ref b, s);
                paddingStream.Write(BitConverter.GetBytes(a), 0, _mW);
                paddingStream.Write(BitConverter.GetBytes(b), 0, _mW);

            } while (inputStream.Position < inputStream.Length);
            for (i = 0; i < _mKey.Length; ++i)//check md5
            {//16-31 password md5
                if (_mKey[i] != padding[i + 16/4])
                {
                    //MessageBox.Show("not right password");
                    if (OnDecryptedPasswordFailed != null)
                        OnDecryptedPasswordFailed();
                    return false;
                }
            }
            int len = padding[padding.Length - 1];
            outputStream.Write(padding, 0, w2 - len);
            if (OnProgressChanged != null)//calculate process
                OnProgressChanged(100);
            return true;
        }