Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- let testN=100;
- let roundCount=1000;
- let message="абракадабра";
- let m=15;
- const alphaFactory= (symbol,code) => {
- return {
- symbol,
- code
- }
- }
- let alpha=[];
- alpha.push(alphaFactory('й','10'));
- alpha.push(alphaFactory('ц','10'));
- alpha.push(alphaFactory('у','10'));
- alpha.push(alphaFactory('к','10'));
- alpha.push(alphaFactory('е','10'));
- alpha.push(alphaFactory('н','10'));
- alpha.push(alphaFactory('г','10'));
- alpha.push(alphaFactory('ш','10'));
- alpha.push(alphaFactory('щ','10'));
- alpha.push(alphaFactory('з','11'));
- alpha.push(alphaFactory('х','111'));
- alpha.push(alphaFactory('ъ','112'));
- alpha.push(alphaFactory('ф','113'));
- alpha.push(alphaFactory('ы','114'));
- alpha.push(alphaFactory('в','115'));
- alpha.push(alphaFactory('а','116'));
- alpha.push(alphaFactory('п','117'));
- alpha.push(alphaFactory('р','118'));
- alpha.push(alphaFactory('о','119'));
- alpha.push(alphaFactory('л','120'));
- alpha.push(alphaFactory('д','121'));
- alpha.push(alphaFactory('ж','122'));
- alpha.push(alphaFactory('э','123'));
- alpha.push(alphaFactory('я','124'));
- alpha.push(alphaFactory('ч','125'));
- alpha.push(alphaFactory('с','126'));
- alpha.push(alphaFactory('м','127'));
- alpha.push(alphaFactory('и','128'));
- alpha.push(alphaFactory('т','129'));
- alpha.push(alphaFactory('ь','130'));
- alpha.push(alphaFactory('б','131'));
- alpha.push(alphaFactory('ю','132'));
- alpha.push(alphaFactory('ё','133'));
- //Необходимые функции
- const breakeNumber = number => {
- let buf=0;
- while (number%2===0) {
- buf++;
- number=number/2;
- }
- return [buf,number]; //buf - степень двойки s, t; number - t
- }
- const checkSimple = (n,roundCount) => {
- let buf=n-1;
- let flag=0;
- let s=breakeNumber(buf)[0];
- let d=breakeNumber(buf)[1];
- for (let i=0;i<roundCount;++i) {
- let a=Math.floor((Math.random()*(n-4))+2);
- if (Math.pow(a,d)%n===1) {
- return true;
- }
- for (let j=0;j<s-1;++j) {
- let buf=Math.pow(a,(d*Math.pow(2,j)));
- if (buf%n===-1) return true;
- }
- }
- return false;
- }
- const checkIsPervSqrt = (A,P) => {
- let buf=[];
- for (let i=1;i<P;++i){
- let bufNumb=Math.pow(A,i)%P;
- if (buf.includes(bufNumb)) { return false;}
- else {
- buf.push(bufNumb); }
- }
- return true;
- }
- const findSqrt=P => {
- for (let i=1;i<P;++i) {
- if (checkIsPervSqrt(i,P)) {
- return i;
- }
- }
- }
- const genRand = (min,max) => {
- return Math.floor(Math.random()*(max-min)+min);
- }
- const codeMessage = (message) => {
- let outputString=[];
- for (let i=0;i<message.length;++i) {
- let buf=message[i];
- console.log(buf);
- for (let j=0;j<alpha.length;++j) {
- if (alpha[j].symbol===buf) {
- outputString+=alpha[j].code;
- }
- }
- }
- return outputString;
- }
- //НОД
- const gcd=(a,b) => {
- if (b==0) return a;
- return gcd(b,a%b);
- }
- const getK = (P) => {
- let buf=Math.floor(Math.random()*P);
- while (gcd(P,buf)!=1) {
- buf=Math.floor(Math.random()*P);
- }
- return buf;
- }
- //Основная часть
- //Кодировка символов
- console.log(codeMessage(message));
- console.log(alpha);
- //Генерируем большое просто число больше сотки с проверкой на простоту
- //Далее для него же находим первообразную в том же
- let A,P;
- while (typeof(A)==='undefined'||typeof(P)==='undefined') {
- P=genRand(80,200);
- if (checkSimple(P,100)) {
- A=findSqrt(P);
- }
- }
- console.log(`A=${A} P=${P}`);
- ///Первый пользователь
- let x=genRand(2,P/2);
- let y=Math.pow(A,x)%P;
- console.log(`m=${m} x= ${x} y=${y}`);
- //шифрование
- let k=getK(10);
- console.log(`k= ${k}`);
- let gamma=Math.pow(A,k)%P;
- let betta=m*Math.pow(y,k)%P;
- console.log(gamma);
- console.log(betta);
- console.log((betta*Math.pow(gamma,P-1-x))%P);
Add Comment
Please, Sign In to add comment