• ClassDatenowKey2.js
    The random number generator:

    //ClassDatenowKey2.js
    //a permutation class
    function Perm(n){
     this.N=n;
     this.C=new Uint8Array(n);
    }
    
    Perm.prototype.swap=function(i,j){
     var a=0;
     a=this.C[i];
     this.C[i]=this.C[j];
     this.C[j]=a;
    };//end swap
    
    //Initialize a random permutation
    Perm.prototype.createPerm=function(){
      var i,j;
      var k=this.N;
      E.srand(E.hwRand());
      for(j=0;j<this.N;j++)this.C[j]=j;
      for(i=0;i<(this.N-1);i++){
       j=parseInt((Math.random()*256)%k,10);
       this.swap(i,i+j);
       k--;
      }//next i
    };//end createPerm
    
    //Use permutation to relocate bits in E to D
    //dir determines the indirection direction
    Perm.prototype.doPerm=function(dir,D,E){­
     var i,j,k,m;
     for(i=0;i<this.N/8;i++)D[i]=0;
     for(i=0;i<this.N;i++){
      j=this.C[i];
      k=i%8;
      m=j%8;
      var a=1;
      var b=1;
      a=1<<k;
      b=1<<m;
      if(dir){
       if(E[(i-k)/8]&a) D[(j-m)/8]=D[(j-m)/8]|b;
      }else{
       if(E[(j-m)/8]&b) D[(i-k)/8]=D[(i-k)/8]|a;
      }//end else
     }//next i
    };//end doPerm
    
    //Use permutation of length N in E to relocate bytes in C to D
    Perm.prototype.doPermBytes=function(dir,­E,C){
     var i,j,k,m;
     for(i=0;i<this.N;i++)D[i]=0;
     for(i=0;i<this.N;i++){
      j=this.C[i];
      if(dir){
       D[i]=E[j];
      }else{
       D[j]=E[i];
      }//end else
     }//next i
    };//end doPermBytes
    ///////////////////////////
    
    //a oneway class
    
    function Oneway(){
     this.A=[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]­;
    }
    
    Oneway.prototype.oneway1=function(mode,A­,B,C,D,E){
      switch(mode){
        case 0:
         return A+B+C+D+E;
        case 1:
         return A^B^C^D^E;
        case 2:
         return A*B*C*D*E;
      }//end switch
    };//end oneway1
    
    Oneway.prototype.modit=function(A){
     for(var i=0;i<16;i++)A[i]=this.A[i]%256;
    };//end modit
    
    Oneway.prototype.fillit=function(b){
     for(var i=0;i<16;i++)this.A[i]=b;
    };//end fillit
    
    Oneway.prototype.oneway=function(mode,B)­{
      var oneway1=this.oneway1;
    //rows
     this.A[0]=oneway1(mode,this.A[0],B[0],B[­1],B[2],B[3]);
     this.A[1]=oneway1(mode,this.A[1],B[4],B[­5],B[6],B[7]);
     this.A[2]=oneway1(mode,this.A[2],B[8],B[­9],B[10],B[11]);
     this.A[3]=oneway1(mode,this.A[3],B[12],B­[13],B[14],B[15]);
    //columns
     this.A[4]=oneway1(mode,this.A[4],B[0],B[­4],B[8],B[12]);
     this.A[5]=oneway1(mode,this.A[5],B[1],B[­5],B[9],B[13]);
     this.A[6]=oneway1(mode,this.A[6],B[2],B[­6],B[10],B[14]);
     this.A[7]=oneway1(mode,this.A[7],B[3],B[­7],B[11],B[15]);
    //diagonals\
     this.A[8]=oneway1(mode,this.A[8],B[0],B[­5],B[10],B[15]);
     this.A[9]=oneway1(mode,this.A[9],B[1],B[­6],B[11],B[12]);
     this.A[10]=oneway1(mode,this.A[10],B[2],­B[7],B[8],B[13]);
     this.A[11]=oneway1(mode,this.A[11],B[3],­B[4],B[9],B[14]);
    //diagonals/
     this.A[12]=oneway1(mode,this.A[12],B[3],­B[6],B[9],B[12]);
     this.A[13]=oneway1(mode,this.A[13],B[0],­B[7],B[10],B[13]);
     this.A[14]=oneway1(mode,this.A[14],B[1],­B[4],B[11],B[14]);
     this.A[15]=oneway1(mode,this.A[15],B[2],­B[5],B[8],B[15]);
    };//end oneway
    
    // Random object
    // aes1 0= encrypt, 1=decrypt
    // aes2 0= encrypt, 1=decrypt
    // pdir1 permutation direction 0 or 1
    // pdir2 permutation direction 0 or 1
    // owtype 0= add, 1= xor, 2=multiply
    function Random(aes1,aes2,pdir1,pdir2,owtype){
     this.AES1=aes1;
     this.AES2=aes2;
     this.Pdir1=pdir1;
     this.Pdir2=pdir2;
     this.Owtype=owtype;
    
     this.ow=new Oneway();
     this.plain=new Uint8Array(16);
     this.R=new ArrayBuffer(16);
     this.perm1=new Perm(128);
     this.key1=new Uint8Array(16);
     this.perm2=new Perm(128);
     this.key2=new Uint8Array(16);
    }
    
    Random.prototype.random=function(){
      var a;
    var tt=Date.now()*1000000;
    for(var i=0;i<16;i++){
     if(i===8)tt=Date.now()*1000000;
     this.R[i]=tt%256;
     tt=tt-this.R[i];
     tt=tt/256;
    }//nexti
    E.srand(E.hwRand());
    this.R[0]=(Math.random()*256)%256;
    this.R[8]=(Math.random()*256)%256;
    this.R[15]=(Math.random()*256)%256;
    //  printit(R);
    this.perm1.doPerm(this.Pdir1,this.plain,­this.R);
    //printit(plain);
    if(this.AES1){this.R=AES.encrypt(this.pl­ain,this.key1);
     }else{
     this.R=AES.decrypt(this.plain,this.key1)­;
    }
    //printit(this.R);
    
    this.ow.fillit(1);
    this.ow.oneway(this.Owtype,this.R);
    this.ow.modit(this.R);
    //printit(this.R);
    
    this.perm2.doPerm(this.Pdir2,this.plain,­this.R);
    //printit(plain);
    if(this.AES2){this.R=AES.encrypt(this.pl­ain,this.key2);
     }else{
     this.R=AES.decrypt(this.plain,this.key2)­;
    }
    //printit(this.R);
     return this.R;
    };//end random
    
    Random.prototype.setup_random=function()­{
     for(var i=0;i<16;i++){
      E.srand(E.hwRand());
      this.key1[i]=parseInt((Math.random()*256­),10);
      E.srand(E.hwRand());
      this.key2[i]=parseInt((Math.random()*256­),10);
     }//nexti
    
     this.perm1.createPerm();
     this.perm2.createPerm();
    
    };
    ///////////////////////////////
    
    function printit(W){
    console.log(W[0]+','+W[1]+','+W[2]+','+W­[3]+','+
                W[4]+','+W[5]+','+W[6]+','+W[7]+','+
                W[8]+','+W[9]+','+W[10]+','+W[11]+','+
                W[12]+','+W[13]+','+W[14]+','+W[15]
               );
    }//end printit
    
    function copy(A,B){for(var i=0;i<16;i++)A[i]=B[i];}
    
    function compare(A,B){
     for(var i=0;i<16;i++)if(A[i]!=B[i])return 0;
     return 1;
    }
    ///////////////////////////////
    // Random object
    // aes1 0= encrypt, 1=decrypt
    // aes2 0= encrypt, 1=decrypt
    // pdir1 permutation direction 0 or 1
    // pdir2 permutation direction 0 or 1
    // owtype 0= add, 1= xor, 2=multiply
    //function Random(aes1,aes2,pdir1,pdir2,owtype){
    var R=new Random(1,1,0,0,0);
    R.setup_random();
    console.log("keys");
     printit(R.key1);
     printit(R.key2);
     console.log("Enter go(); into left screen, press enter");
    function go(){
      R.key1=R.random();
      R.key2=R.random();
      R.key1=R.random();
      R.key2=R.random();
    console.log("keys");
     printit(R.key1);
     printit(R.key2);
    console.log("Randoms");
    for(var j=0;j<16;j++){
     printit(R.random());
    }
    }//end go
    

    The output:

    >echo(0);
    keys
    95,199,130,197,32,174,21,94,190,94,212,0­,219,24,220,209
    127,253,226,16,70,146,185,11,202,192,44,­70,83,51,120,177
    Enter go(); into left screen, press enter
    =undefined
    >go();
    keys
    37,115,239,249,173,235,105,20,202,187,11­9,250,173,235,105,187
    194,44,127,205,23,115,36,6,237,95,250,21­0,33,201,23,211
    Randoms
    142,254,220,240,136,215,19,239,149,77,77­,124,190,204,102,97
    172,247,225,194,124,184,176,179,201,85,7­0,33,145,196,50,223
    171,173,84,67,240,166,53,84,189,241,190,­160,65,126,46,16
    240,110,20,89,34,209,82,26,84,141,66,199­,178,107,45,129
    8,76,198,85,81,141,148,70,243,37,27,18,1­01,39,53,75
    156,154,240,83,125,40,83,111,48,193,73,9­0,88,52,152,150
    132,58,124,16,109,99,134,187,72,139,84,1­09,254,5,163,173
    18,166,184,35,1,188,74,73,75,24,107,129,­229,6,203,35
    46,71,178,73,32,142,191,110,254,127,233,­195,82,219,214,121
    219,68,7,110,28,230,173,64,123,179,128,4­,232,61,157,244
    26,86,10,201,79,18,159,126,178,165,128,2­13,0,220,233,219
    160,45,66,180,92,106,130,148,224,180,18,­141,20,194,197,167
    116,187,81,184,197,46,64,197,107,157,58,­72,70,42,241,205
    106,243,200,96,187,42,68,165,111,85,121,­61,150,128,81,63
    112,139,103,246,27,71,38,240,100,136,250­,230,24,145,76,69
    96,130,236,218,128,193,47,234,34,3,10,30­,130,18,74,154
    
    

    1 Attachment

About