Advertisement
furas

Converted from Python

Nov 17th, 2015
343
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.09 KB | None | 0 0
  1. // converted from Python: http://pastebin.com/w9QNLreT
  2.  
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Linq;
  6. using System.Text;
  7. using System.IO;
  8.  
  9. public class glfsr
  10. {
  11.     int polynom;
  12.     int mask;
  13.     int data;
  14.    
  15.     public glfsr(int polynom, int initial_value)
  16.     {
  17.         int tmp;
  18.        
  19.         this.polynom = polynom | 1;
  20.         this.data = initial_value;
  21.         tmp = polynom;
  22.  
  23.         this.mask = 1;
  24.  
  25.         while(tmp != 0) {
  26.             if((tmp & this.mask) != 0)
  27.             {
  28.                 tmp = tmp ^ this.mask;
  29.             }
  30.  
  31.             if(tmp == 0)
  32.             {
  33.                 break;
  34.             }
  35.  
  36.             this.mask = this.mask << 1;
  37.         }
  38.     }
  39.    
  40.     public int next_state()
  41.     {
  42.         this.data = this.data << 1;
  43.  
  44.         int retval = 0;
  45.  
  46.         if((this.data & this.mask) != 0)
  47.         {
  48.             retval = 1;
  49.             this.data = this.data ^ this.polynom;
  50.         }
  51.  
  52.         return retval;
  53.     }
  54. }
  55.  
  56. public class sprng
  57. {
  58.     glfsr glfsr_d;
  59.     glfsr glfsr_c;
  60.    
  61.     public sprng(int polynom_d, int init_value_d, int polynom_c, int init_value_c)
  62.     {
  63.         this.glfsr_d = new glfsr(polynom_d, init_value_d);
  64.         this.glfsr_c = new glfsr(polynom_c, init_value_c);
  65.     }
  66.  
  67.     public int next_byte()
  68.     {
  69.         int byte_ = 0;
  70.         int bitpos = 7;
  71.  
  72.         while(1 == 1)
  73.         {
  74.             int bit_d = this.glfsr_d.next_state();
  75.             int bit_c = this.glfsr_c.next_state();
  76.  
  77.             if(bit_c != 0)
  78.             {
  79.                 int bit_r = bit_d;
  80.                 byte_ = byte_ | (bit_r << bitpos);
  81.  
  82.                 bitpos = bitpos - 1;
  83.  
  84.                 if(bitpos < 0)
  85.                 {
  86.                     break;
  87.                 }
  88.             }
  89.         }
  90.        
  91.         return byte_;
  92.     }
  93. }
  94.  
  95. //# ----------------------------------------------------------------------------
  96. //# Crypto4o functions end here
  97. //# ----------------------------------------------------------------------------
  98.  
  99. class test
  100. {
  101.     static void Main(String[] args)
  102.     {
  103.         String s;
  104.         char random_ch;
  105.         char input_ch;
  106.        
  107.         sprng prng = new sprng(
  108.                     Convert.ToInt32(args[3], 16),
  109.                     Convert.ToInt32(args[4], 16),
  110.                     Convert.ToInt32(args[5], 16),
  111.                     Convert.ToInt32(args[6], 16)
  112.                     );
  113.  
  114.         s = String.Format("GLFSR D0: using polynom 0x%X, initial value: 0x%X.", Convert.ToInt32(args[3], 16), Convert.ToInt32(args[4], 16));
  115.         Console.WriteLine(s);
  116.        
  117.         s = String.Format("GLFSR C0: using polynom 0x%X, initial value: 0x%X.", Convert.ToInt32(args[5], 16), Convert.ToInt32(args[6], 16));
  118.         Console.Write(s);
  119.  
  120.         StreamReader f = new StreamReader(args[1]);
  121.         StreamWriter g = new StreamWriter(args[2]);
  122.        
  123.         while(!f.EndOfStream)
  124.         {
  125.             input_ch = (char)f.Read();
  126.  
  127.             random_ch = (char)(prng.next_byte() & 0xff);
  128.             g.Write((char)((int)input_ch ^ random_ch));
  129.         }
  130.     }
  131. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement