tags:

views:

173

answers:

7

Hello there guys! I'm using encryption in my program, that decrypts files if it needs to use it, and encrypts it after it done with the file. But my files arouznd 100mb, and it takes more than a minute to crypt them.

My question is, is it possible, to make my file unreadable, without encrypting the whole file? So , for example, just encrypting some byte, or I dont know how to do it. I need to reduce my crypting time. Thanks a lot!

Edit1: Sorry, forgot. The file iam talking about, is MPQ files, that a game using (wow), to read from it model files, music , etc... so its not a text file. It can be opened by a special program (MPQ Editor). I edited some files, and I made my own MPQ file, and I want to protect it from other users, to open it with MPQ Editor. My program can open mpq files too. But the mpq file is created with this mpq editor thing. I just want to make it unreadeably to mpq editor. And if my program will use it, it should decrypt it, and if my program done with the MPQ file, it should encrypt it again, to block others to edit it.

Edit2: Ok, to make it clear. There is a program name world of warcraft. It reads game files from MPQ files, MPQ file is developped by Wow (Blizzard Co) , but It can be opened by some programs, editors. I've got an edited MPQ file, (replaced music) and I uploaded it to my server. My program downloads it to other players game folder, and they will got this change ingame, like me :) They will hear other musics too. (just example.) But I dont want them to edit / or see my MPQ file with any editor program, so I implented a simple encryption , decryption to my program. My MPQ file is uploaded encrypted to my server. When they start the game, my program decrypts the file (yes, they will be able to edit it now, but they dont know this :P , its basic security), so the game will reckognise the MPQ file, and it will load it , with the changes within it. When the game program closes, my program encrypts back the patch, so it will be unreadable again. I hope you understand the process. The thing is, my MPQ (patch) file is ~100mb big. It means, it takes ~1minute to encrypt / decrypt them. And this is what I want to replace, the crypting method. Must encrypt it with something better method, and decrypt it when it needed.

+1  A: 

If by unreadable you mean that the data will not be plain text you can use binary Serialization/De-serialization.

Itay
Unreadeable, I mean to not be able to open with MPQ editor programs.
Antal Dominik
+1  A: 

Read an arbitrary number of bytes from the start and append them to the end, then for the backwards trek - reverse. This should stop an application from recognising the file as a valid type. This won't make text in a file unreadable though.

Adam
This is a good idea, is there any tutorial like this? :OBut if players reallize how do I "protect" the file, they can just revert the file too, and use it. My actuall protection encrypt with a password, but thats too slow :S
Antal Dominik
Obfuscation is often a good-enough form of protection - nothing is un-crackable though. I don't have any tutorial links, I simply made it up off the top of my head when I read your question. People won't ever know how many bytes to shift, you could always make it different for each file and not necessarily use contiguous bytes.
Adam
By the sounds of it though, if these are custom file types then my idea won't work - I was suggesting it to simply break known file types from loading in their respective apps. If you need to mask the entire file, you will have to perform an action across the entire file. Encrypt is expensive to calculate, try shifting the bytes 1 left, reversing them, swapping blocks of them etc - most of this should be faster than encryption and give you enough jibberish to protect the content.
Adam
A: 

if it is just text - no. You will have a bad character in it, but it will still be readable. If it is a custom binary format, people would have to reverse engineer the file anyway, so there is no use.

Femaref
Its custom binary, but its already "cracked" , and there is some program to open it with, or edit. I want to block this, and make only readeable to my program, but with a decryptable encryption :D lol sorry for my english.
Antal Dominik
A: 

You can use a poor mans encryption using xor

        int key = 83847; // whatever value you want
        string str = "This is my file";

        // "Encrypt"
        var sb = new StringBuilder(str.Length);
        foreach (var ch in str)
        {
            sb.Append((char)(ch ^ key));
        }

        Console.WriteLine(sb.ToString()); // Garbled text

        // "Decrypt"
        var sbReversed = new StringBuilder(str.Length);
        foreach (var ch in sb.ToString())
        {
            sbReversed.Append((char)(ch ^ key));
        }

        Console.WriteLine(sbReversed.ToString()); // "This is my file"
simendsjo
It's also probably worth a mention that XOR is susceptible to certain attacks: http://forums.devshed.com/security-and-cryptography-17/is-xor-encryption-unbreakable-407514.html
Wayne Werner
I think, I'm doing this method right now. But if I do this with a ~100 mb file, it takes too long, this is why I want to replace this method, with something , that faster to encrypt / decrypt.
Antal Dominik
@Wayne. Thats why I called it "poor man's encryption" and used "Encrypt" and "Decrypt" in the comments
simendsjo
@Antal: XOR is _very_ fast, and every algorithm needs to read the entire file and operate on it in order to change the data. Don't think you can expect anything faster. Why do you need to encrypt it? If it's sending over the web, you might consider using https to secure.
simendsjo
A: 

You can try to use the TripleDESCryptoServiceProvider.

Encrypt Text Using TripleDESCryptoServiceProvider | C# Snippet | SnippetBank.Net

Syed
A: 

Using DES (64-bit which is faster that rijndael) it takes me less than 5s to process a 112Mb File. Have you tried it ?

Nevertheless, you gave me a funny idea : Encrypt half of the block of your input file and keep the other half cleartext. (You'll get 2 files ... and you must forget about security obviously)

It cut down the processing time by ~40% and you can adjust the ratio encrypted/cleartext blocks to gain time :

        private static void EncryptData(String inName, String outName, byte[] desKey, byte[] desIV)
    {  
        //Create the file streams to handle the input and output files.
        FileStream fin = new FileStream(inName, FileMode.Open, FileAccess.Read);
        FileStream fout = new FileStream(outName, FileMode.OpenOrCreate, FileAccess.Write);
        FileStream foutclear = new FileStream(outName + ".clear", FileMode.OpenOrCreate, FileAccess.Write);
        fout.SetLength(0);

        //Create variables to help with read and write.
        byte[] bin = new byte[4096]; //This is intermediate storage for the encryption.
        long rdlen = 0;              //This is the total number of bytes written.
        long totlen = fin.Length;    //This is the total length of the input file.
        int len;                     //This is the number of bytes to be written at a time.

        System.Security.Cryptography.SymmetricAlgorithm des = SymmetricAlgorithm.Create("DES"); // Using DES      
        CryptoStream encStream = new CryptoStream(fout, des.CreateEncryptor(desKey, desIV), CryptoStreamMode.Write);
        Console.WriteLine("Encrypting...");

        //Read from the input file, then encrypt and write to the output file.
        while (rdlen < totlen)
        {
            len = fin.Read(bin, 0, 4096);
            encStream.Write(bin, 0, len);
            rdlen = rdlen + len;
            len = fin.Read(bin, 0, 4096);
            foutclear.Write(bin, 0, len);
            rdlen = rdlen + len;
        }

        encStream.Close();
        foutclear.Close();
        fout.Close();
        fin.Close();        
    }
hoang
A: 

I don't know anything about mpq files, but my first question would be, "how does the editor recognize that this is an mpq file?". If it is, for example, that there is a certain value in the first 3 bytes, just change those bytes and the editor will fail. Change them back and you can read it. Voila. If some other mechanism, same logic.

The point being, the file does not need to be encrypted at all - merely tweaked so that the mpq editor will not recognize it as a valid file for opening.

mickeyf
Yeah, this is what I need! I didnt even though about this solution. Thank you, I'll give it a try, and ofc thanks for everyone else!
Antal Dominik
And this, ladies and gentlemen, is one of the perennial problems with the software industry. People answer the question that is being asked rather than taking a moment to try to understand what the problem really is.
mickeyf
I'll try to get which that "bytes" I need.I think i've just found what I need, but yet, iam not as pro, to fully understand this :Shttp://www.zezula.net/en/mpq/mpqformat.html
Antal Dominik
Actually, its not their fault, I didnt wrote my whole story, when I posted this topic. And my english is bad too, so its hard to understand.
Antal Dominik
In cases like this, a hex editor is your friend.
mickeyf
@mickey I hope your not including me in that annoucement :)
Adam
@Adamn - It was a bit of a tease aimed at no one in particular. It is actually one of the perennial problems with human beings, myself included.
mickeyf