I'm writting a script that can encrypt files in a folder. It takes like 30s to encrypt a 1GB file, so I wondering if it's safe when I encrypt some bits of a file and leave the rest unencrypted? I want the script to run as fast as possible.
That fully depends on the contents of the file. If this is encrypted text where the key for decryption is placed at the beginning of the file it might be enough to encrypt only this information since it is essential to interpret the rest of the data. But in most other cases it would probably be a bad idea to encrypt only a part.
For example imagine that this is a document containing passwords in plain text. In this case it is obvious that encrypting only part of it would be a bad idea because it would only protect a small number of passwords and leave the rest exposed. But also with most video or audio files it is possible to recover the majority of the content even if some parts are corrupt (or encrypted).
Imagine that you have a photo and want nobody to see it. So you open up an image editor and only blur a small part of it.
I can bet everyone can still guess what's on the photo.
This is exactly what you're trying to do with encryption, and this is also why it's a bad idea.
Modern hardware with AES-NI can do AES encryption at the speed well in excess of 1-2 GB/s. By comparison, your hard disk probably can only write at 50MB/s-100MB/s or SSD about 500MB/s-1GB/s. In other words, with properly implemented hardware accelerated encryption, it's unlikely that encryption speed is your bottleneck here.
If you have a huge file (tens of gigabytes or larger), and you want to be able to edit parts of the file without reencrypting/rewriting the entire file, that's a totally different problem. To be able to do this efficiently and securely, you need to understand about block cipher encryption modes. You'd want to have your encryption system to use encryption mode that supports random access securely, such as XTS mode. Note that encryption mode that supports efficient random access are often considered by some people to be not as strong as serial access encryption mode. You should research and consider if the supposed weaknesses of random access encryption is of concern to your particular scenario.
The easiest way you can use encryption mode that supports random access efficiently, is to mount the encrypted file as a block device/disk using full disk encryption software, like TrueCrypt/VeraCrypt, LUKS, or BitLocker.
30s is not a ton of time, so let it encrypt the entire file, if possible. If it freezes the OS (assuming you know you what that means), then let it encrypt 512 megabytes, or about 1/2 of the file, in that 30s. It seems just as safe to me, and saves you half the lost memory for essential processes. Any less and you risk attackers gaining this information. In short, you can encrypt a small part of the file, given that you encrypt exactly 1/2 of the bytes in this order:
GARRISON +ANANANAN --------- GNREIFOA
of which GARRISON is an arbitrary letter combo, ANANANAN is an arbitrary 2-letter Vigenere key that has an A in it. That A ensures every other letter encodes to itself. I wouldn't recommend using AAAANNNN, because enough of the plaintext is visible for you to decode the encrypted text by using a known plaintext attack. More than this is unnessecary, and (50+ MB) less than this makes it easier for attackers to crack the encryption. I am using text, but how do you think you are going to type 500 million (using UTF-16, 1 billion in ASCII and UTF-8) characters into notepad?! Using a Vernam cipher or an XOR (not reusing the key) may well be suitable for what you describe. XORing a message shouldn't take that long