Org.BouncyCastle.Crypto.Engines.RC2WrapEngine.Init C# (CSharp) Метод

Init() публичный Метод

public Init ( bool forWrapping, ICipherParameters parameters ) : void
forWrapping bool
parameters ICipherParameters
Результат void
		public void Init(
			bool				forWrapping,
			ICipherParameters	parameters)
		{
			this.forWrapping = forWrapping;
			this.engine = new CbcBlockCipher(new RC2Engine());

			if (parameters is ParametersWithRandom)
			{
				ParametersWithRandom pWithR = (ParametersWithRandom)parameters;
				sr = pWithR.Random;
				parameters = pWithR.Parameters;
			}
			else
			{
				sr = new SecureRandom();
			}

			if (parameters is ParametersWithIV)
			{
				if (!forWrapping)
					throw new ArgumentException("You should not supply an IV for unwrapping");

				this.paramPlusIV = (ParametersWithIV)parameters;
				this.iv = this.paramPlusIV.GetIV();
				this.parameters = this.paramPlusIV.Parameters;

				if (this.iv.Length != 8)
					throw new ArgumentException("IV is not 8 octets");
			}
			else
			{
				this.parameters = parameters;

				if (this.forWrapping)
				{
					// Hm, we have no IV but we want to wrap ?!?
					// well, then we have to create our own IV.
					this.iv = new byte[8];
					sr.NextBytes(iv);
					this.paramPlusIV = new ParametersWithIV(this.parameters, this.iv);
				}
			}
		}

Usage Example

Пример #1
0
		private ITestResult wrapTest(
			int     id,
			ICipherParameters paramsWrap,
			ICipherParameters paramsUnwrap,
			byte[]  inBytes,
			byte[]  outBytes)
		{
			IWrapper wrapper = new RC2WrapEngine();

			wrapper.Init(true, paramsWrap);

			try
			{
				byte[]  cText = wrapper.Wrap(inBytes, 0, inBytes.Length);
				if (!Arrays.AreEqual(cText, outBytes))
				{
					return new SimpleTestResult(false, Name + ": failed wrap test " + id
						+ " expected " + Hex.ToHexString(outBytes)
						+ " got " + Hex.ToHexString(cText));
				}
			}
			catch (Exception e)
			{
				return new SimpleTestResult(false, Name + ": failed wrap test exception " + e, e);
			}

			wrapper.Init(false, paramsUnwrap);

			try
			{
				byte[]  pText = wrapper.Unwrap(outBytes, 0, outBytes.Length);
				if (!Arrays.AreEqual(pText, inBytes))
				{
					return new SimpleTestResult(false, Name + ": failed unwrap test " + id
						+ " expected " + Hex.ToHexString(inBytes)
						+ " got " + Hex.ToHexString(pText));
				}
			}
			catch (Exception e)
			{
				return new SimpleTestResult(false, Name + ": failed unwrap test exception " + e, e);
			}

			return new SimpleTestResult(true, Name + ": Okay");
		}