0

如何在 C# 中播放加密的 swf。我使用des 算法来加密该 swf。我如何在不创建文件的情况下播放它。

这是我的代码...

        sKey = "tharu123";
        sInputFilename = "D:\\Main.swf";
        FileStream fsInput = new FileStream(sInputFilename, FileMode.Open, FileAccess.Read);
        MemoryStream stm = new MemoryStream();

        DESCryptoServiceProvider DES = new DESCryptoServiceProvider();
        DES.Key = ASCIIEncoding.ASCII.GetBytes(sKey);
        DES.IV = ASCIIEncoding.ASCII.GetBytes(sKey);
        ICryptoTransform desencrypt = DES.CreateDecryptor();

        CryptoStream cryptostream = new CryptoStream(stm, desencrypt, CryptoStreamMode.Write);

        //byte[] bytearrayinput = new byte[fsInput.Length];
        //fsInput.Read(bytearrayinput, 0, bytearrayinput.Length);
        //cryptostream.Write(bytearrayinput, 0, bytearrayinput.Length);

        BinaryWriter writer = new BinaryWriter(cryptostream);

        writer.Write(cryptostream.Length);

        writer.Write(0x55665566);

        stm.Seek(0, SeekOrigin.Begin);

        axShockwaveFlash1.OcxState = new AxHost.State(stm, 1, false, null);
        fsInput.Close();

这不是很好..

4

2 回答 2

1

我为您创建了一个类来加密到文件并解密到文件和流。

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;

namespace DES_Encryption
{
    class DES_Class
    {
        public static void EncryptAndSaveFile(string InputFilePath, string OutputFilePath, byte[] Key, byte[] IV)
        {
            if (((InputFilePath.Length <= 0 || InputFilePath == null) || (Key.Length <= 0 || Key == null) || (IV.Length <= 0 || IV == null)))
                throw new Exception("All Values must be filled");

            using (FileStream fileCrypt = new FileStream(OutputFilePath, FileMode.Create))
            {
                using (DESCryptoServiceProvider encrypt = new DESCryptoServiceProvider())
                {
                    using (CryptoStream cs = new CryptoStream(fileCrypt, encrypt.CreateEncryptor(Key, IV), CryptoStreamMode.Write))
                    {
                        using (FileStream fileInput = new FileStream(InputFilePath, FileMode.Open))
                        {
                            int data;
                            while ((data = fileInput.ReadByte()) != -1)
                                cs.WriteByte((byte)data);
                        }
                    }
                }
            }
        }


        public static void DecryptAndSaveFile(string InputFilePath, string OutputFilePath, byte[] Key, byte[] IV)
        {
            if (((InputFilePath.Length <= 0 || InputFilePath == null) || (OutputFilePath.Length <= 0 || OutputFilePath == null) 
                || (Key.Length <= 0 || Key == null) || (IV.Length <= 0 || IV == null)))
                throw new Exception("All Values must be filled");

            using (FileStream fileCrypt = new FileStream(OutputFilePath, FileMode.Create))
            {
                using (DESCryptoServiceProvider decrypt = new DESCryptoServiceProvider())
                {
                    using (CryptoStream cs = new CryptoStream(fileCrypt, decrypt.CreateDecryptor(Key, IV), CryptoStreamMode.Write))
                    {
                        using (FileStream fileInput = new FileStream(InputFilePath, FileMode.Open))
                        {
                            int data;
                            while ((data = fileInput.ReadByte()) != -1)
                                cs.WriteByte((byte)data);
                        }
                    }
                }
            }
        }

        public static byte[] DecryptFileAndReturnStream(string InputFilePath, byte[] Key, byte[] IV)
        {
            if (((InputFilePath.Length <= 0 || InputFilePath == null) || (Key.Length <= 0 || Key == null) || (IV.Length <= 0 || IV == null)))
                throw new Exception("All Values must be filled");

            using (MemoryStream stream = new MemoryStream())
            {
                using (DESCryptoServiceProvider decrypt = new DESCryptoServiceProvider())
                {
                    using (CryptoStream cs = new CryptoStream(stream, decrypt.CreateDecryptor(Key, IV), CryptoStreamMode.Write))
                    {
                        using (FileStream fileInput = new FileStream(InputFilePath, FileMode.Open))
                        {
                            int data;
                            while ((data = fileInput.ReadByte()) != -1)
                                cs.WriteByte((byte)data);
                        }
                    }
                }
                return stream.ToArray();
            }
        }


        public static byte[] StringToByteArray(string cipher)
        {
            byte[] ByteArray = Encoding.ASCII.GetBytes(cipher);
            return ByteArray;
        }

        public static string ByteArrayToString(byte[] arr)
        {
            string ASCII = Encoding.ASCII.GetString(arr);
            return ASCII;
        }
    }
}

您需要为您的 Flashfiles 创建一个控件,例如像这样(在 Form.Designer.cs 中):

private AxShockwaveFlashObjects.AxShockwaveFlash AxShockwaveFlash1;

this.AxShockwaveFlash1 = new AxShockwaveFlashObjects.AxShockwaveFlash();

// 
// AxShockwaveFlash1
// 
this.AxShockwaveFlash1.Enabled = true;
this.AxShockwaveFlash1.Location = new System.Drawing.Point(6, 19);
this.AxShockwaveFlash1.Name = "AxShockwaveFlash1";
this.AxShockwaveFlash1.Size = new System.Drawing.Size(643, 294);
this.AxShockwaveFlash1.TabIndex = 0;

最后,您可以使用以下代码加载 flash 文件(感谢 tyranid):

    private void Button_Decrypt_Click(object sender, EventArgs e)
{
    byte[] Key = DES_Class.StringToByteArray(TextBox_Encrypt_Key.Text);
    byte[] IV = DES_Class.StringToByteArray(TextBox_Encrypt_IV.Text);
    byte[] str = DES_Class.DecryptFileAndReturnStream(TextBox_Encrypt_OuputFilePath.Text,Key, IV);
    InitFlashMovie(str);
}

private void InitFlashMovie(byte[] swfFile)
{
    using (MemoryStream stm = new MemoryStream())
    {
        using (BinaryWriter writer = new BinaryWriter(stm))
        {
            /* Write length of stream for AxHost.State */
            writer.Write(8 + swfFile.Length);
            /* Write Flash magic 'fUfU' */
            writer.Write(0x55665566);
            /* Length of swf file */
            writer.Write(swfFile.Length);
            writer.Write(swfFile);
            stm.Seek(0, SeekOrigin.Begin);
            /* 1 == IPeristStreamInit */
            AxShockwaveFlash1.OcxState = new AxHost.State(stm, 1, true, null);
        }
    }
}

在这里你可以得到演示项目:LINK

在这里你有一个较小的项目(仅解密):LINK

于 2013-11-05T11:37:38.840 回答
0

使用 MemoryStreams 将加密的 bytes[] 保存到内存中。

// Set this
static byte[] mybytes;

static System.IO.MemoryStream ms = new MemoryStream(mybytes);


public static byte[] MyMemoStream()
{
    return ms.ToArray();
}

发布您的代码也是一个好主意。

于 2013-11-04T12:29:19.023 回答