コード例 #1
0
 public byte[] Encrypt(byte[] en)
 {
     int[] scr = new int[en.Length];
     for (int i = 0; i < scr.Length; i++)
     {
         scr[i] = i;
     }
     for (int a = 0; a < scr.Length; ++a)
     {
         int b = cr.Next(0, scr.Length);
         if (b <= a)
         {
             continue;
         }
         int ch = scr[a];
         scr[a] = scr[b];
         scr[b] = ch;
     }
     byte[] retv = new byte[en.Length];
     for (int a = 0; a < en.Length; ++a)
     {
         retv[a] = en[scr[a]];
     }
     cr = new LegacyCrossRandom();
     return(retv);
 }
コード例 #2
0
 public LegacyAutokryptexEncryptor(String password, int MaxEncryptors = 6, int seed = 179426447)
 {
     cr = new LegacyCrossRandom();
     instancePassword = cr.CramString(password, 256);
     int[] args = new int[256];
     for (int i = 0; i < args.Length; i++)
     {
         args[i] = instancePassword[i] ^ cr.Next(Int32.MaxValue);
     }
     Init(args, MaxEncryptors);
 }
コード例 #3
0
        public byte[] CramString(String input, int digitCount)
        {
            LegacyCrossRandom cr = this;

            byte[] workset = Fi.StandardEncoding.GetBytes(input);
            while (workset.Count() > digitCount)
            {
                byte ch = workset[0];
                workset = workset.Skip(1).ToArray();
                workset[cr.Next(workset.Length)] = (byte)(workset[cr.Next(workset.Length)] ^ ch);
            }
            while (workset.Count() < digitCount)
            {
                var ws = new byte[workset.Count() + 1];
                workset.CopyTo(ws, 0);
                ws[ws.Count() - 1] = (byte)cr.Next(byte.MaxValue);
                workset            = ws;
            }

            return(workset);
        }
コード例 #4
0
        public void UseInstanceSecret(String secret)
        {
            var secretBytes      = CramStringPlus(secret, 16);
            LegacyCrossRandom cr = new LegacyCrossRandom();

            for (int i = 0; i < secretBytes.Length; i++)
            {
                if (i <= InstanceSecret.Length)
                {
                    InstanceSecret[i] = Primes[secretBytes[i] % Primes.Length] + cr.Next(Primes.Length ^ secretBytes[i]);
                }
            }
        }
コード例 #5
0
        private byte[] GenerateEnigmaStrip(byte[] Map)
        {
            int num = cr.Next(7) + 7;

            byte[] enigma = new byte[Map.Length];
            Map.CopyTo(enigma, 0);
            while (num-- > 0)
            {
                // Runs pseudorandom swapping positions for 7 to 14 times
                // Its not as good as Turing's, but it's fine I guess.
                for (int a = 0; a < Map.Length; ++a)
                {
                    int b = cr.Next(0, Map.Length);
                    if (b == a)
                    {
                        continue;
                    }
                    byte ch = enigma[a];
                    enigma[a] = enigma[b];
                    enigma[b] = ch;
                }
            }
            return(enigma);
        }
コード例 #6
0
        private void Init(int[] args, int maxEncryptors)
        {
            if (args.Length < 2)
            {
                throw new Exception("Crazy Locking Engine requires 2 or more integers as 'Key', preferably big primes");
            }
            var passBytes = instancePassword;

            Type[] availableMethods = new Type[] {
                typeof(BinaryBlur),
                typeof(BinaryNegativation),
                typeof(LegacyBinaryScramble),
                typeof(LegacyEnigmaEncryptor),
            };
            var pickedKeys = new Queue <int>();

            for (int i = 0; i < maxEncryptors; i++)
            {
                pickedKeys.Enqueue(args[cr.Next(args.Length - 1)]);
            }

            foreach (var a in pickedKeys)
            {
                var em    = availableMethods[cr.Next(availableMethods.Length)];
                var ctors = em.GetConstructors();
                if (ctors.Any(
                        c => c.GetParameters().Length == 1 &&
                        c.GetParameters()[0].ParameterType == typeof(int)
                        ))
                {
                    encryptor.Add(
                        (IEncryptionMethod)
                        Activator.CreateInstance(em,
                                                 new Object[] { a ^ cr.Next(Int32.MaxValue) }
                                                 )
                        );
                }
                else
                if (ctors.Any(
                        c => c.GetParameters().Length == 0
                        ))
                {
                    var inst =
                        (IEncryptionMethod)
                        Activator.CreateInstance(em);

                    passBytes = inst.Encrypt(passBytes);

                    if (MultiPassRandomEncoder && inst.GetType() != typeof(LegacyEnigmaEncryptor))
                    {
                        encryptor.Add(
                            inst
                            );
                    }
                }
            }

            while (encryptor.Count > maxEncryptors)
            {
                encryptor.RemoveAt(cr.Next(encryptor.Count));
            }
            encodedPassword = Convert.ToBase64String(passBytes);
            encryptor.Add(new LegacyAesEncryptor(Fi.StandardEncoding.GetString(instancePassword)));
        }