r/learnprogramming • • 16d ago

Is it possible to program a cipher into my computer/keyboard?

Forgive me if this is a dumb question. I am genuinely awful with technology. Please speak to me like your grandmother who only just recently learned what the power button does.

In the last two years I came up with a cipher that started as simple letter substitution, but grew over time to include many bigrams and some trigrams as well. It’s decently difficult to crack at this point. It’s just a hobby of mine, but one I quite enjoy, and I wish I could type in my cipher on the computer. I know you can sort of do this making a custom font, but wouldn’t that require me to only use the standard amount of characters in the alphabet plus punctuation? For context, I have nearly 200 characters in my cipher now. It would be terribly sad for me to surrender so many of them to do this. I guess my idea was some sort of program that would automatically change a character to the appropriate symbol based on letter combinations or at the very least suggest the character as if it was a spelling suggestion.

Not sure if it’s possible or not but if it is I’d appreciate input!

8 Upvotes

28 comments sorted by

5

u/AcornAnomaly 16d ago

A font wouldn't work, anyway. Your information would only appear to be ciphered, and only to those who had your font.

If you took the texted and switched it to a different font, you'd see what was actually typed in.

The simplest way to do this would be to have a program with an input feature, that let you type in what you wanted ciphered, and then an output feature that outputs the texted after enciphering.

It sounds like that's not quite what you wanted, though.

It sounds like you want something that does the enciphering as you type? Like an enigma machine? Is that correct?

If so, it's possible to do, but it would be a lot more complicated than a simple input translator.

Do you want this input to just work on your program, or are you looking for something that works no matter what program you're typing in to?

0

u/Last_Spinach_2708 16d ago

Honestly I don’t actually care if it’s legitimately encrypted. It’s more just so I have the satisfaction of seeing it neatly on my screen. Actually being able to quickly swap text between fonts so I could potentially change other documents into my cipher to practice reading it would be awesome. As I said it’s really only a hobby for me, I initially started it so someone I lived with wouldn’t go through my private things but that persons been gone for years. Now I just do it because it’s fun lol

0

u/Refwah 15d ago

So you just want to create a font then, rather than work on a programmatic cypher

0

u/Last_Spinach_2708 15d ago

One I could actually have nearly 200 characters for. No idea how to do that

1

u/Refwah 15d ago

The majority of fonts already cover more than 200 characters

1

u/Last_Spinach_2708 15d ago

Oh neat I didn’t know that

3

u/Meisterthemaster 16d ago

I have really fun reading these comments, you all must have amazing grandmothers xD

2

u/Last_Spinach_2708 16d ago

LOL I think I maybe should’ve emphasized that point a little harder. I feel like I’m going to have a stroke 😭😭

19

u/DrShocker 16d ago

If we for example took your post and wanted to translate it into your code, do you have specific rules to follow to accomplish that? Then you can do it.

Doing it with just a keyboard may be tricky because they keyboard is only intended to tell programs what keys are pressed, not to actually render the text. That's the job of whatever program you're actually using and the font.

4

u/jbaker88 16d ago

OP is going to want a QMK or Via compatible keyboard and from there they can write their own firmware to output whatever they want

4

u/Refwah 16d ago

Are the characters you have in the Unicode character set https://en.wikipedia.org/wiki/Unicode

1

u/Merinther 15d ago

Are you saying you’ve created new characters? Drawn new letters/symbols, rather than just swapping regular letters?

1

u/Last_Spinach_2708 15d ago

Yep!

1

u/Merinther 15d ago

Then a font would be the only obvious option. If you have symbols for letter combinations, those can be encoded as ligatures, so when you type for example ”th” on the keyboard it comes out as one symbol.

1

u/Last_Spinach_2708 15d ago

Okay awesome thank you! I’ll look into this further!

3

u/Severe-Ladder 16d ago

I bet you could probably do it with an AutoHotKey script and the custom font

1

u/Shadowwynd 16d ago

This would be my solution. Start looking at auto hot keys.

2

u/mredding 16d ago

Computers don't care about your symbols. All it cares about in a cipher is the symbol space itself, enumerated. So your first symbol is 0, you second symbol is 1, etc. An 8-bit byte can store 255 values.

Computers don't see characters, only encodings, and it all reduces to binary. ASCII encoding takes up the first 127 values of Unicode, and everything beyond that is a multi-byte or wide encoding (both of which can be broken down into bytes).

If you just focus on ASCII English text and punctuation, then you can operate in byte-for-byte. As raw computer data, it doesn't matter WHAT a text editor or byte editor displays, it's been cyphered. And code breakers won't care about your special symbols, either.

BUT! Yes, you can program your cypher into a computer and output a custom encoding. And then yes, you can create a custom font that maps encodings to characters you draw. You'll be interested in a pixel font, which is just a bunch of pictures. Vector fonts store the characters as a series of brush strokes. I'd say ask google where or how to create your own font files, as I bet there are websites that let you draw pixel art or strokes and give you back the resultant file. I will warn you that fonts are an incredibly complex subject, and maybe ignorance is a bliss. There's also going to be some necessity for you to be aware of encodings - the front of ASCII encoding, and thus Unicode encoding, are a couple control blocks that feature things like tabs and newlines, which you're possibly going to want if you want your custom font to draw correctly in a text editor that supports custom fonts.

1

u/fllthdcrb 15d ago

An 8-bit byte can store 255 values.

Maximum value (unsigned) is 255. Since it starts at 0, that makes for 256 values.

1

u/mredding 15d ago

Damn. You know, after 37 years...

1

u/fllthdcrb 15d ago

Don't feel too bad. No one is immune to off-by-one errors.

2

u/captainAwesomePants 16d ago

What you want is something like an Enigma machine. The user types keys, and what comes out on the other side is a bunch of letters or characters that are basically noise without the right decryption technique.

I would suggest that the best way to do this would be in the keyboard itself. Many more expensive keyboards offer reprogrammable firmware, and you could add some custom handlers to QMK to cipher the keys you typed according to whatever scheme, so long as it's not too terribly complex. And you'd definitely want a key that switches back and forth between cipher and regular typing.

You'd need the output to be visible in some way, though. If you could find appropriate mappings for your bi/trigrams in existing unicode, that'd be ideal, but if not, you'd need to get into custom fonts. Which is doable, but you'd need to choose that font in every application you wanted to type in, which would be a real pain.

1

u/MacAddict81 16d ago edited 16d ago

To make everything you type into your computer automatically be encrypted in your cypher, which is what I think you're asking, you'd need either a Human Interface Device (HID) driver that takes the raw output of your keyboard and cyphers it using your transposition matrix (or whatever, you mentioned bigrams and trigrams, which I'm assuming means that certain characters have multiple outputs in the cipher to bypass basic frequency analysis, but I haven't really delved deeper into encryption much beyond a basic Caesar Cipher).

You could also do this with a microcontroller, and basically create a keyboard device that only outputs in your cipher using a RP2040, for instance. There are open-source designs and firmwares out there to be hacked upon.

The only downside I can see, if what I'm assuming is your goal is indeed your goal, is that if everything you type is ciphered, it diminishes your computer's functionality with interacting with the world outside of your local machine. For instance, if everything you type is ciphered, you'd have to work backwards through your cipher mentally if you wanted to type a URL in the address bar, or have a mode switch on your keyboard, so the output could come out unencrypted when necessary.

If all you want to do is encrypt an input text file using your custom cipher, you can write a simple program for that in numerous different languages with a basic knowledge from a language tutorial, especially if you wrote it like a text processing terminal command for a POSIX compatible environment (for instance, Linux), where anything from standard input would be ciphered, and the encrypted text output to standard output. If you have a clearly defined set of rules for your cipher, translating it to a set of steps in a programming language should be pretty straightforward once you learn the language. And since any language I can think of such as C, C++, Java, Python, Rust, etc. all can use standard input and output, it basically comes down to what one you want to learn.

1

u/kilkil 16d ago edited 16d ago

what's a bigram/trigram?

in general I would say yeah, what you're describing sounds computable, so it should be possible to program a computer to do it. only question is how easy/difficult it would be.

1

u/JGhostThing 16d ago

Yes, this is possible. You'll need to google the docs on how to reprogram your keyboard. It's easiest to put a driver in your system to modify the output of the keyboard. And you will have to relearn typing, unless you're inputting in plaintext and outputing your ciphertext.

1

u/Feathercrown 16d ago

Sounds like you could use a keyboard like the ones they use in China where you use multiple keys to type a single more complicated character

1

u/Ahren_with_an_h 16d ago

If you can figure out how to do it in AHK, you can figure out how to do it in QMK.

1

u/Available_Safe1818 16d ago

Uiua does this in it's online codepad. I think it's done via a Language Server Protocol, or would be if it wasn't in a web page. I know very little about LSPs. You could write a program that monitored files for saved changes and then re-writes the file, if it can determine what parts are already encrypted. But if you do that outside of the editor, the editor won't know about the changes or will be constantly warning you that the file has been changed externally. You could start with something that just encrypts or decrypts whole files.