Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // converted from Python: http://pastebin.com/w9QNLreT
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.IO;
- public class glfsr
- {
- int polynom;
- int mask;
- int data;
- public glfsr(int polynom, int initial_value)
- {
- int tmp;
- this.polynom = polynom | 1;
- this.data = initial_value;
- tmp = polynom;
- this.mask = 1;
- while(tmp != 0) {
- if((tmp & this.mask) != 0)
- {
- tmp = tmp ^ this.mask;
- }
- if(tmp == 0)
- {
- break;
- }
- this.mask = this.mask << 1;
- }
- }
- public int next_state()
- {
- this.data = this.data << 1;
- int retval = 0;
- if((this.data & this.mask) != 0)
- {
- retval = 1;
- this.data = this.data ^ this.polynom;
- }
- return retval;
- }
- }
- public class sprng
- {
- glfsr glfsr_d;
- glfsr glfsr_c;
- public sprng(int polynom_d, int init_value_d, int polynom_c, int init_value_c)
- {
- this.glfsr_d = new glfsr(polynom_d, init_value_d);
- this.glfsr_c = new glfsr(polynom_c, init_value_c);
- }
- public int next_byte()
- {
- int byte_ = 0;
- int bitpos = 7;
- while(1 == 1)
- {
- int bit_d = this.glfsr_d.next_state();
- int bit_c = this.glfsr_c.next_state();
- if(bit_c != 0)
- {
- int bit_r = bit_d;
- byte_ = byte_ | (bit_r << bitpos);
- bitpos = bitpos - 1;
- if(bitpos < 0)
- {
- break;
- }
- }
- }
- return byte_;
- }
- }
- //# ----------------------------------------------------------------------------
- //# Crypto4o functions end here
- //# ----------------------------------------------------------------------------
- class test
- {
- static void Main(String[] args)
- {
- String s;
- char random_ch;
- char input_ch;
- sprng prng = new sprng(
- Convert.ToInt32(args[3], 16),
- Convert.ToInt32(args[4], 16),
- Convert.ToInt32(args[5], 16),
- Convert.ToInt32(args[6], 16)
- );
- s = String.Format("GLFSR D0: using polynom 0x%X, initial value: 0x%X.", Convert.ToInt32(args[3], 16), Convert.ToInt32(args[4], 16));
- Console.WriteLine(s);
- s = String.Format("GLFSR C0: using polynom 0x%X, initial value: 0x%X.", Convert.ToInt32(args[5], 16), Convert.ToInt32(args[6], 16));
- Console.Write(s);
- StreamReader f = new StreamReader(args[1]);
- StreamWriter g = new StreamWriter(args[2]);
- while(!f.EndOfStream)
- {
- input_ch = (char)f.Read();
- random_ch = (char)(prng.next_byte() & 0xff);
- g.Write((char)((int)input_ch ^ random_ch));
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement