Exemple #1
0
		/*
		* Constructor, that takes the arguments appropriate for
		* processing the command line directives.
		*/
		public DesExample(
			string	infile,
			string	outfile,
			string	keyfile,
			bool	encrypt)
		{
			/* 
			* First, determine that infile & keyfile exist as appropriate.
			*
			* This will also create the BufferedInputStream as required
			* for reading the input file.  All input files are treated
			* as if they are binary, even if they contain text, it's the
			* bytes that are encrypted.
			*/
			this.encrypt = encrypt;
			try
			{
				inStr = File.OpenRead(infile);
			}
			catch (FileNotFoundException e)
			{
				Console.Error.WriteLine("Input file not found ["+infile+"]");
				throw e;
			}

			try
			{
				outStr = File.Create(outfile);
			}
			catch (IOException e)
			{
				Console.Error.WriteLine("Output file not created ["+outfile+"]");
				throw e;
			}

			if (encrypt)
			{
				try
				{
					/*
					* The process of creating a new key requires a 
					* number of steps.
					*
					* First, create the parameters for the key generator
					* which are a secure random number generator, and
					* the length of the key (in bits).
					*/
					SecureRandom sr = new SecureRandom();

					KeyGenerationParameters kgp = new KeyGenerationParameters(
						sr, 
						DesEdeParameters.DesEdeKeyLength * 8);

					/*
					* Second, initialise the key generator with the parameters
					*/
					DesEdeKeyGenerator kg = new DesEdeKeyGenerator();
					kg.Init(kgp);

					/*
					* Third, and finally, generate the key
					*/
					key = kg.GenerateKey();

					/*
					* We can now output the key to the file, but first
					* hex Encode the key so that we can have a look
					* at it with a text editor if we so desire
					*/
					using (Stream keystream = File.Create(keyfile))
					{
						Hex.Encode(key, keystream);
					}
				}
				catch (IOException e)
				{
					Console.Error.WriteLine("Could not decryption create key file "+
										"["+keyfile+"]");
					throw e;
				}
			}
			else
			{
				try
				{
					// TODO This block is a bit dodgy

					// read the key, and Decode from hex encoding
					Stream keystream = File.OpenRead(keyfile);
//					int len = keystream.available();
					int len = (int) keystream.Length;
					byte[] keyhex = new byte[len];
					keystream.Read(keyhex, 0, len);
					key = Hex.Decode(keyhex);
				}
				catch (IOException e)
				{
					Console.Error.WriteLine("Decryption key file not found, "
						+ "or not valid ["+keyfile+"]");
					throw e;
				}
			}
		}
Exemple #2
0
		public override void PerformTest()
		{
			base.PerformTest();

			byte[] kek1 = Hex.Decode("255e0d1c07b646dfb3134cc843ba8aa71f025b7c0838251f");
			byte[] iv1 = Hex.Decode("5dd4cbfc96f5453b");
			byte[] in1 = Hex.Decode("2923bf85e06dd6ae529149f1f1bae9eab3a7da3d860d3e98");
			byte[] out1 = Hex.Decode("690107618ef092b3b48ca1796b234ae9fa33ebb4159604037db5d6a84eb3aac2768c632775a467d4");

			WrapTest(1, kek1, iv1, in1, out1);

			//
			// key generation
			//
			SecureRandom random = new SecureRandom();
			DesEdeKeyGenerator keyGen = new DesEdeKeyGenerator();

			keyGen.Init(new KeyGenerationParameters(random, 112));

			byte[] kB = keyGen.GenerateKey();
	        
			if (kB.Length != 16)
			{
				Fail("112 bit key wrong length.");
			}
	        
			keyGen.Init(new KeyGenerationParameters(random, 168));
	        
			kB = keyGen.GenerateKey();
	        
			if (kB.Length != 24)
			{
				Fail("168 bit key wrong length.");
			}
	        
			try
			{
				keyGen.Init(new KeyGenerationParameters(random, 200));
	            
				Fail("invalid key length not detected.");
			}
			catch (ArgumentException)
			{
				// expected
			}

			try
			{
				DesEdeParameters.IsWeakKey(new byte[4], 0);
				Fail("no exception on small key");
			}
			catch (ArgumentException e)
			{
				if (!e.Message.Equals("key material too short."))
				{
					Fail("wrong exception");
				}
			}

			try
			{
				new DesEdeParameters(weakKey);
				Fail("no exception on weak key");
			}
			catch (ArgumentException e)
			{
				if (!e.Message.Equals("attempt to create weak DESede key"))
				{
					Fail("wrong exception");
				}
			}
		}
Exemple #3
0
 /// <summary>
 /// The generate secret key.
 /// </summary>
 /// <returns>
 /// The <see cref="SecretKey"/>.
 /// </returns>
 public SecretKey GenerateSecretKey()
 {
     var kgp = new KeyGenerationParameters(new SecureRandom(), DesEdeParameters.DesEdeKeyLength * 8);
     var kg = new DesEdeKeyGenerator();
     kg.Init(kgp);
     var key = new SecretKey(kg.GenerateKey());
     return key;
 }