Inheritance: Mono.Security.Cryptography.SymmetricTransform
		public TripleDESTransform (TripleDES algo, bool encryption, byte[] key, byte[] iv) : base (algo, encryption, iv) 
		{
			if (key == null) {
				key = GetStrongKey ();
			}
			// note: checking weak keys also checks valid key length
			if (TripleDES.IsWeakKey (key)) {
				string msg = Locale.GetText ("This is a known weak key.");
				throw new CryptographicException (msg);
			}

			byte[] key1 = new byte [8];
			byte[] key2 = new byte [8];
			byte[] key3 = new byte [8];
			DES des = DES.Create ();
			Buffer.BlockCopy (key, 0, key1, 0, 8);
			Buffer.BlockCopy (key, 8, key2, 0, 8);
			if (key.Length == 16)
				Buffer.BlockCopy (key, 0, key3, 0, 8);
			else
				Buffer.BlockCopy (key, 16, key3, 0, 8);
	
			// note: some modes (like CFB) requires encryption when decrypting
			if ((encryption) || (algo.Mode == CipherMode.CFB)) {
				E1 = new DESTransform (des, true, key1, iv);
				D2 = new DESTransform (des, false, key2, iv);
				E3 = new DESTransform (des, true, key3, iv);
			}
			else {
				D1 = new DESTransform (des, false, key3, iv);
				E2 = new DESTransform (des, true, key2, iv);
				D3 = new DESTransform (des, false, key1, iv);
			}
		}
Exemple #2
0
        internal static void Permutation(byte[] input, byte[] output, uint[] permTab, bool preSwap)
        {
            if (preSwap && BitConverter.IsLittleEndian)
            {
                DESTransform.BSwap(input);
            }
            int  num  = input[0] >> 4 << 1;
            int  num2 = 32 + ((int)(input[0] & 15) << 1);
            uint num3 = permTab[num++] | permTab[num2++];
            uint num4 = permTab[num] | permTab[num2];
            int  num5 = DESTransform.BLOCK_BYTE_SIZE << 1;
            int  i    = 2;
            int  num6 = 1;

            while (i < num5)
            {
                int num7 = (int)input[num6];
                num   = (i << 5) + (num7 >> 4 << 1);
                num2  = (i + 1 << 5) + ((num7 & 15) << 1);
                num3 |= (permTab[num++] | permTab[num2++]);
                num4 |= (permTab[num] | permTab[num2]);
                i    += 2;
                num6++;
            }
            if (preSwap || !BitConverter.IsLittleEndian)
            {
                output[0] = (byte)num3;
                output[1] = (byte)(num3 >> 8);
                output[2] = (byte)(num3 >> 16);
                output[3] = (byte)(num3 >> 24);
                output[4] = (byte)num4;
                output[5] = (byte)(num4 >> 8);
                output[6] = (byte)(num4 >> 16);
                output[7] = (byte)(num4 >> 24);
            }
            else
            {
                output[0] = (byte)(num3 >> 24);
                output[1] = (byte)(num3 >> 16);
                output[2] = (byte)(num3 >> 8);
                output[3] = (byte)num3;
                output[4] = (byte)(num4 >> 24);
                output[5] = (byte)(num4 >> 16);
                output[6] = (byte)(num4 >> 8);
                output[7] = (byte)num4;
            }
        }
Exemple #3
0
 // note: this method is garanteed to be called with a valid blocksize
 // for both input and output
 protected override void ECB(byte[] input, byte[] output)
 {
     DESTransform.Permutation(input, output, DESTransform.ipTab, false);
     if (encrypt)
     {
         E1.ProcessBlock(output, output);
         D2.ProcessBlock(output, output);
         E3.ProcessBlock(output, output);
     }
     else
     {
         D1.ProcessBlock(output, output);
         E2.ProcessBlock(output, output);
         D3.ProcessBlock(output, output);
     }
     DESTransform.Permutation(output, output, DESTransform.fpTab, true);
 }
Exemple #4
0
        public TripleDESTransform(TripleDES algo, bool encryption, byte[] key, byte[] iv) : base(algo, encryption, iv)
        {
#if NET_2_0
            if (key == null)
            {
                key = GetStrongKey();
            }
#endif
            // note: checking weak keys also checks valid key length
            if (TripleDES.IsWeakKey(key))
            {
                string msg = Locale.GetText("This is a known weak key.");
                throw new CryptographicException(msg);
            }

            byte[] key1 = new byte [8];
            byte[] key2 = new byte [8];
            byte[] key3 = new byte [8];
            DES    des  = DES.Create();
            Buffer.BlockCopy(key, 0, key1, 0, 8);
            Buffer.BlockCopy(key, 8, key2, 0, 8);
            if (key.Length == 16)
            {
                Buffer.BlockCopy(key, 0, key3, 0, 8);
            }
            else
            {
                Buffer.BlockCopy(key, 16, key3, 0, 8);
            }

            // note: some modes (like CFB) requires encryption when decrypting
            if ((encryption) || (algo.Mode == CipherMode.CFB))
            {
                E1 = new DESTransform(des, true, key1, iv);
                D2 = new DESTransform(des, false, key2, iv);
                E3 = new DESTransform(des, true, key3, iv);
            }
            else
            {
                D1 = new DESTransform(des, false, key3, iv);
                E2 = new DESTransform(des, true, key2, iv);
                D3 = new DESTransform(des, false, key1, iv);
            }
        }
Exemple #5
0
 internal DESTransform(SymmetricAlgorithm symmAlgo, bool encryption, byte[] key, byte[] iv) : base(symmAlgo, encryption, iv)
 {
     byte[] array = null;
     if (key == null)
     {
         key   = DESTransform.GetStrongKey();
         array = key;
     }
     if (DES.IsWeakKey(key) || DES.IsSemiWeakKey(key))
     {
         string text = Locale.GetText("This is a known weak, or semi-weak, key.");
         throw new CryptographicException(text);
     }
     if (array == null)
     {
         array = (byte[])key.Clone();
     }
     this.keySchedule = new byte[DESTransform.KEY_BYTE_SIZE * 16];
     this.byteBuff    = new byte[DESTransform.BLOCK_BYTE_SIZE];
     this.dwordBuff   = new uint[DESTransform.BLOCK_BYTE_SIZE / 4];
     this.SetKey(array);
 }
        public TripleDESTransform(TripleDES algo, bool encryption, byte[] key, byte[] iv) : base(algo, encryption, iv)
        {
            if (key == null)
            {
                key = TripleDESTransform.GetStrongKey();
            }
            if (TripleDES.IsWeakKey(key))
            {
                string text = Locale.GetText("This is a known weak key.");
                throw new CryptographicException(text);
            }
            byte[] array    = new byte[8];
            byte[] array2   = new byte[8];
            byte[] array3   = new byte[8];
            DES    symmAlgo = DES.Create();

            Buffer.BlockCopy(key, 0, array, 0, 8);
            Buffer.BlockCopy(key, 8, array2, 0, 8);
            if (key.Length == 16)
            {
                Buffer.BlockCopy(key, 0, array3, 0, 8);
            }
            else
            {
                Buffer.BlockCopy(key, 16, array3, 0, 8);
            }
            if (encryption || algo.Mode == CipherMode.CFB)
            {
                this.E1 = new DESTransform(symmAlgo, true, array, iv);
                this.D2 = new DESTransform(symmAlgo, false, array2, iv);
                this.E3 = new DESTransform(symmAlgo, true, array3, iv);
            }
            else
            {
                this.D1 = new DESTransform(symmAlgo, false, array3, iv);
                this.E2 = new DESTransform(symmAlgo, true, array2, iv);
                this.D3 = new DESTransform(symmAlgo, false, array, iv);
            }
        }
Exemple #7
0
 public override void GenerateKey()
 {
     KeyValue = DESTransform.GetStrongKey();
 }
Exemple #8
0
 protected override void ECB(byte[] input, byte[] output)
 {
     DESTransform.Permutation(input, output, DESTransform.ipTab, false);
     this.ProcessBlock(output, this.byteBuff);
     DESTransform.Permutation(this.byteBuff, output, DESTransform.fpTab, true);
 }