It is no exaggeration to say that the modern information society is supported by cryptographic technology. Nevertheless, we are benefiting from it without being particularly aware of cryptography. Moreover, cryptography has been around for a long time, about 100 BC. In addition, modern cryptography is trying to ensure high security by making full use of advanced mathematics, and eventually it will be derived from blockchain technology.
What is the length of this history and the breadth of defense! !!
So, I'm aware of the emo of cryptography, and I'm trying to implement and defeat cryptography in Python for knowledge maintenance and updates.
Cryptography is largely made up of three elements: ** key generation **, ** encryption **, and ** decryption **.
In particular, keys are a very important factor in cryptography. Rather, I think that cryptography is a technology of how to generate this key. This is because this key is used for both encryption and decryption and is directly related to security.
Depending on how the key is handled, it can be classified into two types: ** common key cryptography ** and ** public key cryptography **.
Symmetric-key cryptography uses the same key in both the encryption and decryption processes. This was the mainstream for a while after the birth of cryptography, but it had the disadvantage of costly key management because the keys had to be exchanged only by the parties without being known to a third party.
In order to overcome the drawbacks of common key cryptography, public key cryptography creates two types of keys, a private key and a public key, and literally discloses the public key, which drastically reduces the cost of key management. You can do it. Public key cryptography is often used because it can be used by opening the public key, which is compatible with the exchange of information on the Internet. In the case of public key cryptography, the private key and the public key must have a correspondence relationship like a tally, and at the same time, the private key must not be inferred from the public key. Therefore, mathematics such as prime factorization of large numbers and discrete logarithm problems are used.
Cryptography has changed since the first half of the 20th century. Until then, it was called classical cryptography, and cryptanalysis was mainly the job of linguists. This is because I used to make and decrypt the code while playing with the letters, such as shifting the alphabet by a few letters and using the correspondence table prepared in advance.
However, during World War II, the cryptographic war between Enigma developed by the German army and the cryptanalysis team led by British information scientist Turing became one of the triggers, and cryptography gradually became the work of mathematicians and informatics. It was becoming.
In addition, cryptography is a cat-and-mouse game of development and decipherment. In fact, the British troops who deciphered the Enigma hid the fact that they could decipher it for about 20 years. This is because if you announce that you have cracked it, you risk developing stronger ciphers.
This is just one way of looking at it, but because of this background, I think that cryptography can be understood relatively easily by learning from classical cryptography to modern cryptography. It is highly recommended to learn by tracing the genealogy of the development of cryptographic technology, because you can understand the differences such as which areas have been improved.
The Caesar cipher is said to be the oldest cipher (?) In the world. As the name suggests, it is a code created by Julius Caesar, who was active around the 1st century BC. This is to convert the characters by shifting the characters by 3 characters when the characters are arranged in alphabetical order. If you implement the encryption part in Python, it will be as follows. ASCII code is used for the part that shifts the characters.
caesar-cipher.py
def enc(text):
result = ""
for i in range(len(text)):
cha = text[i]
if(cha.isupper()):
result += chr((ord(cha) - 62) % 26 + 65)
else:
result += chr((ord(cha) - 94) % 26 + 97)
return print(result)
When actually encrypted, it looks like this.
caesar-cipher.py
enc("Caesar")
#Ciphertext: Fdhvdu
When decrypting, the reverse of encryption can be done, so the implementation is as follows.
caesar-cipher.py
def dec(text):
result = ""
for i in range(len(text)):
cha = text[i]
if(cha.isupper()):
result += chr((ord(cha) - 42) % 26 + 65)
else:
result += chr((ord(cha) - 74) % 26 + 97)
return print(result)
When executed, it looks like this.
caesar-cipher.py
dec("Fdhvdu")
#Deciphered text: Caesar
The key to Caesar cipher was to shift 3 characters. For example, if there is a spy and it is misaligned by 3 characters, that is the end of the volume. Therefore, a code was devised to change the third part, that is, the key part. That is what is called shift cryptography.
The encryption part is as follows. This time, the number of characters to shift is specified as an argument. You can create a function to generate the key, but I decided to specify it as an argument because it just returns the entered numerical value as it is.
shift-cipher.py
def enc(plaintext, rot):
if(rot > 25):
rot = rot % 26
result = ""
for i in range(len(plaintext)):
cha = plaintext[i]
if(cha.isupper()):
result += chr((ord(cha) + rot - 65) % 26 + 65)
else:
result += chr((ord(cha) + rot - 97) % 26 + 97)
return print(result)
Decryption is the reverse of encryption.
shift-cipher.py
def dec(encryptedtext, rot):
if(rot > 25):
rot = rot % 26
result = ""
for i in range(len(encryptedtext)):
cha = encryptedtext[i]
if(cha.isupper()):
result += chr((ord(cha) - rot - 39) % 26 + 65)
else:
result += chr((ord(cha) - rot - 71) % 26 + 97)
return print(result)
I'm planning to implement and defeat the classical ciphers of BC to the most advanced ciphers of today in Python, which also serves as maintenance and update of knowledge. When I tried it, I began to see the history of cryptography and the struggles of our predecessors, and it became unexpectedly fun.
For the time being, our immediate goal is to reach the zero-knowledge proof system and secret sharing that often appear in the blockchain area.