Beispiel #1
0
 public static void Main(string[] args)
 {
     var Sample = new Integer ();
     Sample += new Integer(555);
     Sample = Integer.Pow (Sample, 2);
     var output = Sample.ToBytes ();
     var Test = new Integer (output);
     Console.WriteLine (Sample.ToString ());
     Console.WriteLine ();
     Console.WriteLine (Test.ToString ());
     if (Sample.IsEqual(Test))
         Console.WriteLine ("Success");
     else
         Console.WriteLine ("Failed");
     Console.ReadLine ();
 }
 Integer L_GeneratePrime()
 {
     int limit = bytes;
     if (limit < 4)
         limit = 4;
     var raw = new byte[limit];
     RandomGenerator.NextBytes (raw);
     var newInt = new Integer (raw);
     return newInt;
 }
        /// <summary>
        /// Generates the key after a response is received.
        /// </summary>
        /// <param name="response">The string representation of the response.</param>
        public void HandleResponse(byte[] response)
        {
            var currentPlatform = OSCheck.RunningPlatform ();
            if (currentPlatform == OSCheck.Platform.Mac) {
                throw new PlatformNotSupportedException ("Mac OSX is not a supported operating system for this.");
            }
            if (currentPlatform == OSCheck.Platform.Windows) {
                var B = new mpz_t (response, 1);

                S = B.Power (a);
                S = (S as mpz_t).Mod ((p as mpz_t));
                W_GetKeyData ();
            } else if (currentPlatform == OSCheck.Platform.Linux) {
                var B = new Integer (response);
                S = B.Pow (a);
                S %= p;
                L_GetKeyData ();
            }
        }
        /// <summary>
        /// Generate a response packet.
        /// </summary>
        /// <param name="request">The string representation of the request.</param>
        /// <returns></returns>
        public byte[] GenerateResponse(byte[] request)
        {
            var currentPlatform = OSCheck.RunningPlatform ();
            if (currentPlatform == OSCheck.Platform.Mac) {
                throw new PlatformNotSupportedException ("Mac OSX is not a supported operating system for this.");
            }

            var instream = new MemoryStream (request);
            var gData = DataUtility.ReadBytesFromStream (instream);
            var pData = DataUtility.ReadBytesFromStream (instream);
            var AData = DataUtility.ReadBytesFromStream (instream);
            byte[] BData = null;

            if (currentPlatform == OSCheck.Platform.Windows) {
                g = new mpz_t (gData, 1);
                p = new mpz_t (pData, 1);
                var A = new mpz_t (AData, 1);

                // Generate the parameters.
                a = RandomGenerator.Next (bytes);
                var B = g.Power (a);
                B = B.Mod (p);

                // Get Raw IntX Data
                BData = B.ToByteArray (1);

                // Got the key!!! HOORAY!
                S = A.Power (a);
                S = S.Mod (p);
                W_GetKeyData ();
            } else if (currentPlatform == OSCheck.Platform.Linux) {
                g = new Integer ();
                g.FromBytes (gData);
                p = new Integer ();
                p.FromBytes (pData);
                var A = new Integer (AData);

                // Generate the parameters.
                a = RandomGenerator.Next (bytes);
                var B = g.Pow (a);
                B %= p;

                // Get Raw IntX Data
                BData = B.ToBytes ();

                // Got the key!!! HOORAY!
                S = A.Pow (a);
                S %= p;
                L_GetKeyData ();
            }
            return BData;
        }
Beispiel #5
0
 public static Integer operator *(Integer op1, Integer op2)
 {
     var result = new Integer();
     mpz_mul(ref result._pointer, ref op1._pointer, ref op2._pointer);
     return result;
 }
Beispiel #6
0
 public static Integer operator /(Integer g1, Integer g2)
 {
     var result = new Integer();
     mpz_tdiv_q(ref result._pointer, ref g1._pointer, ref g2._pointer);
     return result;
 }
Beispiel #7
0
 public Integer PowMod(Integer exp, Integer mod)
 {
     var result = new Integer();
     mpz_powm(ref result._pointer, ref this._pointer, ref exp._pointer, ref mod._pointer);
     return result;
 }
Beispiel #8
0
 public static Integer operator %(Integer op1, Integer mod)
 {
     Integer result = new Integer();
     mpz_mod(ref result._pointer, ref op1._pointer, ref mod._pointer);
     return result;
 }
Beispiel #9
0
 public static Integer Sqrt(Integer i)
 {
     var result = new Integer();
     mpz_sqrt(ref result._pointer, ref i._pointer);
     return result;
 }
Beispiel #10
0
 public bool IsEqual(Integer op1)
 {
     return mpz_cmp(ref op1._pointer, ref _pointer) == 0;
 }
Beispiel #11
0
 public static Integer Pow(uint bas, uint exp)
 {
     var result = new Integer();
     mpz_ui_pow_ui(ref result._pointer, bas, exp);
     return result;
 }
Beispiel #12
0
 public static Integer Pow(Integer i, int j)
 {
     if (j >= 0) return Pow(i, (uint) j);
     throw new ArgumentOutOfRangeException("j");
 }
Beispiel #13
0
 public static Integer Pow(Integer i, uint j)
 {
     var result = new Integer();
     mpz_pow_ui(ref result._pointer, ref i._pointer, j);
     return result;
 }
Beispiel #14
0
 public static bool IsEqual(Integer op1, Integer op2)
 {
     return mpz_cmp(ref op1._pointer, ref op2._pointer) == 0;
 }
Beispiel #15
0
 public static int InverseModulo(Integer result, Integer op1, Integer op2)
 {
     return mpz_invert(ref result._pointer, ref op1._pointer, ref op2._pointer);
 }