⬆️ ⬇️

Encryption algorithm based on elementary cellular automata

Hello, dear inhabitants of Habr! In this publication (and, most likely, the cycle) I will talk about my implementation of one of the encryption algorithms. Why about implementation? Because the idea is not new, and it is impossible to assert that the idea belongs to me. But the method is quite interesting, so it’s worth finding out about it.



In this part I will briefly describe the principle of operation of the algorithm itself and my implementation.



Essence of the work



This section will describe the encryption algorithm. To describe it, we will answer some questions:





What happens inside? This is the most interesting! We feed each character of the input string to the elementary cellular automaton ! Why he?



  1. It's simple. The algorithm of the elementary cellular automaton is simple to understand and implement.



  2. It is fast. Even my (not the most successful) implementation works quickly enough (we will be convinced of it, having finished work).


How does this happen? Yes, very simple!



  1. We split the input string into characters
  2. The code of each character is divided into binary digits.
  3. Write this string of ones and zeros in the field of the cellular automaton.
  4. We calculate the evolution of the cellular automaton using the state transition rule and at some moment return the current state of the field to the caller.
  5. Transform this state back to character code.
  6. "Push" the character into the output string


Actions 2-6 are repeated for each character and the output line is ready, we return it.



Implementation



The link on GitHub I will give in the end of the description.



To implement my idea, the object-oriented programming language C ++ was used. For clarity and convenience, all entities identified in the first part were implemented as separate classes. They received logical names - Field and Rule.



Class rule



This is an auxiliary class that stores information about the state transition rule and contains functions for working with the rule.



Data



Each instance has only cell states stored in std :: vector in accordance with each neighborhood case. Why std :: vector? Because for the bool type there is a specialization of this container. At the same time, only 1 bit is allocated for each element, instead of a whole byte. Due to this, the amount of occupied memory is reduced by 8 times.



Functions





The class is easy to use and understand. But here there is a topic for discussion. Since it is used only in conjunction with an instance of the Field class, so why not make it internal? I do not have a clear answer to this question. And really, why not? You can make it private so that the objects are constructed as needed. It is tempting, but at the moment I think that he is quite independent. After all, you can define in advance several separate transition rules and encode strings with them that require different encryption methods. I do not want to deprive programmers of this convenience, so in my implementation the type Rule is declared separately from the type Field.



Field class



This is the main type in which the functions that directly perform the encoding are defined.



Data





Functions





Some tests



You can check the functionality of the program yourself at the link below. Here I will give some facts about performance that seemed interesting to me. Remember, they characterize only this program, but in no case the algorithm itself. Let's start!



To encrypt all Unicode characters using rule 110, my system took 645,000 ticks or approximately 0.645 seconds (average values ​​for 10 starts).



To encrypt a single Unicode character encoding using rule 110, it took 80 ticks or approximately 0.00008 seconds (average values ​​for 10 starts).



All Unicode characters using all 256 rules were encrypted in 133500000 ticks or approximately 134 seconds (average values ​​for 10 runs).



But the strangest thing is that you get the right results! Checked manually by personally encrypting some numbers.



Conclusion



In my opinion, it turned out quite well and clearly. From mistakes you can not escape, but who is not without sin. The implementation works correctly, does its job. Of course, it is still too early to use it, much is not fully implemented, but I threw you some food for thought and I hope that your attempts to implement this algorithm will soon appear.

Source: https://habr.com/ru/post/323302/



All Articles