Esempio n. 1
0
        private static byte[] RSAEncryptData(AsymmetricKeyParameter publicKey, byte[] originalDataBytes)
        {
            bool isEncryption = true;

            byte[] encryptedDataArray;

            using (MemoryStream originalDataStream = new MemoryStream(originalDataBytes, false))
            {
                using (MemoryStream encryptedDataStream = new MemoryStream())
                {
                    IBufferedCipher rsacipher = RSACreateCipher(isEncryption, publicKey);
                    using (CipherStream cipherStream = new CipherStream(originalDataStream, rsacipher, null))
                    {
                        int oneByte;
                        while ((oneByte = cipherStream.ReadByte()) >= 0)
                        {
                            encryptedDataStream.WriteByte((byte)oneByte);
                        }

                        cipherStream.Close();
                        cipherStream.Dispose();
                    }

                    encryptedDataArray = encryptedDataStream.ToArray();

                    encryptedDataStream.Close();
                    encryptedDataStream.Dispose();
                }

                originalDataStream.Close();
                originalDataStream.Dispose();
            }
            return(encryptedDataArray);
        }
Esempio n. 2
0
        public static byte[] AESEncryptData(byte[] originalDataBytes, byte[] aesKeyByteData, byte[] iv = null)
        {
            bool isEncryption = true;

            MemoryStream    originalDataStream  = new MemoryStream(originalDataBytes, false);
            MemoryStream    encryptedDataStream = new MemoryStream();
            IBufferedCipher aesCipher           = AESCreateCipher(isEncryption, aesKeyByteData, iv);
            CipherStream    cipherStream        = new CipherStream(originalDataStream, aesCipher, null);

            int oneByte;

            while ((oneByte = cipherStream.ReadByte()) >= 0)
            {
                encryptedDataStream.WriteByte((byte)oneByte);
            }
            encryptedDataStream.Close();
            cipherStream.Close();

            return(encryptedDataStream.ToArray());
        }
Esempio n. 3
0
        private static byte[] RSAEntschluesselDaten(AsymmetricKeyParameter privateKey, byte[] encryptedDataBytes)
        {
            bool isEncryption = false;

            MemoryStream    enryptedDataSream   = new MemoryStream(encryptedDataBytes, false);
            MemoryStream    decryptedDataStream = new MemoryStream();
            IBufferedCipher aesCipher           = RSACreateCipher(isEncryption, privateKey);
            CipherStream    decodedCipherStream = new CipherStream(enryptedDataSream, aesCipher, null);

            int oneByte;

            while ((oneByte = decodedCipherStream.ReadByte()) >= 0)
            {
                decryptedDataStream.WriteByte((byte)oneByte);
            }

            decodedCipherStream.Close();
            decryptedDataStream.Close();

            return(decryptedDataStream.ToArray());
        }
Esempio n. 4
0
        public static byte[] AESDecryptData(byte[] encryptedDataBytes, byte[] aesKeyByteData, byte[] iv = null)
        {
            bool isEncryption = false;

            MemoryStream    enryptedDataSream   = new MemoryStream(encryptedDataBytes, false);
            MemoryStream    decryptedDataStream = new MemoryStream();
            IBufferedCipher aesCipher           = AESCreateCipher(isEncryption, aesKeyByteData, iv);
            CipherStream    decodedCipherStream = new CipherStream(enryptedDataSream, aesCipher, null);

            int oneByte;

            while ((oneByte = decodedCipherStream.ReadByte()) >= 0)
            {
                decryptedDataStream.WriteByte((byte)oneByte);
            }

            decodedCipherStream.Close();
            decryptedDataStream.Close();

            return(decryptedDataStream.ToArray());
        }
Esempio n. 5
0
		private byte[] encryptOnRead(byte[] dataBytes)
		{
			MemoryStream dataStream = new MemoryStream(dataBytes, false);
			MemoryStream encryptedDataStream = new MemoryStream();
			IBufferedCipher inCipher = createCipher(true);
			CipherStream inCipherStream = new CipherStream(dataStream, inCipher, null);

			int ch;
			while ((ch = inCipherStream.ReadByte()) >= 0)
			{
				encryptedDataStream.WriteByte((byte) ch);
			}

			encryptedDataStream.Close();
			inCipherStream.Close();

			byte[] encryptedDataBytes = encryptedDataStream.ToArray();
			Assert.AreEqual(dataBytes.Length, encryptedDataBytes.Length);

			return encryptedDataBytes;
		}
Esempio n. 6
0
        private byte[] encryptOnRead(byte[] dataBytes)
        {
            MemoryStream    dataStream          = new MemoryStream(dataBytes, false);
            MemoryStream    encryptedDataStream = new MemoryStream();
            IBufferedCipher inCipher            = createCipher(true);
            CipherStream    inCipherStream      = new CipherStream(dataStream, inCipher, null);

            int ch;

            while ((ch = inCipherStream.ReadByte()) >= 0)
            {
                encryptedDataStream.WriteByte((byte)ch);
            }

            encryptedDataStream.Close();
            inCipherStream.Close();

            byte[] encryptedDataBytes = encryptedDataStream.ToArray();
            Assert.AreEqual(dataBytes.Length, encryptedDataBytes.Length);

            return(encryptedDataBytes);
        }
Esempio n. 7
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.");
            }
        }