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)); } } } }
public virtual byte[] ProcessBlock(byte[] inBytes, int inOff, int length) { //IL_000d: Unknown result type (might be due to invalid IL or missing references) if (key == null) { throw new InvalidOperationException("NaccacheStern engine not initialised"); } if (length > GetInputBlockSize() + 1) { throw new DataLengthException("input too large for Naccache-Stern cipher.\n"); } if (!forEncryption && length < GetInputBlockSize()) { throw new InvalidCipherTextException("BlockLength does not match modulus for Naccache-Stern cipher.\n"); } BigInteger bigInteger = new BigInteger(1, inBytes, inOff, length); if (forEncryption) { return(Encrypt(bigInteger)); } global::System.Collections.IList list = Platform.CreateArrayList(); NaccacheSternPrivateKeyParameters naccacheSternPrivateKeyParameters = (NaccacheSternPrivateKeyParameters)key; global::System.Collections.IList smallPrimesList = naccacheSternPrivateKeyParameters.SmallPrimesList; for (int i = 0; i < ((global::System.Collections.ICollection)smallPrimesList).get_Count(); i++) { BigInteger bigInteger2 = bigInteger.ModPow(naccacheSternPrivateKeyParameters.PhiN.Divide((BigInteger)smallPrimesList.get_Item(i)), naccacheSternPrivateKeyParameters.Modulus); global::System.Collections.IList list2 = lookup[i]; if (((global::System.Collections.ICollection)lookup[i]).get_Count() != ((BigInteger)smallPrimesList.get_Item(i)).IntValue) { throw new InvalidCipherTextException(string.Concat(new object[6] { "Error in lookup Array for ", ((BigInteger)smallPrimesList.get_Item(i)).IntValue, ": Size mismatch. Expected ArrayList with length ", ((BigInteger)smallPrimesList.get_Item(i)).IntValue, " but found ArrayList of length ", ((global::System.Collections.ICollection)lookup[i]).get_Count() })); } int num = list2.IndexOf((object)bigInteger2); if (num == -1) { throw new InvalidCipherTextException("Lookup failed"); } list.Add((object)BigInteger.ValueOf(num)); } BigInteger bigInteger3 = chineseRemainder(list, smallPrimesList); return(bigInteger3.ToByteArray()); }
public virtual byte[] ProcessBlock(byte[] inBytes, int inOff, int length) { if (key == null) { throw new InvalidOperationException("NaccacheStern engine not initialised"); } if (length > GetInputBlockSize() + 1) { throw new DataLengthException("input too large for Naccache-Stern cipher.\n"); } if (!forEncryption && length < GetInputBlockSize()) { throw new InvalidCipherTextException("BlockLength does not match modulus for Naccache-Stern cipher.\n"); } BigInteger bigInteger = new BigInteger(1, inBytes, inOff, length); if (forEncryption) { return(Encrypt(bigInteger)); } IList list = Platform.CreateArrayList(); NaccacheSternPrivateKeyParameters naccacheSternPrivateKeyParameters = (NaccacheSternPrivateKeyParameters)key; IList smallPrimesList = naccacheSternPrivateKeyParameters.SmallPrimesList; for (int i = 0; i < smallPrimesList.Count; i++) { BigInteger value = bigInteger.ModPow(naccacheSternPrivateKeyParameters.PhiN.Divide((BigInteger)smallPrimesList[i]), naccacheSternPrivateKeyParameters.Modulus); IList list2 = lookup[i]; if (lookup[i].Count != ((BigInteger)smallPrimesList[i]).IntValue) { throw new InvalidCipherTextException("Error in lookup Array for " + ((BigInteger)smallPrimesList[i]).IntValue + ": Size mismatch. Expected ArrayList with length " + ((BigInteger)smallPrimesList[i]).IntValue + " but found ArrayList of length " + lookup[i].Count); } int num = list2.IndexOf(value); if (num == -1) { throw new InvalidCipherTextException("Lookup failed"); } list.Add(BigInteger.ValueOf(num)); } BigInteger bigInteger2 = chineseRemainder(list, smallPrimesList); return(bigInteger2.ToByteArray()); }
/** * 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)); } } } }
/** * Process a single Block using the Naccache-Stern algorithm. * * @see org.bouncycastle.crypto.AsymmetricBlockCipher#ProcessBlock(byte[], * int, int) */ public byte[] ProcessBlock( byte[] inBytes, int inOff, int length) { if (key == null) { throw new InvalidOperationException("NaccacheStern engine not initialised"); } if (length > (GetInputBlockSize() + 1)) { throw new DataLengthException("input too large for Naccache-Stern cipher.\n"); } if (!forEncryption) { // At decryption make sure that we receive padded data blocks if (length < GetInputBlockSize()) { throw new InvalidCipherTextException("BlockLength does not match modulus for Naccache-Stern cipher.\n"); } } // transform input into BigInteger BigInteger input = new BigInteger(1, inBytes, inOff, length); if (debug) { Console.WriteLine("input as BigInteger: " + input); } byte[] output; if (forEncryption) { output = Encrypt(input); } else { IList plain = Platform.CreateArrayList(); NaccacheSternPrivateKeyParameters priv = (NaccacheSternPrivateKeyParameters)key; IList primes = priv.SmallPrimesList; // Get Chinese Remainders of CipherText for (int i = 0; i < primes.Count; i++) { BigInteger exp = input.ModPow(priv.PhiN.Divide((BigInteger)primes[i]), priv.Modulus); IList al = lookup[i]; if (lookup[i].Count != ((BigInteger)primes[i]).IntValue) { if (debug) { Console.WriteLine("Prime is " + primes[i] + ", lookup table has size " + al.Count); } throw new InvalidCipherTextException("Error in lookup Array for " + ((BigInteger)primes[i]).IntValue + ": Size mismatch. Expected ArrayList with length " + ((BigInteger)primes[i]).IntValue + " but found ArrayList of length " + lookup[i].Count); } int lookedup = al.IndexOf(exp); if (lookedup == -1) { if (debug) { Console.WriteLine("Actual prime is " + primes[i]); Console.WriteLine("Decrypted value is " + exp); Console.WriteLine("LookupList for " + primes[i] + " with size " + lookup[i].Count + " is: "); for (int j = 0; j < lookup[i].Count; j++) { Console.WriteLine(lookup[i][j]); } } throw new InvalidCipherTextException("Lookup failed"); } plain.Add(BigInteger.ValueOf(lookedup)); } BigInteger test = chineseRemainder(plain, primes); // Should not be used as an oracle, so reencrypt output to see // if it corresponds to input // this breaks probabilisic encryption, so disable it. Anyway, we do // use the first n primes for key generation, so it is pretty easy // to guess them. But as stated in the paper, this is not a security // breach. So we can just work with the correct sigma. // if (debug) { // Console.WriteLine("Decryption is " + test); // } // if ((key.G.ModPow(test, key.Modulus)).Equals(input)) { // output = test.ToByteArray(); // } else { // if(debug){ // Console.WriteLine("Engine seems to be used as an oracle, // returning null"); // } // output = null; // } output = test.ToByteArray(); } return(output); }
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"); } }
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); }