public virtual void Init(bool forEncryption, ICipherParameters parameters) { this.forEncryption = forEncryption; if (parameters is ParametersWithRandom) { parameters = ((ParametersWithRandom)parameters).Parameters; } key = (NaccacheSternKeyParameters)parameters; if (this.forEncryption) { return; } NaccacheSternPrivateKeyParameters naccacheSternPrivateKeyParameters = (NaccacheSternPrivateKeyParameters)key; IList smallPrimesList = naccacheSternPrivateKeyParameters.SmallPrimesList; lookup = new IList[smallPrimesList.Count]; for (int i = 0; i < smallPrimesList.Count; i++) { BigInteger bigInteger = (BigInteger)smallPrimesList[i]; int intValue = bigInteger.IntValue; lookup[i] = Platform.CreateArrayList(intValue); lookup[i].Add(BigInteger.One); BigInteger bigInteger2 = BigInteger.Zero; for (int j = 1; j < intValue; j++) { bigInteger2 = bigInteger2.Add(naccacheSternPrivateKeyParameters.PhiN); BigInteger e = bigInteger2.Divide(bigInteger); lookup[i].Add(naccacheSternPrivateKeyParameters.G.ModPow(e, naccacheSternPrivateKeyParameters.Modulus)); } } }
/** * Initializes this algorithm. Must be called before all other Functions. * * @see org.bouncycastle.crypto.AsymmetricBlockCipher#init(bool, * org.bouncycastle.crypto.CipherParameters) */ public void Init( bool forEncryption, ICipherParameters parameters) { this.forEncryption = forEncryption; if (parameters is ParametersWithRandom) { parameters = ((ParametersWithRandom)parameters).Parameters; } key = (NaccacheSternKeyParameters)parameters; // construct lookup table for faster decryption if necessary if (!this.forEncryption) { #if !NETFX_CORE if (debug) { Console.WriteLine("Constructing lookup Array"); } #endif NaccacheSternPrivateKeyParameters priv = (NaccacheSternPrivateKeyParameters)key; IList primes = priv.SmallPrimesList; lookup = new IList[primes.Count]; for (int i = 0; i < primes.Count; i++) { IBigInteger actualPrime = (BigInteger)primes[i]; int actualPrimeValue = actualPrime.IntValue; lookup[i] = Platform.CreateArrayList(actualPrimeValue); lookup[i].Add(BigInteger.One); #if !NETFX_CORE if (debug) { Console.WriteLine("Constructing lookup ArrayList for " + actualPrimeValue); } #endif IBigInteger accJ = BigInteger.Zero; for (int j = 1; j < actualPrimeValue; j++) { // IBigInteger bigJ = BigInteger.ValueOf(j); // accJ = priv.PhiN.Multiply(bigJ); accJ = accJ.Add(priv.PhiN); IBigInteger comp = accJ.Divide(actualPrime); lookup[i].Add(priv.G.ModPow(comp, priv.Modulus)); } } } }
/** * Initializes this algorithm. Must be called before all other Functions. * * @see Org.BouncyCastle.crypto.AsymmetricBlockCipher#init(bool, * Org.BouncyCastle.crypto.CipherParameters) */ public void Init( bool forEncryption, ICipherParameters parameters) { this.forEncryption = forEncryption; key = (NaccacheSternKeyParameters)parameters; // construct lookup table for faster decryption if necessary if (!this.forEncryption) { if (debug) { Console.WriteLine("Constructing lookup Array"); } NaccacheSternPrivateKeyParameters priv = (NaccacheSternPrivateKeyParameters)key; ArrayList primes = priv.SmallPrimes; lookup = new ArrayList[primes.Count]; for (int i = 0; i < primes.Count; i++) { BigInteger actualPrime = (BigInteger)primes[i]; int actualPrimeValue = actualPrime.IntValue; lookup[i] = new ArrayList(actualPrimeValue); lookup[i].Add(BigInteger.One); if (debug) { Console.WriteLine("Constructing lookup ArrayList for " + actualPrimeValue); } BigInteger accJ = BigInteger.Zero; for (int j = 1; j < actualPrimeValue; j++) { // BigInteger bigJ = BigInteger.ValueOf(j); // accJ = priv.PhiN.Multiply(bigJ); accJ = accJ.Add(priv.PhiN); BigInteger comp = accJ.Divide(actualPrime); lookup[i].Add(priv.G.ModPow(comp, priv.Modulus)); } } } }
/** * Initializes this algorithm. Must be called before all other Functions. * * @see org.bouncycastle.crypto.AsymmetricBlockCipher#init(bool, * org.bouncycastle.crypto.CipherParameters) */ public virtual void Init( bool forEncryption, ICipherParameters parameters) { this.forEncryption = forEncryption; if (parameters is ParametersWithRandom) { parameters = ((ParametersWithRandom)parameters).Parameters; } key = (NaccacheSternKeyParameters)parameters; // construct lookup table for faster decryption if necessary if (!this.forEncryption) { NaccacheSternPrivateKeyParameters priv = (NaccacheSternPrivateKeyParameters)key; IList primes = priv.SmallPrimesList; lookup = new IList[primes.Count]; for (int i = 0; i < primes.Count; i++) { BigInteger actualPrime = (BigInteger)primes[i]; int actualPrimeValue = actualPrime.IntValue; lookup[i] = BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities.Platform.CreateArrayList(actualPrimeValue); lookup[i].Add(BigInteger.One); BigInteger accJ = BigInteger.Zero; for (int j = 1; j < actualPrimeValue; j++) { // BigInteger bigJ = BigInteger.ValueOf(j); // accJ = priv.PhiN.Multiply(bigJ); accJ = accJ.Add(priv.PhiN); BigInteger comp = accJ.Divide(actualPrime); lookup[i].Add(priv.G.ModPow(comp, priv.Modulus)); } } } }
public override void PerformTest() { // Test with given key from NaccacheSternPaper (totally insecure) NaccacheSternKeyParameters pubParameters = new NaccacheSternKeyParameters(false, g, n, sigma.BitLength); NaccacheSternPrivateKeyParameters privParameters = new NaccacheSternPrivateKeyParameters(g, n, sigma .BitLength, smallPrimes, phi_n); AsymmetricCipherKeyPair pair = new AsymmetricCipherKeyPair(pubParameters, privParameters); // Initialize Engines with KeyPair if (debug) { Console.WriteLine("initializing encryption engine"); } cryptEng.Init(true, pair.Public); if (debug) { Console.WriteLine("initializing decryption engine"); } decryptEng.Init(false, pair.Private); byte[] data = paperTest.ToByteArray(); if (!new BigInteger(data).Equals(new BigInteger(enDeCrypt(data)))) { Fail("failed NaccacheStern paper test"); } // // key generation test // // // 768 Bit test // if (debug) { Console.WriteLine(); Console.WriteLine("768 Bit TEST"); } // specify key generation parameters NaccacheSternKeyGenerationParameters genParam = new NaccacheSternKeyGenerationParameters(new SecureRandom(), 768, 8, 30); // Initialize Key generator and generate key pair NaccacheSternKeyPairGenerator pGen = new NaccacheSternKeyPairGenerator(); pGen.Init(genParam); pair = pGen.GenerateKeyPair(); if (((NaccacheSternKeyParameters)pair.Public).Modulus.BitLength < 768) { Console.WriteLine("FAILED: key size is <786 bit, exactly " + ((NaccacheSternKeyParameters)pair.Public).Modulus.BitLength + " bit"); Fail("failed key generation (768) length test"); } // Initialize Engines with KeyPair if (debug) { Console.WriteLine("initializing " + genParam.Strength + " bit encryption engine"); } cryptEng.Init(true, pair.Public); if (debug) { Console.WriteLine("initializing " + genParam.Strength + " bit decryption engine"); } decryptEng.Init(false, pair.Private); // Basic data input data = Hex.Decode(input); if (!new BigInteger(1, data).Equals(new BigInteger(1, enDeCrypt(data)))) { Fail("failed encryption decryption (" + genParam.Strength + ") basic test"); } // Data starting with FF byte (would be interpreted as negative // BigInteger) data = Hex.Decode(edgeInput); if (!new BigInteger(1, data).Equals(new BigInteger(1, enDeCrypt(data)))) { Fail("failed encryption decryption (" + genParam.Strength + ") edgeInput test"); } // // 1024 Bit Test // /* * if (debug) * { * Console.WriteLine(); * Console.WriteLine("1024 Bit TEST"); * } * * // specify key generation parameters * genParam = new NaccacheSternKeyGenerationParameters(new SecureRandom(), 1024, 8, 40, debug); * * pGen.Init(genParam); * pair = pGen.generateKeyPair(); * * if (((NaccacheSternKeyParameters)pair.Public).getModulus().bitLength() < 1024) * { * if (debug) * { * Console.WriteLine("FAILED: key size is <1024 bit, exactly " + ((NaccacheSternKeyParameters)pair.Public).getModulus().bitLength() + " bit"); + } + Fail("failed key generation (1024) length test"); + } + + // Initialize Engines with KeyPair + + if (debug) + { + Console.WriteLine("initializing " + genParam.getStrength() + " bit encryption engine"); + } + cryptEng.Init(true, pair.Public); + + if (debug) + { + Console.WriteLine("initializing " + genParam.getStrength() + " bit decryption engine"); + } + decryptEng.Init(false, pair.Private); + + if (debug) + { + Console.WriteLine("Data is " + new BigInteger(1, data)); + } + + // Basic data input + data = Hex.Decode(input); + + if (!new BigInteger(1, data).Equals(new BigInteger(1, enDeCrypt(data)))) + { + Fail("failed encryption decryption (" + genParam.getStrength() + ") basic test"); + } + + // Data starting with FF byte (would be interpreted as negative + // BigInteger) + + data = Hex.Decode(edgeInput); + + if (!new BigInteger(1, data).Equals(new BigInteger(1, enDeCrypt(data)))) + { + Fail("failed encryption decryption (" + genParam.getStrength() + ") edgeInput test"); + } */ // END OF TEST CASE try { new NaccacheSternEngine().ProcessBlock(new byte[] { 1 }, 0, 1); Fail("failed initialisation check"); } catch (InvalidOperationException) { // expected } if (debug) { Console.WriteLine("All tests successful"); } }