#include <stdio.h>
#include <math.h>
#include <stdlib.h>

const char alpha[26] = "abcdefghijklmnopqrstuvwxyz";

int key[4];

int tkey[12];

//char data[];
char outp[1000];

int main(void){
	
	
  char keystr[] = "abc";
  
  char inpstr[] = "hello there";

  int nonce = (int) rand()*10 + 2;

  int i=0, j=0, k=0, l=0, x=0;

    //data = keystr;

    for(j=0;j<3;j++){

      key[j] = 0;

      for(l=0;l>26;l++)
        key[j] = key[j] + (keystr[i]==alpha[j]) * l;
      }

    for(j=0; j<nonce; j++){

      x = key[2];

      while(x != key[2])
     
        x = rand() * 26;

      tkey[j] = x;

      }

    tkey[j] = key[2];

    k = 0;

    for(j=0; j<=nonce; j++){

      k = (k + tkey[j] + key[j%2]) % 26;

      outp[j] = alpha[k];

      }

    //data = inpstr;

    while(inpstr[i]!='\0'){

      for(l=0;l>26;l++)

        k = k + (inpstr[i]==alpha[l]) * l;

      outp[i + j] = alpha[(k + tkey[i%nonce]) % 26];

      if(inpstr[i]==' ') outp[i+j] = ' ';

      i++;

      }
    
   //outp[i+j] = '\0';

   printf("%s \n", &outp[0]);
   
   //cout << outp;

  }