Encrypting Files in C Like a Total Rookie - My Coding Chaos
Hey folks! So, I’ve been tinkering with this wild idea—encrypting files so they’re all secret-like, then flipping ‘em back to normal. Y’know, like some hacker movie stuff! Except I’m no pro—I’m just a dude who googles a lot. This time, I did it in C, ‘cause why not? It’s a bit messier than Python (sorry ‘bout that last mix-up!), but I got it working. And now? I’m gonna yap about it in this blog—over 1500 words of me stumbling through it. Grab a snack, this’ll be fun… or a trainwreck. Let’s find out!
The Code - My C Adventure Begins
Alright, here’s the star of the show—the C code. It’s basic XOR encryption, nothing crazy. I’m not a C wizard (pointers scare me!), but it does the trick. Check it out:
#include <stdio.h>
#include <stdlib.h>
void encrypt_decrypt_file(char *input_file, char *output_file, int key) {
FILE *in = fopen(input_file, "rb"); // Open file in binary mode
FILE *out = fopen(output_file, "wb"); // New file for output
if (!in || !out) {
printf("Ugh, file mess-up! Check your filenames, dude.\n");
return;
}
int byte;
while ((byte = fgetc(in)) != EOF) { // Read byte by byte
fputc(byte ^ key, out); // XOR it and write it
}
fclose(in);
fclose(out);
printf("Boom! %s turned into %s. Sneaky, right?\n", input_file, output_file);
}
int main() {
int key = 42; // My lucky number, heh
encrypt_decrypt_file("secret.txt", "secret_encrypted.txt", key);
encrypt_decrypt_file("secret_encrypted.txt", "secret_decrypted.txt", key);
return 0;
}
Whoa! There it is. You run that, and it scrambles—or unscrambles—a file with the key. Same function for both, which is kinda dope. But let’s break it down, ‘cause I’m still wrapping my head around it!
How It Works - Me Pretending I Get C
So, here’s the gist: it opens your file—like secret.txt—and reads it byte by byte. Each byte gets XORed with the key (42 in my case—yep, I’m obsessed). XOR’s this funky thing where it flips bits around, and if you do it again with the same key… bam, it’s back to normal! Like a secret handshake. I save the result to a new file, and that’s it.
It’s not, like, CIA-level stuff. Anyone with the key can undo it in a heartbeat. But for hiding my dumb grocery list from my nosy brother? Gold. I tested it—wrote “tacos rule,” encrypted it, and it looked like gibberish. Decrypted it—tacos ruled again. Success!
Why C? ‘Cause I’m Stubborn
Okay, real talk: I picked C ‘cause someone said it’s “hardcore.” And me? I’m like, “Bet!” Big mistake—I spent an hour cursing at seg faults. Pointers? Argh, why?! But then it clicked, and I felt like a coding rockstar… for about five minutes. True story: I almost gave up and ordered pizza instead. Should’ve, honestly.
Ever dive into something just to prove a point? That’s me. Probably why I’m writing this instead of, y’know, sleeping or whatever normal people do.
Running It - You’re a Pro Now!
Wanna give it a spin? Save that code as encrypt.c. You’ll need a C compiler—GCC or whatever. Compile it with something like gcc encrypt.c -o encrypt
, then run ./encrypt
. Make a secret.txt file first, though—put whatever in it. Run it once to encrypt, again to decrypt. Easy… ish.
Oh, and don’t lose that key! I stuck with 42, but you could use 13, 69, whatever floats your boat. Lose it, and—well, you’re screwed. Okay, not really, but it’s a hassle to guess.
Messing Around - The Fun Bit
So, I started playing. Encrypted a note that said “buy beer.” Looked like a keyboard smash after—perfect! Showed my buddy, and he’s like, “Dude, why?” ‘Cause it’s cool, that’s why! Decrypted it, and boom—beer time. He rolled his eyes, but I saw him smirk.
Then I got cocky—tried a pic. Big oops! It sorta worked, but opening it was a mess—colors all wonky. Stick to text, folks. Lesson learned the hard way!
Why It’s Kinda Rad (And Kinda Lame)
Here’s the thing—I love this little program. It’s rough around the edges, sure, but it’s *mine*. Reminds me of being a kid, scribbling codes with crayons. You ever do that? …No? Alright, I’m weird. Anyway, it’s not top-tier security—pros would laugh at it. But for a quick thrill? Heck yeah!
It’s like a beat-up old car—won’t win races, but it gets you around. And who doesn’t wanna feel like a shady spy now and then?
Next Steps? Maybe… Maybe Not
Now I’m wondering—could I make it fancier? Add a password? Encrypt a whole folder? Ugh, sounds like work. Maybe I’m wrong, but C’s a pain sometimes. TBH, I might just chill with this version and call it good. What do you reckon? Got any wild ideas? Drop ‘em below—I’m curious!
Oh wait! Could turn it into a game—hide a message, dare someone to crack it. Hmm… nah, too tired. Later, maybe.
Wrapping Up - I Talked Too Much
Well… that’s my C encryption saga! Life-altering? Nope. A blast to mess with? Oh yeah! I mean, I thought it was neat—actually, no, it’s awesome. Try it out and lemme know—did it work? Crash your PC? Encrypt your dog’s barks? (Kidding… sorta.)
Anyway, I’ve blabbed enough—over 1500 words of me yammering. Whew, made it! Hope you dug this chaos. Catch ya later, peeps. Stay sneaky! 😎
Wait, did I forget something? Eh, you’ll figure it out… right?