Inheritance: IAsymmetricBlockCipher
示例#1
0
文件: AWiz.cs 项目: burstas/rmps
 private void CreateAcryptoEngine()
 {
     IAsymmetricBlockCipher engine;
     switch (_MSec.Algorithm)
     {
         case ESec.ACRYPTO_ELGAMAL:
             engine = new ElGamalEngine();
             break;
         case ESec.ACRYPTO_NACCACHESTERN:
             engine = new NaccacheSternEngine();
             break;
         case ESec.ACRYPTO_RSABLINDED:
             engine = new RsaBlindedEngine();
             break;
         case ESec.ACRYPTO_RSABLINDING:
             engine = new RsaBlindingEngine();
             break;
         case ESec.ACRYPTO_RSA:
             engine = new RsaEngine();
             break;
     }
 }
        public virtual byte[] ProcessBlock(byte[] inBytes, int inOff, int length)
        {
            if (this.key == null)
            {
                throw new InvalidOperationException("NaccacheStern engine not initialised");
            }
            if (length > this.GetInputBlockSize() + 1)
            {
                throw new DataLengthException("input too large for Naccache-Stern cipher.\n");
            }
            if (!this.forEncryption && length < this.GetInputBlockSize())
            {
                throw new InvalidCipherTextException("BlockLength does not match modulus for Naccache-Stern cipher.\n");
            }
            BigInteger bigInteger = new BigInteger(1, inBytes, inOff, length);

            if (this.debug)
            {
                Console.WriteLine("input as BigInteger: " + bigInteger);
            }
            byte[] result;
            if (this.forEncryption)
            {
                result = this.Encrypt(bigInteger);
            }
            else
            {
                IList list = Platform.CreateArrayList();
                NaccacheSternPrivateKeyParameters naccacheSternPrivateKeyParameters = (NaccacheSternPrivateKeyParameters)this.key;
                IList smallPrimesList = naccacheSternPrivateKeyParameters.SmallPrimesList;
                for (int i = 0; i < smallPrimesList.Count; i++)
                {
                    BigInteger bigInteger2 = bigInteger.ModPow(naccacheSternPrivateKeyParameters.PhiN.Divide((BigInteger)smallPrimesList[i]), naccacheSternPrivateKeyParameters.Modulus);
                    IList      list2       = this.lookup[i];
                    if (this.lookup[i].Count != ((BigInteger)smallPrimesList[i]).IntValue)
                    {
                        if (this.debug)
                        {
                            Console.WriteLine(string.Concat(new object[]
                            {
                                "Prime is ",
                                smallPrimesList[i],
                                ", lookup table has size ",
                                list2.Count
                            }));
                        }
                        throw new InvalidCipherTextException(string.Concat(new object[]
                        {
                            "Error in lookup Array for ",
                            ((BigInteger)smallPrimesList[i]).IntValue,
                            ": Size mismatch. Expected ArrayList with length ",
                            ((BigInteger)smallPrimesList[i]).IntValue,
                            " but found ArrayList of length ",
                            this.lookup[i].Count
                        }));
                    }
                    int num = list2.IndexOf(bigInteger2);
                    if (num == -1)
                    {
                        if (this.debug)
                        {
                            Console.WriteLine("Actual prime is " + smallPrimesList[i]);
                            Console.WriteLine("Decrypted value is " + bigInteger2);
                            Console.WriteLine(string.Concat(new object[]
                            {
                                "LookupList for ",
                                smallPrimesList[i],
                                " with size ",
                                this.lookup[i].Count,
                                " is: "
                            }));
                            for (int j = 0; j < this.lookup[i].Count; j++)
                            {
                                Console.WriteLine(this.lookup[i][j]);
                            }
                        }
                        throw new InvalidCipherTextException("Lookup failed");
                    }
                    list.Add(BigInteger.ValueOf((long)num));
                }
                BigInteger bigInteger3 = NaccacheSternEngine.chineseRemainder(list, smallPrimesList);
                result = bigInteger3.ToByteArray();
            }
            return(result);
        }