Question:
How do you write an encryption algorithm?
Temari
2012-04-23 16:57:22 UTC
I need something that's very detailed, and includes all of how you write the computer coded things, and all the rest of it please!
Three answers:
2012-04-23 20:12:40 UTC
What is the purpose of encrypting something? To change data that can only be changed back with they key it was encrypted with.



Therefore an encryption algorithm modifies data from a key.



A [very] basic encryption algorithm might read some data, and a number (say 10) to it and then move on the the next chunk of data. This would be a very easy encryption to crack but it is an example.



Another form of encryption is an XOR encryption. If your familiar with modifying data on the bit level you will know that XOR'ing two numbers will result in a different number. An encryption algorithm might use this.



Say it expected a key which was 10 bytes long. The encryption algorithm might read a byte of data, XOR it with the first byte of the key. Then it would XOR the next byte with the next byte in the key. I could keep doing this and when reaching the 10th byte, start XOR'ing with the first byte of the key again.



An even more advanced encryption algorithm might not XOR the first byte of the data with the first byte of the key, it might use XOR the data byte with the last key byte and then the second data byte with the second-to-last key byte and so on. It might even use every second byte from the key until the end then use the bytes it skipped, making it harder to crack.



Decryption would be just as easy as encrypting it, so long as you have the key, just instead of XOR'ing the data.



I wrote a simple C++ program that encrypts a byte and then decrypts it using the XOR method.





#include



using namespace std;



int main()

{

int dataByte = 12;

int keyByte = 65;



int encrypted = dataByte ^ keyByte;



cout << "Data byte: " << dataByte << "\n";

cout << "Key Byte: " << keyByte << "\n";



cout << "\n";



cout << "XOR'ing the data byte with the key byte results in excrypted data\n";

cout << dataByte << " ^ " << keyByte << " = " << encrypted << "\n";



cout << "XOR'ing the key byte with the encrypted byte decrypts the byte\n";

cout << keyByte << " ^ " << encrypted << " = " << (keyByte ^ encrypted) << "\n";



return 0;

}
Ratchetr
2012-04-23 17:16:03 UTC
Typically, you don't. For encryption, you find a library function that does the work for you. You want to pick a library that implements a well known and accepted encryption algorithm from a well known source.



Encryption is a very specialized field of Computer Science. There are a lot of really smart people (smarter than you and I) that have spent decades on this problem. You don't want to try and reinvent the wheel here. (Would you go to a Car forum and ask how to build your own wheels?)



See Wiki for more:
2016-12-01 01:49:17 UTC
properly i do no longer know approximately C, yet while it makes use of VCL then you somewhat ought to seem for encryption aspects as writing cryptography code by using your self is a few thing many human beings have a PHD in computing technology for. i would not advise writing crypting code your self - you will likely loose some years of your existence. What you're able to do even with the undeniable fact that, is to place in writing the GUI, the thread code, the enter/output flow code etc etc. maximum extreme element of undergo in concepts: Cryptography can take fairly some time and equipment factors. This in turn can freeze the GUI except you nevertheless technique the needs message queue from abode windows. subsequently i could strongly advise that the crytpo stuff is finished in a seperate thread which will enable the GUI to proceed clean itself. stable success.... i think of you may % it ;) J


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...