예제 #1
0
        private void InitCiphers()
        {
            CfbBlockCipher encryptor = new CfbBlockCipher(new AesEngine(), 8);
            CfbBlockCipher decryptor = new CfbBlockCipher(new AesEngine(), 8);

            encryptor.Init(true, new ParametersWithIV(new KeyParameter(SharedKey), SharedKey));
            decryptor.Init(false, new ParametersWithIV(new KeyParameter(SharedKey), SharedKey));
            encStream = new StreamBlockCipher(encryptor);
            decStream = new StreamBlockCipher(decryptor);
        }
예제 #2
0
        public virtual ITestResult Perform()
        {
            KeyParameter key = new KeyParameter(Hex.Decode("0011223344556677"));

            byte[] input = Hex.Decode("4e6f7720");
            byte[] out1  = new byte[4];
            byte[] out2  = new byte[4];


            IBlockCipher ofb = new OfbBlockCipher(new DesEngine(), 32);

            ofb.Init(true, new ParametersWithIV(key, Hex.Decode("1122334455667788")));

            ofb.ProcessBlock(input, 0, out1, 0);

            ofb.Init(false, new ParametersWithIV(key, Hex.Decode("1122334455667788")));
            ofb.ProcessBlock(out1, 0, out2, 0);

            if (!Arrays.AreEqual(out2, input))
            {
                return(new SimpleTestResult(false, Name + ": test 1 - in != out"));
            }

            ofb.Init(true, new ParametersWithIV(key, Hex.Decode("11223344")));

            ofb.ProcessBlock(input, 0, out1, 0);

            ofb.Init(false, new ParametersWithIV(key, Hex.Decode("0000000011223344")));
            ofb.ProcessBlock(out1, 0, out2, 0);

            if (!Arrays.AreEqual(out2, input))
            {
                return(new SimpleTestResult(false, Name + ": test 2 - in != out"));
            }

            IBlockCipher cfb = new CfbBlockCipher(new DesEngine(), 32);

            cfb.Init(true, new ParametersWithIV(key, Hex.Decode("1122334455667788")));

            cfb.ProcessBlock(input, 0, out1, 0);

            cfb.Init(false, new ParametersWithIV(key, Hex.Decode("1122334455667788")));
            cfb.ProcessBlock(out1, 0, out2, 0);

            if (!Arrays.AreEqual(out2, input))
            {
                return(new SimpleTestResult(false, Name + ": test 3 - in != out"));
            }

            cfb.Init(true, new ParametersWithIV(key, Hex.Decode("11223344")));

            cfb.ProcessBlock(input, 0, out1, 0);

            cfb.Init(false, new ParametersWithIV(key, Hex.Decode("0000000011223344")));
            cfb.ProcessBlock(out1, 0, out2, 0);

            if (!Arrays.AreEqual(out2, input))
            {
                return(new SimpleTestResult(false, Name + ": test 4 - in != out"));
            }

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

        /*
         * private static bool DONT_USE_AESEncDecArray(bool bEncryptorDecrypt, ref byte[] aData, byte[] aAESKey, byte[] aIV = null)
         * {
         *  bool bRet = true;
         *  try
         *  {
         *      if (   (aData == null)
         || ((bEncryptorDecrypt == true)  && (aData.Length == 0))
         || ((bEncryptorDecrypt == false) && (aData.Length <= 16))   //Must be longer than the IV, when decrypting
         || (aAESKey.Length != 32)
         || ((aIV != null) && (aIV.Length != 16)))
         ||         return false;
         ||
         ||     // Instantiate a new Aes object to perform string symmetric encryption
         ||
         ||     using (Rijndael AES = RijndaelManaged.Create())  //Not FIPS compliant.  :(
         ||     //using (Aes AES = Aes.Create())  //Issues! Not compatible with AES CPP implementation
         ||     {
         ||         //Construct the IV
         ||         bool bIVWasNull = (aIV == null);
         ||         if (bIVWasNull)
         ||             aIV = new byte[16];
         ||
         ||         if (bEncryptorDecrypt)
         ||         {
         ||             if (bIVWasNull)
         ||                 aIV.FillRandom();  //when encrypting: new IV
         ||         }
         ||         else
         ||             Array.Copy(aData, aData.Length - 16, aIV, 0, 16);  //when decrypting: use the IV thats the last 16 bytes of the data.
         ||
         ||         // Set mode, padding, key and IV
         ||         AES.Mode      = CipherMode.CFB;
         ||         AES.Padding   = PaddingMode.None;
         ||         AES.Key       = aAESKey;
         ||         AES.IV        = aIV;
         ||         AES.BlockSize = 128; //16 bytes
         ||
         ||         // Instantiate a new MemoryStream object to contain the encrypted bytes
         ||         MemoryStream ms = new MemoryStream();
         ||         //Dispose not required on MemoryStream, but gives a compiler warning/error when in a 'using' as CryptoStream below disposes of it as well.
         ||         //using (MemoryStream ms = new MemoryStream())
         ||         {
         ||             // Instantiate a new encryptor/decryptor from our Aes object
         ||             using (ICryptoTransform aesCryptor = bEncryptorDecrypt ? AES.CreateEncryptor() : AES.CreateDecryptor())
         ||             {
         ||                 // Instantiate a new CryptoStream object to process the data and write it to the memory stream
         ||                 using (CryptoStream cs = new CryptoStream(ms, aesCryptor, CryptoStreamMode.Write))
         ||                 {
         ||                     // Encrypt/decrypt the input
         ||                     int iCount = bEncryptorDecrypt ? aData.Length : aData.Length - 16;
         ||                     cs.Write(aData, 0, iCount);  //-16, ignore the IV
         ||
         ||                     //Add padding
         ||                     int iPadded = -1;
         ||                     {
         ||                         //Padding.. .Why do we have to do this!
         ||                         int iBlockSizeInBytes = AES.BlockSize / 8;  //Bits to bytes.
         ||
         ||                         int iCompleteBlock = iBlockSizeInBytes - (iCount % iBlockSizeInBytes);
         ||                         if (iCompleteBlock < iBlockSizeInBytes)
         ||                         {
         ||                             iPadded = iCompleteBlock;
         ||                             byte[] aZero = new byte[iPadded];
         ||                             cs.Write(aZero, 0, iPadded);  //-16, ignore the IV
         ||                         }
         ||
         ||                     }
         ||
         ||                     // Complete the encrypt/decrypt process
         ||                     cs.FlushFinalBlock();
         ||
         ||                     //Remove Padding
         ||                     if (iPadded > 0)
         ||                     {
         ||
         ||                         long iLen = ms.Length;
         ||                         if (iLen > iPadded)
         ||                             ms.SetLength(iLen - iPadded);
         ||                     }
         ||
         ||                     if (bEncryptorDecrypt)
         ||                         ms.Write(aIV, 0, aIV.Length);
         ||
         ||                     // Convert the encrypted/decrypted data from a MemoryStream to a byte array
         ||                     aData = ms.ToArray();
         ||                 }
         ||             }
         ||         }
         ||     }
         || }
         || catch (Exception e) { bRet = false;  e.CatchMessage().TraceError("EncDecArray(): "); }
         || return bRet;
         ||} // DONT_USE_AESEncDecArray
         */

        private static bool AESEncDecArray(bool bEncryptorDecrypt, ref byte[] aData, byte[] aAESKey, byte[] aIV = null)
        {
            bool bRet = true;

            try
            {
                if ((aData == null) ||
                    ((bEncryptorDecrypt == true) && (aData.Length == 0)) ||
                    ((bEncryptorDecrypt == false) && (aData.Length <= 16)) ||   //Must be longer than the IV, when decrypting
                    (aAESKey.Length != 32) ||
                    ((aIV != null) && (aIV.Length != 16)))
                {
                    return(false);
                }

                //Construct the IV
                bool bIVWasNull = (aIV == null);
                if (bIVWasNull)
                {
                    aIV = new byte[16];
                }

                int iDataLength = 0;
                if (bEncryptorDecrypt)
                {
                    iDataLength = aData.Length;
                    if (bIVWasNull)
                    {
                        aIV.FillRandom();  //when encrypting: new IV
                    }
                }
                else
                {
                    Array.Copy(aData, aData.Length - 16, aIV, 0, 16);  //when decrypting: use the IV thats the last 16 bytes of the data.
                    iDataLength = aData.Length - 16;
                }

                CfbBlockCipher cfb = new CfbBlockCipher(new AesEngine(), 128);
                cfb.Init(bEncryptorDecrypt, new ParametersWithIV(new KeyParameter(aAESKey), aIV));

                int iBlockSize = cfb.GetBlockSize();
                for (int i = 0; i < iDataLength; i += iBlockSize)
                {
                    int iRet       = 0;
                    int iRemaining = iDataLength - i;
                    if (iRemaining < iBlockSize)
                    {
                        //Last partial block
                        byte[] aIn  = new byte[iBlockSize];
                        byte[] aOut = new byte[iBlockSize];
                        Array.Copy(aData, i, aIn, 0, iRemaining);

                        if (bEncryptorDecrypt)
                        {
                            iRet = cfb.EncryptBlock(aIn, 0, aOut, 0);
                        }
                        else
                        {
                            iRet = cfb.DecryptBlock(aIn, 0, aOut, 0);
                        }
                        Array.Copy(aOut, 0, aData, i, iRemaining);
                        continue;
                    }

                    if (bEncryptorDecrypt)
                    {
                        iRet = cfb.EncryptBlock(aData, i, aData, i);
                    }
                    else
                    {
                        iRet = cfb.DecryptBlock(aData, i, aData, i);
                    }
                }

                if (bEncryptorDecrypt)
                {
                    //Append IV.
                    Array.Resize(ref aData, iDataLength + 16);
                    Array.Copy(aIV, 0, aData, iDataLength, 16);
                }
                else
                {
                    //Remove IV
                    Array.Resize(ref aData, iDataLength);
                }
            }
            catch (Exception e) { bRet = false; e.CatchMessage().TraceError("AESEncDecArray(): "); }
            return(bRet);
        } // AESEncDecArray