Inheritance: BufferedCipherBase
 private IBufferedCipher CreateRijndael(WinzipAesEncryptionData winzipAesEncryptionData)
 {
     var blockCipher = new BufferedBlockCipher(new RijndaelEngine());
     var param = new KeyParameter(winzipAesEncryptionData.KeyBytes);
     blockCipher.Init(true, param);
     return blockCipher;
 }
		public override void PerformTest()
		{
			BufferedBlockCipher cipher = new BufferedBlockCipher(engine);

			cipher.Init(true, param);

			byte[]  outBytes = new byte[input.Length];

			int len1 = cipher.ProcessBytes(input, 0, input.Length, outBytes, 0);

				cipher.DoFinal(outBytes, len1);

			if (!AreEqual(outBytes, output))
			{
				Fail("failed - " + "expected " + Hex.ToHexString(output) + " got " + Hex.ToHexString(outBytes));
			}

			cipher.Init(false, param);

			int len2 = cipher.ProcessBytes(output, 0, output.Length, outBytes, 0);

			cipher.DoFinal(outBytes, len2);

			if (!AreEqual(input, outBytes))
			{
				Fail("failed reversal got " + Hex.ToHexString(outBytes));
			}
		}
Esempio n. 3
0
        public override void ChangeMode(string mode)
        {
            switch (mode)
            {
                case "CBC":
                    _Engine = new CbcBlockCipher(_Engine);
                    break;
                case "CFB":
                    _Engine = new CfbBlockCipher(_Engine, 8);
                    break;
                case "GOFB":
                    _Engine = new GOfbBlockCipher(_Engine);
                    break;
                case "OFB":
                    _Engine = new OfbBlockCipher(_Engine, 8);
                    break;
                case "OpenPgpCFB":
                    _Engine = new OpenPgpCfbBlockCipher(_Engine);
                    break;
                case "SIC":
                    _Engine = new SicBlockCipher(_Engine);
                    break;
            }

            _Cm.CbPads.SelectedIndex = 0;
            _Cipher = null;
        }
Esempio n. 4
0
 public static Stream encryptStream(Stream stream)
 {
     BufferedBlockCipher output = new BufferedBlockCipher(new CfbBlockCipher(new AesFastEngine(), 8));
     output.Init(true, new ParametersWithIV(new KeyParameter(SecretKey), SecretKey, 0, 16));
     BufferedBlockCipher input = new BufferedBlockCipher(new CfbBlockCipher(new AesFastEngine(), 8));
     input.Init(false, new ParametersWithIV(new KeyParameter(SecretKey), SecretKey, 0, 16));
     return new CipherStream(stream, input, output);
 }
Esempio n. 5
0
		public Packet MessageEncrypt(ICipherSetRemoteInfo remoteInfo, Packet inner)
		{
			CS1ARemoteInfo ri = (CS1ARemoteInfo)remoteInfo;

			var agreedValue = ECDHAgree (ri.RemotePublicKey, ri.EphemeralKeys.PrivateKey);

			// Hash the agreed key
			var hashedValue = Helpers.SHA256Hash (Helpers.ToByteArray(agreedValue, 20));

			// Fold to get the actual key for AES
			byte[] aesKey = Helpers.Fold (hashedValue);
			Random rnd = new Random ();

			// Setup and encrypt the actual data
			byte[] aesIV = new byte[16];
			rnd.NextBytes (aesIV);
			Array.Clear (aesIV, 4, 12);

			var cipher = new SicBlockCipher (new AesFastEngine ());
			var parameters = new ParametersWithIV (new KeyParameter (aesKey), aesIV);
			cipher.Init (true, parameters);

			var encryptedInner = new byte[inner.FullPacket.Length];
			BufferedBlockCipher bufferCipher = new BufferedBlockCipher (cipher);
			var offset = bufferCipher.ProcessBytes (inner.FullPacket, encryptedInner, 0);
			bufferCipher.DoFinal (encryptedInner, offset);

			// Construct the packet minus the hmac
			Packet outPacket = new Packet ();
			outPacket.Body = new byte[29 + encryptedInner.Length];
			Buffer.BlockCopy (ri.EphemeralKeys.PublicKey, 0, outPacket.Body, 0, ri.EphemeralKeys.PublicKey.Length);
			Buffer.BlockCopy (aesIV, 0, outPacket.Body, 21, 4);
			Buffer.BlockCopy (encryptedInner, 0, outPacket.Body, 25, encryptedInner.Length);

			// ECDH for the hmac key using 
			var idAgreedValue = ECDHAgree (ri.RemotePublicKey, Key.PrivateKey);

			// Mash on the IV for the compound key
			byte[] macKey = new byte[24];
			byte[] idAgreedValueArray = Helpers.ToByteArray(idAgreedValue, 20);
			Buffer.BlockCopy(idAgreedValueArray, 0, macKey, 0, idAgreedValueArray.Length);
			Buffer.BlockCopy(aesIV, 0, macKey, idAgreedValueArray.Length, 4);

			// Actually hmac all the data now
			var hmac = new HMac (new Sha256Digest ());
			hmac.Init(new KeyParameter (macKey, 0, 24));
			hmac.BlockUpdate(outPacket.Body, 0, 25 + encryptedInner.Length);
			byte[] mac = new byte[hmac.GetMacSize()];
			hmac.DoFinal(mac, 0);

			// Fold it up, shove it in and we're done
			var foldedMac = Helpers.Fold(mac, 3);
			Buffer.BlockCopy(foldedMac, 0, outPacket.Body, 25 + encryptedInner.Length, foldedMac.Length);

			return outPacket;
		}
        public BouncyCastleAesStream(Stream stream, byte[] key)
        {
            _baseStream = stream;

            _encryptCipher = new BufferedBlockCipher(new CfbBlockCipher(new AesFastEngine(), 8));
            _encryptCipher.Init(true, new ParametersWithIV(new KeyParameter(key), key, 0, 16));

            _decryptCipher = new BufferedBlockCipher(new CfbBlockCipher(new AesFastEngine(), 8));
            _decryptCipher.Init(false, new ParametersWithIV(new KeyParameter(key), key, 0, 16));
        }
Esempio n. 7
0
        public BouncyCastleAes(ITCPClient tcp, byte[] key)
        {
            Stream = tcp.GetStream();

            EncryptCipher = new BufferedBlockCipher(new CfbBlockCipher(new AesFastEngine(), 8));
            EncryptCipher.Init(true, new ParametersWithIV(new KeyParameter(key), key, 0, 16));

            DecryptCipher = new BufferedBlockCipher(new CfbBlockCipher(new AesFastEngine(), 8));
            DecryptCipher.Init(false, new ParametersWithIV(new KeyParameter(key), key, 0, 16));
        }
Esempio n. 8
0
 public AesStream(Stream stream, byte[] key)
 {
     BaseStream = stream;
     Key = key;
     encryptCipher = new BufferedBlockCipher(new CfbBlockCipher(new AesFastEngine(), 8));
     encryptCipher.Init(true, new ParametersWithIV(
         new KeyParameter(key), key, 0, 16));
     decryptCipher = new BufferedBlockCipher(new CfbBlockCipher(new AesFastEngine(), 8));
     decryptCipher.Init(false, new ParametersWithIV(
         new KeyParameter(key), key, 0, 16));
 }
Esempio n. 9
0
        public AesStream(Stream stream, byte[] key)
        {
            BaseStream = stream;
            Key = key;
#if MONO
            encryptCipher = new BufferedBlockCipher(new CfbBlockCipher(new AesFastEngine(), 8));
            encryptCipher.Init(true, new ParametersWithIV(
                new KeyParameter(key), key, 0, 16));
            decryptCipher = new BufferedBlockCipher(new CfbBlockCipher(new AesFastEngine(), 8));
            decryptCipher.Init(false, new ParametersWithIV(
                new KeyParameter(key), key, 0, 16));
#else
            var rijndael = GenerateAES(key);
            var encryptTransform = rijndael.CreateEncryptor();
            var decryptTransform = rijndael.CreateDecryptor();
            encryptStream = new CryptoStream(BaseStream, encryptTransform, CryptoStreamMode.Write);
            decryptStream = new CryptoStream(BaseStream, decryptTransform, CryptoStreamMode.Read);
#endif
        }
Esempio n. 10
0
            public string GenerateActivationKey()
            {
                var engine = new BlowfishEngine();
                var cipher = new CbcBlockCipher(engine);
                var bbc = new BufferedBlockCipher(cipher);
                bbc.Init(true, new ParametersWithIV(new KeyParameter(Encoding.ASCII.GetBytes(EncKey)), IV));

                var n = Prng.Next(0, 999999);

                var s = String.Format("{0,6};YNAB4;;;;", n); // must be fixed length due to padding issue
                var sb = Encoding.ASCII.GetBytes(s);
                sb[sb.Length - 4] = 0x4; //
                sb[sb.Length - 3] = 0x4; // padding issue???
                sb[sb.Length - 2] = 0x4; // PCKS#5
                sb[sb.Length - 1] = 0x4; //
                var cipherText = new byte[bbc.GetOutputSize(sb.Length)];
                var outputLen = bbc.ProcessBytes(sb, 0, sb.Length, cipherText, 0);
                bbc.DoFinal(sb, outputLen);
                var encryptedLic = Base32.EncodeByteArray(cipherText);

                return encryptedLic;
            }
Esempio n. 11
0
        private static byte[] decrypt28147cfb(byte[] key, byte[] input)
        {
            ICipherParameters param = new ParametersWithIV(
                new ParametersWithSBox(
                    new KeyParameter(key), //key
                    Gost28147Engine.GetSBox("E-A")), //type S-box
                Hex.Decode("0000000000000000")); //IV

            byte[] output = new byte[input.Length];

            BufferedBlockCipher cipher = new BufferedBlockCipher(new CfbBlockCipher(new Gost28147Engine(), 64));
            cipher.Init(false, param);
            int len1 = cipher.ProcessBytes(input, 0, input.Length, output, 0);
            try
            {
                cipher.DoFinal(output, len1);
            }
            catch (CryptoException ex)
            {
                //MessageBox.Show("Error: " + ex.Message);
            }

            return output;
        }
		public override void PerformTest()
		{
			BufferedBlockCipher cipher = new BufferedBlockCipher(engine);

			cipher.Init(true, param);

			byte[]  outBytes = new byte[input.Length];

			Array.Copy(input, 0, outBytes, 0, outBytes.Length);

			for (int i = 0; i != iterations; i++)
			{
				int len1 = cipher.ProcessBytes(outBytes, 0, outBytes.Length, outBytes, 0);

				cipher.DoFinal(outBytes, len1);
			}

			if (!AreEqual(outBytes, output))
			{
				Fail("failed - " + "expected " + Hex.ToHexString(output) + " got " + Hex.ToHexString(outBytes));
			}

			cipher.Init(false, param);

			for (int i = 0; i != iterations; i++)
			{
				int len1 = cipher.ProcessBytes(outBytes, 0, outBytes.Length, outBytes, 0);

				cipher.DoFinal(outBytes, len1);
			}

			if (!AreEqual(input, outBytes))
			{
				Fail("failed reversal");
			}
		}
Esempio n. 13
0
        public override void Serialize(Stream aStream, object aObject)
        {
            /* check for required parameters */
              if (aStream == null) {
            throw new ArgumentNullException("aStream");
              }

              if (aObject == null) {
            throw new ArgumentNullException("aObject");
              }

              PasswordFinder pwFinder = null;
              if (GetPassphraseCallbackMethod != null) {
            pwFinder = new PasswordFinder(GetPassphraseCallbackMethod);
              }
              PinnedArray<char> passphrase = null;
              if (pwFinder != null) {
            passphrase = new PinnedArray<char>(0);
            passphrase.Data = pwFinder.GetPassword();
              }

              byte cipherType;
              if (passphrase == null || passphrase.Data.Length == 0) {
            cipherType = SSH_CIPHER_NONE;
              } else {
            cipherType = SSH_CIPHER_3DES;
              }

              BlobBuilder builder = new BlobBuilder();

              ISshKey sshKey = aObject as ISshKey;
              RsaKeyParameters publicKeyParams = sshKey.GetPublicKeyParameters()
            as RsaKeyParameters;
              RsaPrivateCrtKeyParameters privateKeyParams = sshKey.GetPrivateKeyParameters()
            as RsaPrivateCrtKeyParameters;

              /* writing info headers */
              builder.AddBytes(Encoding.ASCII.GetBytes(FILE_HEADER_LINE + "\n"));
              builder.AddByte(0);          //end of string
              builder.AddByte(cipherType); //cipher
              builder.AddInt(0);           //reserved

              /* writing public key */
              builder.AddInt(sshKey.Size);
              builder.AddSsh1BigIntBlob(publicKeyParams.Modulus);
              builder.AddSsh1BigIntBlob(publicKeyParams.Exponent);
              builder.AddStringBlob(sshKey.Comment);

              /* writing private key */
              BlobBuilder privateKeyBuilder = new BlobBuilder();

              /* adding some control values */
              Random random = new Random();
              byte[] resultCheck = new byte[2];
              random.NextBytes(resultCheck);

              privateKeyBuilder.AddByte(resultCheck[0]);
              privateKeyBuilder.AddByte(resultCheck[1]);
              privateKeyBuilder.AddByte(resultCheck[0]);
              privateKeyBuilder.AddByte(resultCheck[1]);
              privateKeyBuilder.AddSsh1BigIntBlob(privateKeyParams.Exponent);
              privateKeyBuilder.AddSsh1BigIntBlob(privateKeyParams.DQ);
              privateKeyBuilder.AddSsh1BigIntBlob(privateKeyParams.P);
              privateKeyBuilder.AddSsh1BigIntBlob(privateKeyParams.Q);

              if (cipherType == SSH_CIPHER_NONE) {
            /* plain-text */
            builder.AddBytes(privateKeyBuilder.GetBlobAsPinnedByteArray().Data);
              } else {
            byte[] keydata;
            using (MD5 md5 = MD5.Create()) {
              keydata = md5.ComputeHash(Encoding.ASCII.GetBytes(passphrase.Data));
            }

            /* encryption */
            DesSsh1Engine desEngine = new DesSsh1Engine();
            desEngine.Init(true, new KeyParameter(keydata));

            BufferedBlockCipher bufferedBlockCipher = new BufferedBlockCipher(desEngine);
            byte[] ouputBuffer = bufferedBlockCipher.ProcessBytes(
              privateKeyBuilder.GetBlobAsPinnedByteArray().Data);

            builder.AddBytes(ouputBuffer);

            passphrase.Dispose();
              }

              /* writing result to file */
              var builderOutput = builder.GetBlobAsPinnedByteArray();
              aStream.Write(builderOutput.Data, 0, builderOutput.Data.Length);
              aStream.Close();
        }
Esempio n. 14
0
		public Packet ChannelDecrypt(ICipherSetRemoteInfo channelInfo, Packet outer)
		{
			// We gotta have the primary components and something to decrypt
			if (outer.Body.Length < 25) {
				return null;
			}

			var ci = (CS1ARemoteInfo)channelInfo;

			// Rip apart our packet
			byte[] token = outer.Body.Take (16).ToArray ();
			byte[] iv = outer.Body.Skip (16).Take (4).ToArray ();
			byte[] encryptedData = outer.Body.Skip (20).Take (outer.Body.Length - 24).ToArray ();
			byte[] dataMac = outer.Body.Skip (outer.Body.Length - 4).Take (4).ToArray ();

			// Make sure we're on the right channel
			if (!token.SequenceEqual (ci.Token)) {
				return null;
			}

			// Validate us some hmac
			byte[] hmacKey = new byte[20];
			Buffer.BlockCopy (ci.DecryptionKey, 0, hmacKey, 0, 16);
			Buffer.BlockCopy (iv, 0, hmacKey, 16, 4);

			var hmac = new HMac (new Sha256Digest ());
			hmac.Init(new KeyParameter (hmacKey));
			hmac.BlockUpdate(encryptedData, 0, encryptedData.Length);
			byte[] mac = new byte[hmac.GetMacSize()];
			hmac.DoFinal(mac, 0);
			var foldedMac = Helpers.Fold (mac, 3);

			if (!foldedMac.SequenceEqual (dataMac)) {
				// Get out of here with your bad data
				return null;
			}

			// Everything seems ok.  Get it decrypted
			byte[] aesIV = new byte[16];
			Buffer.BlockCopy (iv, 0, aesIV, 0, 4);
			Array.Clear (aesIV, 4, 12);

			var cipher = new SicBlockCipher (new AesFastEngine ());
			var parameters = new ParametersWithIV (new KeyParameter (ci.DecryptionKey), aesIV);
			cipher.Init (false, parameters);

			var decryptedData = new byte[encryptedData.Length];
			BufferedBlockCipher bufferCipher = new BufferedBlockCipher (cipher);
			var offset = bufferCipher.ProcessBytes (encryptedData, decryptedData, 0);
			bufferCipher.DoFinal (decryptedData, offset);

			// Build a packet and ship it off
			return Packet.DecodePacket (decryptedData);
		}
Esempio n. 15
0
        private byte[] BouncyCastleCrypto(bool forEncrypt, byte[] input)
        {
            try
            {
                this.mode.Init(forEncrypt, new ParametersWithIV(new KeyParameter(key), iv));

                BufferedBlockCipher cipher = new BufferedBlockCipher(this.mode);

                return cipher.DoFinal(input);
            }
            catch (CryptoException)
            {
                throw;
            }
        }
Esempio n. 16
0
		public Packet ChannelEncrypt(ICipherSetRemoteInfo channelInfo, Packet inner)
		{
			var ci = (CS1ARemoteInfo)channelInfo;

			// TODO:  Validate we don't care about endianess of IV here

			// Setup and encrypt the actual data
			byte[] aesIV = new byte[16];
			Buffer.BlockCopy (BitConverter.GetBytes(ci.IV), 0, aesIV, 0, 4);
			Array.Clear (aesIV, 4, 12);

			var cipher = new SicBlockCipher (new AesFastEngine ());
			var parameters = new ParametersWithIV (new KeyParameter (ci.EncryptionKey), aesIV);
			cipher.Init (true, parameters);

			var encryptedInner = new byte[inner.FullPacket.Length];
			BufferedBlockCipher bufferCipher = new BufferedBlockCipher (cipher);
			var offset = bufferCipher.ProcessBytes (inner.FullPacket, encryptedInner, 0);
			bufferCipher.DoFinal (encryptedInner, offset);

			// Hmac the output
			byte[] hmacKey = new byte[20];
			Buffer.BlockCopy (ci.EncryptionKey, 0, hmacKey, 0, 16);
			Buffer.BlockCopy (BitConverter.GetBytes(ci.IV), 0, hmacKey, 16, 4);

			var hmac = new HMac (new Sha256Digest ());
			hmac.Init(new KeyParameter (hmacKey));
			hmac.BlockUpdate(encryptedInner, 0, encryptedInner.Length);
			byte[] mac = new byte[hmac.GetMacSize()];
			hmac.DoFinal(mac, 0);
			var foldedMac = Helpers.Fold (mac, 3);

			// Create the outgoing packet
			Packet outPacket = new Packet();
			outPacket.Body = new byte[encryptedInner.Length + 24];
			Buffer.BlockCopy(ci.Token, 0, outPacket.Body, 0, 16);
			Buffer.BlockCopy(BitConverter.GetBytes(ci.IV), 0, outPacket.Body, 16, 4);
			Buffer.BlockCopy(encryptedInner, 0, outPacket.Body, 20, encryptedInner.Length);
			Buffer.BlockCopy(foldedMac, 0, outPacket.Body, outPacket.Body.Length - 4, 4);

			// Next IV next packet
			++ci.IV;

			return outPacket;
		}
Esempio n. 17
0
		public Packet MessageDecrypt(Packet outer)
		{
			byte[] remoteKeyData = outer.Body.Take(21).ToArray();
			byte[] ivData = outer.Body.Skip(21).Take(4).ToArray();
			byte[] innerEncryptedData = outer.Body.Skip(25).Take(outer.Body.Length - 29).ToArray();

			// Decode the body
			ECKeyPair remoteEphemeralKeys = ECKeyPair.LoadKeys (SecNamedCurves.GetByName ("secp160r1"), remoteKeyData, null);

			var idAgreement = ECDHAgree (remoteEphemeralKeys.PublicKey, Key.PrivateKey);
			var agreedHash = Helpers.SHA256Hash (Helpers.ToByteArray (idAgreement, 20));
			var aesKey = Helpers.FoldOnce(agreedHash);

			// Pad out the IV
			byte[] aesIV = new byte[16];
			Array.Clear (aesIV, 0, 16);
			Buffer.BlockCopy (ivData, 0, aesIV, 0, 4);

			// Decrypt it
			var cipher = new BufferedBlockCipher (new SicBlockCipher (new AesFastEngine ()));
			var parameters = new ParametersWithIV (new KeyParameter (aesKey), aesIV);
			cipher.Init (false, parameters);
			byte[] decryptedBody = new byte[innerEncryptedData.Length];
			var offset = cipher.ProcessBytes (innerEncryptedData, decryptedBody, 0);
			cipher.DoFinal (decryptedBody, offset);

			Packet outPacket = Packet.DecodePacket (decryptedBody);

			return outPacket;
		}
        private byte[] DecryptBytes(byte[] p_key, byte[] p_data)
        {
            BufferedBlockCipher cipher = new BufferedBlockCipher(new BlowfishEngine());

            // init using the given key
            cipher.Init(false, new KeyParameter(p_key));

            // decrypt the given data
            return cipher.ProcessBytes(p_data);
        }
Esempio n. 19
0
        public override object Deserialize(Stream aStream)
        {
            /* check for required parameters */
              if (aStream == null) {
            throw new ArgumentNullException("aStream");
              }

              /* reading unencrypted part */
              BlobParser parser = new BlobParser(aStream);

              parser.ReadBytes((uint)FILE_HEADER_LINE.Length + 2);  //Skipping header line

              byte cipherType = parser.ReadByte();
              if (cipherType != SSH_CIPHER_3DES && cipherType != SSH_CIPHER_NONE) {
            //TripleDes is the only encryption supported
            throw new KeyFormatterException("Unsupported cypherType: " + cipherType);
              }

              parser.ReadInt(); //reserved

              /* reading public key */
              AsymmetricKeyParameter aPublicKeyParameter =
             parser.ReadSsh1PublicKeyData(false);
              String keyComment = parser.ReadString();

              /* reading private key */
              byte[] inputBuffer = new byte[aStream.Length];
              aStream.Read(inputBuffer, 0, inputBuffer.Length);
              byte[] ouputBuffer;

              try {
            if (cipherType == 3) {
              /* private key is 3DES encrypted */
              PasswordFinder pwFinder = null;
              if (GetPassphraseCallbackMethod != null) {
            pwFinder = new PasswordFinder(GetPassphraseCallbackMethod);
              }

              byte[] keydata;
              try {
            using (MD5 md5 = MD5.Create()) {
              char[] md5Buffer = pwFinder.GetPassword();
              keydata = md5.ComputeHash(Encoding.ASCII.GetBytes(md5Buffer));
            }
              } catch (PasswordException ex) {
            if (GetPassphraseCallbackMethod == null) {
              throw new CallbackNullException();
            }
            throw new KeyFormatterException("see inner exception", ex);
              }

              /* decryption */
              DesSsh1Engine desEngine = new DesSsh1Engine();
              desEngine.Init(false, new KeyParameter(keydata));

              BufferedBlockCipher bufferedBlockCipher = new BufferedBlockCipher(desEngine);
              ouputBuffer = bufferedBlockCipher.ProcessBytes(inputBuffer);

            } else {
              /* private key is stored in plain text */
              ouputBuffer = inputBuffer;
            }

            var privateKeyParser = new BlobParser(ouputBuffer);

            /* checking result of decryption */
            byte[] resultCheck = privateKeyParser.ReadBytes(4);
            if (resultCheck[0] != resultCheck[2] || resultCheck[1] != resultCheck[3]) {
              throw new KeyFormatterException("bad passphrase");
            }

            /* reading private key */
            var keyPair = privateKeyParser.ReadSsh1KeyData(aPublicKeyParameter);
            SshKey key = new SshKey(SshVersion.SSH1, keyPair);
            key.Comment = keyComment;
            return key;
              } catch (KeyFormatterException) {
            throw;
              } catch (Exception ex) {
            throw new KeyFormatterException("see inner exception", ex);
              }
        }
Esempio n. 20
0
 public void setEncryptCipher(BufferedBlockCipher encryptCipher)
 {
     encrypt = encryptCipher;
 }
Esempio n. 21
0
 public void setDecryptCipher(BufferedBlockCipher decryptCipher)
 {
     decrypt = decryptCipher;
 }
Esempio n. 22
0
        public override void ChangePads(string pads)
        {
            IBlockCipherPadding padding = null;
            switch (pads)
            {
                case "ISO10126d2":
                    padding = new ISO10126d2Padding();
                    break;
                case "ISO7816d4":
                    padding = new ISO7816d4Padding();
                    break;
                case "Pkcs7":
                    padding = new Pkcs7Padding();
                    break;
                case "Tbc":
                    padding = new TbcPadding();
                    break;
                case "X923":
                    padding = new X923Padding();
                    break;
                case "ZeroByte":
                    padding = new ZeroBytePadding();
                    break;
            }

            _Cipher = new PaddedBufferedBlockCipher(_Engine, padding);
        }
 public void EncryptStream(byte[] key)
 {
     this.NetStream.Flush();
     encryptCipher = new BufferedBlockCipher(new CfbBlockCipher(new AesFastEngine(), 8));
     encryptCipher.Init(true, new ParametersWithIV(
         new KeyParameter(key), key, 0, 16));
     decryptCipher = new BufferedBlockCipher(new CfbBlockCipher(new AesFastEngine(), 8));
     decryptCipher.Init(false, new ParametersWithIV(
         new KeyParameter(key), key, 0, 16));
     encrypted = true;
 }
Esempio n. 24
0
        public override void ChangeName(string name)
        {
            if (name == "0")
            {
                _Cipher = null;
                return;
            }

            switch (name)
            {
                case ESec.SCRYPTO_AES:
                    _Engine = new AesEngine();
                    _KeySize = 32;
                    _IVSize = 0;
                    break;
                case ESec.SCRYPTO_AESFAST:
                    _Engine = new AesFastEngine();
                    _KeySize = 32;
                    _IVSize = 0;
                    break;
                case ESec.SCRYPTO_AESLIGHT:
                    _Engine = new AesLightEngine();
                    _KeySize = 32;
                    _IVSize = 0;
                    break;
                case ESec.SCRYPTO_BLOWFISH:
                    _Engine = new BlowfishEngine();
                    _KeySize = 56;
                    _IVSize = 0;
                    break;
                case ESec.SCRYPTO_CAMELLIA:
                    _Engine = new CamelliaEngine();
                    _KeySize = 32;
                    _IVSize = 0;
                    break;
                case ESec.SCRYPTO_CAMELLIALIGHT:
                    _Engine = new CamelliaLightEngine();
                    _KeySize = 32;
                    _IVSize = 0;
                    break;
                case ESec.SCRYPTO_CAST5:
                    _Engine = new Cast5Engine();
                    _KeySize = 16;
                    _IVSize = 0;
                    break;
                case ESec.SCRYPTO_CAST6:
                    _Engine = new Cast6Engine();
                    _KeySize = 32;
                    _IVSize = 0;
                    break;
                case ESec.SCRYPTO_DES:
                    _Engine = new DesEngine();
                    _KeySize = 8;
                    _IVSize = 0;
                    break;
                case ESec.SCRYPTO_DESEDE:
                    _Engine = new DesEdeEngine();
                    _KeySize = 24;
                    _IVSize = 0;
                    break;
                case ESec.SCRYPTO_GOST28147:
                    _Engine = new Gost28147Engine();
                    _KeySize = 32;
                    _IVSize = 0;
                    break;
                case ESec.SCRYPTO_NOEKEON:
                    _Engine = new NoekeonEngine();
                    _KeySize = 16;
                    _IVSize = 0;
                    break;
                case ESec.SCRYPTO_NULL:
                    _Engine = new NullEngine();
                    _KeySize = 32;
                    _IVSize = 16;
                    break;
                case ESec.SCRYPTO_RC2:
                    _Engine = new RC2Engine();
                    _KeySize = 128;
                    _IVSize = 0;
                    break;
                case ESec.SCRYPTO_RC532:
                    _Engine = new RC532Engine();
                    _KeySize = 16;
                    _IVSize = 0;
                    break;
                case ESec.SCRYPTO_RC564:
                    _Engine = new RC564Engine();
                    _KeySize = 16;
                    _IVSize = 0;
                    break;
                case ESec.SCRYPTO_RC6:
                    _Engine = new RC6Engine();
                    _KeySize = 32;
                    _IVSize = 0;
                    break;
                case ESec.SCRYPTO_RIJNDAEL:
                    _Engine = new RijndaelEngine();
                    _KeySize = 32;
                    _IVSize = 0;
                    break;
                case ESec.SCRYPTO_SEED:
                    _Engine = new SeedEngine();
                    _KeySize = 16;
                    _IVSize = 0;
                    break;
                case ESec.SCRYPTO_SERPENT:
                    _Engine = new SerpentEngine();
                    _KeySize = 32;
                    _IVSize = 0;
                    break;
                case ESec.SCRYPTO_SKIPJACK:
                    _Engine = new SkipjackEngine();
                    _KeySize = 16;
                    _IVSize = 0;
                    break;
                case ESec.SCRYPTO_TEA:
                    _Engine = new TeaEngine();
                    _KeySize = 16;
                    _IVSize = 0;
                    break;
                case ESec.SCRYPTO_TWOFISH:
                    _Engine = new TwofishEngine();
                    _KeySize = 32;
                    _IVSize = 0;
                    break;
                case ESec.SCRYPTO_XTEA:
                    _Engine = new XteaEngine();
                    _KeySize = 16;
                    _IVSize = 0;
                    break;
                default:
                    _Engine = null;
                    _KeySize = 0;
                    _IVSize = 0;
                    break;
            }

            _Cm.CbMode.SelectedIndex = 0;

            _Cm.CbPads.SelectedIndex = 0;
            _Cipher = null;
        }
Esempio n. 25
0
        /// <summary>
        /// Encapsulates the specified links into an RSDF container.
        /// </summary>
        /// <param name="links">The links.</param>
        /// <returns>
        /// Base-16-encoded RSDF container.
        /// </returns>
        public static string CreateRSDF(string[] links)
        {
            var aes = new AesEngine();
            var cfb = new CfbBlockCipher(aes, 8);
            var pad = new BufferedBlockCipher(cfb);
            var sb  = new StringBuilder();

            pad.Init(true, new ParametersWithIV(new KeyParameter(RSDFKey), RSDFIV));

            foreach (var link in links)
            {
                var input  = Encoding.UTF8.GetBytes(link);
                var output = new byte[input.Length];

                for (var i = 0; i < input.Length; i++)
                {
                    output[i] = pad.ProcessByte(input[i])[0];
                }

                sb.Append(Convert.ToBase64String(output));
                sb.Append(Environment.NewLine);
            }

            return BitConverter.ToString(Encoding.ASCII.GetBytes(sb.ToString())).Replace("-", string.Empty);
        }
Esempio n. 26
0
 private BufferedBlockCipher GenerateAES(byte[] key, bool forEncryption)
 {
     BufferedBlockCipher cipher = new BufferedBlockCipher(new CfbBlockCipher(new AesFastEngine(), 8));
     cipher.Init(forEncryption, new ParametersWithIV(new KeyParameter(key), key));
     return cipher;
 }