Пример #1
0
 public void Close()
 {
     if (cOut != null)
     {
         if (digestOut != null)
         {
             BcpgOutputStream bcpgOutputStream = new BcpgOutputStream(digestOut, PacketTag.ModificationDetectionCode, 20L);
             bcpgOutputStream.Flush();
             digestOut.Flush();
             byte[] array = DigestUtilities.DoFinal(digestOut.WriteDigest());
             cOut.Write(array, 0, array.Length);
         }
         cOut.Flush();
         try
         {
             pOut.Write(c.DoFinal());
             pOut.Finish();
         }
         catch (Exception ex)
         {
             throw new IOException(ex.Message, ex);
         }
         cOut = null;
         pOut = null;
     }
 }
Пример #2
0
        /// <summary>
        /// <p>
        /// Close off the encrypted object - this is equivalent to calling Close() on the stream
        /// returned by the Open() method.
        /// </p>
        /// <p>
        /// <b>Note</b>: This does not close the underlying output stream, only the stream on top of
        /// it created by the Open() method.
        /// </p>
        /// </summary>
        public void Close()
        {
            if (cOut != null)
            {
                // TODO Should this all be under the try/catch block?
                if (digestOut != null)
                {
                    //
                    // hand code a mod detection packet
                    //
                    BcpgOutputStream bOut = new BcpgOutputStream(
                        digestOut, PacketTag.ModificationDetectionCode, 20);

                    bOut.Flush();
                    digestOut.Flush();

                    // TODO
                    byte[] dig = DigestUtilities.DoFinal(digestOut.WriteDigest());
                    cOut.Write(dig, 0, dig.Length);
                }

                cOut.Flush();

                try
                {
                    pOut.Write(c.DoFinal());
                    pOut.Finish();
                }
                catch (Exception e)
                {
                    throw new IOException(e.Message, e);
                }

                cOut = null;
                pOut = null;
            }
        }
Пример #3
0
        private void doRunTest(
            string name,
            int ivLength)
        {
            string lCode = "ABCDEFGHIJKLMNOPQRSTUVWXY0123456789";

            string baseName = name;

            if (name.IndexOf('/') >= 0)
            {
                baseName = name.Substring(0, name.IndexOf('/'));
            }

            CipherKeyGenerator kGen = GeneratorUtilities.GetKeyGenerator(baseName);

            IBufferedCipher inCipher  = CipherUtilities.GetCipher(name);
            IBufferedCipher outCipher = CipherUtilities.GetCipher(name);
            KeyParameter    key       = ParameterUtilities.CreateKeyParameter(baseName, kGen.GenerateKey());
            MemoryStream    bIn       = new MemoryStream(Encoding.ASCII.GetBytes(lCode), false);
            MemoryStream    bOut      = new MemoryStream();

            // In the Java build, this IV would be implicitly created and then retrieved with getIV()
            ICipherParameters cipherParams = key;

            if (ivLength > 0)
            {
                cipherParams = new ParametersWithIV(cipherParams, new byte[ivLength]);
            }

            inCipher.Init(true, cipherParams);

            // TODO Should we provide GetIV() method on IBufferedCipher?
            //if (inCipher.getIV() != null)
            //{
            //	outCipher.Init(false, new ParametersWithIV(key, inCipher.getIV()));
            //}
            //else
            //{
            //	outCipher.Init(false, key);
            //}
            outCipher.Init(false, cipherParams);

            CipherStream cIn  = new CipherStream(bIn, inCipher, null);
            CipherStream cOut = new CipherStream(bOut, null, outCipher);

            int c;

            while ((c = cIn.ReadByte()) >= 0)
            {
                cOut.WriteByte((byte)c);
            }

            cIn.Close();

            cOut.Flush();
            cOut.Close();

            byte[] bs  = bOut.ToArray();
            string res = Encoding.ASCII.GetString(bs, 0, bs.Length);

            if (!res.Equals(lCode))
            {
                Fail("Failed - decrypted data doesn't match.");
            }
        }