Click here to Skip to main content
15,880,891 members
Articles / Security / Cryptography

Encrypting Text in C# with this Simple M3 Algorithm, Text Encryption Made Easy

Rate me:
Please Sign up or sign in to vote.
4.69/5 (51 votes)
21 Jan 2017CPOL5 min read 110.7K   2.1K   80   32
Encryption algorithm based on key self-mutation. Complete source code and samples.

Introduction

You don't need to be an encryption-expert to encrypt your text in C# with this simple to implement Symmetric Encryption algorithm. The algorithm itself is very complex and secure, but using it is as simple as it can be.

Using the Code

Insert the class from the downloaded source code inside your namespace, and you will be able to instantiate objects of the class.

Encryption

The following code block is a short example of how to encrypt the string variable YourTextString with the encryption key variable YourEncryptionKey.

C#
M3encryption.Key = YourEncryptionKey;
M3encryption objEncrypt = new M3encryption();
objEncrypt.ClearTekst = YourTextString; 
objEncrypt.Encrypt();
if (objEncrypt.errorState > -1) {       
    MessageBox.Show(objEncrypt.EncryptedTekst);
}

Check the property objEncrypt.errorState for errors before proceeding in your code.

errorState -1:

This means the text encrypted contained characters outside of the UTF-16 range. These characters were changed to "?" in the cleartext before encryption.

errorState -2:

No encryption key was provided. The encryption process was aborted.

Decryption

C#
M3encryption.Key = YourEncryptionKey;
M3encryption objEncrypt = new M3encryption();
objEncrypt.EncryptedTekst = YourTextString; 
objEncrypt.Decrypt();
if (objEncrypt.errorState > -1) {       
    MessageBox.Show(objEncrypt.ClearTekst);
}

Randomization

Setting the property Randomization = true will produce a different cipher-text by the same key and clear text when run multiple times.

Randomization = false is default.

C#
M3encryption.Randomization = true;

Fastmode

Setting the property Fastmode = true will make the encryption/decryption process faster, but also a little less secure. If you're encrypting small strings like passwords, card numbers, etc., you should not do it in Fastmode, but if you're encrypting documents like several pages long, you should do it in Fastmode.

Fastmode = false is default.

Notice! Decryption should be done in the same mode it was encrypted in.

C#
M3encryption.Fastmode = true;

The Inner Workings of the Algorithm

General

The basic principle of this algorithm is character-remapping based on key self-mutation.

The lifespan of a key is equal to the length of the key. This means that any state of the key will only be responsible for encrypting a part of the clear-text that is equal in length to the length of that version of the key before the key is self-mutated into a new version. This new version will then encrypt the next part of the clear-text, etc.

In the overall process, you have a clear-key entered by the user which is diverted into 4 separate "threads" of different and constantly self-mutating keys. These 4 different keys are responsible for simultaneously converting the clear-text letters one letter at a time into cipher text by 2 different methods, these methods being: array remapping, and a sort of dynamic "substitution cipher". This whole process is repeated over and over again, re-encrypting everything a number of times before the cipher-text is finalized.

An attempt of reversing the process by a potential attacker would require figuring out the end state of 4 different keys simultaneously going backwards one mutation-version at a time. As 2 of these keys are used for array remapping, it is necessary to get the whole of these keys per letter decoded in the clear-text.

The bootstrap Process

As the algorithm uses self-mutating keys, it needs a bootstrap process to initiate the first encryption of the clear text key in a secure way. It's kinda like a "chicken or the egg" situation, in order to encrypt the key, it needs a mapping array which was produced by an encrypted key, but the key can't be encrypted without a mapping array. Alternatively, the mapping array could be produced by the clear text key, or it could be produced by a static hard-coded array, but that would make it a little less secure.

The bootstrap process consists of the following 4 steps:

  1. A mapping array will be produced by the cleartext key
  2. The cleartext key is encrypted by the mapping array
  3. A new mapping array is produced by the newly encrypted key
  4. A new key is encrypted by the newly created mapping array

The product will be a key encrypted by a mapping array which was produced by an encrypted key, and a mapping array for encrypting keys which was produced by an "unknown" encrypted key.

This is just the initial encryption of the key though, the key will self mutate multiple times throughout the entire encryption process. The mapping array for the "self-mutating inner key" will likewise be recreated multiple times before the final cipher-text is produced.

Outer and Inner Loops

The algorithm consists of an outer and inner loop. The outer loop iterates the rounds, the inner loop iterates the entire clear text one character at a time.

Self-mutating Outer and Inner Keys

The Self-mutating Inner Key

This key is used for encryption in the inner encryption process. Each letter of the key is used for encrypting the letter in the respective position in the clear text. When the key reaches the end of its string, the process will continue in a new mutated version of the key. This process will continue until the entire clear text string has been encrypted. See Figure 1 underneath for an illustration of this process. The self-mutating inner key is marked in blue.

Click to enlarge image

Figure 1. The key pattern in the above illustration will never repeat itself throughout the entire encryption process.

The Self-mutating Outer Key

The self-mutating outer key is initiated from the key entered by the user. This key is used in the outer loop and is self-mutated in the beginning of each round. The purpose of the key is to generate a unique mapping array for each round and to initiate the self-mutating inner key in these rounds. The mapping array is used by the encryption process in the inner loop.

Example of the key self-mutating in a 6 rounds iteration:

Key entered by the user xxxxxxxxxxxx
Round 1 xX7IOiI:Ie7N
Round 2 &8Xy4@obt $_W
Round 3 3cj`#sie391_?&
Round 4 +sp=VGjHV>~tQ|C
Round 5 J_fe2brc'3Rguxt^
Round 6 T2MV)X!CXV"2sUp{d

History

  • 20th December, 2015: Initial version

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer
Denmark Denmark
IT-Security
Server management
Development (C#)

Comments and Discussions

 
QuestionKey Size Pin
Member 1362817712-Mar-18 21:19
Member 1362817712-Mar-18 21:19 
Suggestionoutdated error handling Pin
sx200827-Jan-17 14:19
sx200827-Jan-17 14:19 
QuestionA few questions Pin
CodesInChaos7-Nov-16 7:32
CodesInChaos7-Nov-16 7:32 
GeneralWhy reinventing the wheel ? Pin
ObiWan_MCC7-Nov-16 3:22
ObiWan_MCC7-Nov-16 3:22 
Questionsevere encryption ... how to decrypt ? Pin
InvisibleMedia4-Nov-16 10:23
professionalInvisibleMedia4-Nov-16 10:23 
QuestionReasoning Pin
ArchAngel1234-Nov-16 10:04
ArchAngel1234-Nov-16 10:04 
AnswerRe: Reasoning Pin
Michael_Jacobsen4-Nov-16 10:14
Michael_Jacobsen4-Nov-16 10:14 
GeneralRe: Reasoning Pin
Member 80241957-Nov-16 8:50
Member 80241957-Nov-16 8:50 
GeneralRe: Reasoning Pin
PIEBALDconsult7-Nov-16 9:04
mvePIEBALDconsult7-Nov-16 9:04 
AnswerRe: Reasoning Pin
Howard Duane Allman4-Nov-16 11:00
professionalHoward Duane Allman4-Nov-16 11:00 
QuestionSource code Pin
RochefortSandro26-Oct-16 8:06
RochefortSandro26-Oct-16 8:06 
AnswerRe: Source code Pin
Michael_Jacobsen26-Oct-16 8:47
Michael_Jacobsen26-Oct-16 8:47 
QuestionshuffleKeyToEndState Pin
Member 1172068114-Oct-16 2:25
Member 1172068114-Oct-16 2:25 
AnswerRe: shuffleKeyToEndState Pin
Michael_Jacobsen14-Oct-16 3:34
Michael_Jacobsen14-Oct-16 3:34 
GeneralRe: shuffleKeyToEndState Pin
Member 1172068114-Oct-16 9:40
Member 1172068114-Oct-16 9:40 
GeneralRe: shuffleKeyToEndState Pin
Michael_Jacobsen14-Oct-16 10:20
Michael_Jacobsen14-Oct-16 10:20 
GeneralRe: shuffleKeyToEndState Pin
Member 1172068114-Oct-16 10:44
Member 1172068114-Oct-16 10:44 
GeneralRe: shuffleKeyToEndState Pin
Michael_Jacobsen14-Oct-16 10:55
Michael_Jacobsen14-Oct-16 10:55 
Questionthe Jacobson M3 algorithm appears to have a defect Pin
Member 1172068113-Oct-16 17:17
Member 1172068113-Oct-16 17:17 
AnswerRe: the Jacobson M3 algorithm appears to have a defect Pin
Michael_Jacobsen14-Oct-16 0:28
Michael_Jacobsen14-Oct-16 0:28 
QuestionStateful IV Pin
bling21-Dec-15 12:30
bling21-Dec-15 12:30 
AnswerRe: Stateful IV Pin
Michael_Jacobsen21-Dec-15 22:00
Michael_Jacobsen21-Dec-15 22:00 
QuestionKey length Pin
Budsy21-Dec-15 11:31
Budsy21-Dec-15 11:31 
AnswerRe: Key length Pin
Michael_Jacobsen21-Dec-15 21:53
Michael_Jacobsen21-Dec-15 21:53 
QuestionUse? PinPopular
ArchAngel12321-Jul-15 11:10
ArchAngel12321-Jul-15 11:10 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.