📜 ⬆️ ⬇️

My encryption algorithm

Not so long ago, I faced the task of encoding user correspondence. The goal of the task was to send the already encoded string from user A to user B. The string is encoded and decoded using a key that is known to both. It is understood that the message from user A is sent to server B by user, where user B retrieves it. To avoid receiving data in the case of a third party receiving the message by intercepting the message, or accessing the server where it is stored, it was decided to organize the function in JavaScript, which allows users to send the encoded string directly from the browser window.

Having run through some encryption methods, I decided to write my own algorithm. It was decided to reduce the essence of the algorithm to mixing each individual character in some unique value mixed with a key, so that the value that mixes these characters is unique and is formed from the specified password or key. The creative idea for writing just such an algorithm was the El-Gamal cipher, the Punycode and Base64 transform method. I do not claim the Nobel Prize, but nevertheless I decided to share my own creation and ...

passCode - password, key. Since the user sets this parameter independently, and it can be quite simple, I additionally encode it in MD5.
Incode - encoded string

function txtencode(Incode, passCode) { //       ,            ,        var b52 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"; //               var maxPC = ifPC = 0; //  maxPC.            for(var i=0; i<passCode.length; i++) maxPC += passCode.charCodeAt(i); // maxPCmod    ,          //   maxPC ,   ,    maxPCmod  ,     0. maxPCmod = maxPC; //  .   . var rexcode = ""; //    : passCode.charCodeAt(numPC) //            var numPC = 0; //    for(var i=0; i<Incode.length; i++) { //    ,       if(numPC == passCode.length) numPC = 0; //  maxPCmod  ,    . if(maxPCmod < 1) maxPCmod = maxPC+ifPC; //       maxPCmod,     ,       . //        maxPCmod        //     maxPCmod, maxPC  ifPC          ,     -   ifPC += maxPCmod % passCode.charCodeAt(numPC); //  ,         var iscode = maxPCmod % passCode.charCodeAt(numPC); // ,         var nCode = (Incode.charCodeAt(i)+iscode); //  maxPCmod     maxPCmod -= passCode.charCodeAt(numPC); //     numPC++; //     . //     52    ,   . // 22  22*52+2,          . rexcode += parseInt(nCode / 52) + b52.charAt(parseInt(nCode % 52)); } //   return rexcode; } 

')
The decoding function is almost the same as the previous one, with a few exceptions.

 function txtdecode(Incode, passCode) { var b52 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"; var maxPC = 0; for(var i=0; i<passCode.length; i++) maxPC += passCode.charCodeAt(i); maxPCmod = maxPC; ifPC = 0; //   ,        var Incode = Incode.match(/\d+\w/g); var rexcode = ""; var numPC = 0; for(var i=0; i<Incode.length; i++) { if(numPC == passCode.length) numPC = 0; if(maxPCmod < 1) maxPCmod = maxPC+ifPC; ifPC += maxPCmod % passCode.charCodeAt(numPC); var iscode = maxPCmod % passCode.charCodeAt(numPC); //    ,       var nCode = (parseInt(Incode[i])*52)+parseInt(b52.indexOf(Incode[i].substr(-1))); maxPCmod -= passCode.charCodeAt(numPC); numPC++; //      ,   rexcode += String.fromCharCode(nCode-iscode); } //   return rexcode. //     ,      ,     . return rexcode.replace(/&/g, "&").replace(/</g, "<").replace(/>/g, ">").replace(/ /g, " ").replace(/\r\n|\r|\n/g,"<br />").replace(/(https?\:\/\/|www\.)([--\d\w#!:.?+=&%@!\-\/]+)/gi, function(url) { return '<a target="_blank" href="'+ (( url.match('^https?:\/\/') )?url:'http://' + url) +'">'+ url +'</a>'; }); } 


The result can be seen on the websites avtodot.ru , baby-all-pro.ru and building-repairs.ru

Legal aspects of data encryption

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


All Articles