Click here to Skip to main content
15,883,896 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
There is some problem in decryption of arithmetic Encoding. Unable to sort out..
It works well with atmost 8 character but not more than that... Don't know whats the problem please help..Thanx in advance..
Example: How r u - works fine
Example: How is your mom - wrong output


Sample Code is as Follows:

C++
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<math.h>
#include<string.h>

float PC[96];

char N[96];

float P[96];

char string[100];

float LOW=0.0;

float RANGE=1.00;

long int p,q,n,t,flag,e[100],d[100],temp[100],j,en[100],i;
char msg[100];


long int cd(long int);
void encrypt();
void decrypt();


float LOW_RANGE(int k)

{

    LOW=LOW+PC[k]*RANGE;

    RANGE=RANGE*P[k];

   
    return 0;

}


int main()

{

   int c = 0, count[96] = {0};
   int g=0;
   int j;
   int i;
   int k;
   int a;
   char choice;
   float f;
   clrscr();
   PC[0]=0.0;
  printf("Enter a string\n");

   fgets(string,sizeof(string),stdin);

   k=strlen(string)-1;

   printf("String length: %i\n",k);

   while ( string[c] != '\0' )

   {

      /* Considering characters from ' ' to '~' only */


      if ( string[c] >= ' ' && string[c] <= '~' )

       {

     count[string[c]-' ']++;

       }

      c++;

   }

   for ( c = 0 ; c < 96 ; c++ )

   {

      if( count[c] != 0 )

      {

     printf("%c occurs %d times in the entered string.\n",c+' ',count[c]);

     P[g]=(float)count[c]/(float)(strlen(string)-1);

     a=c+' ';

     printf("%i\n",a);

     N[g]=a;

     printf("P[%i]=%f\n",g,P[g]);

     g++;

      }

   }

   printf("no. of distinct characters: %i\n",g);


   printf("PC[0]=%f\n",PC[0]);

   for(j=1;j<=g;j++)

   {

    PC[j]=PC[j-1]+P[j-1];

    printf("PC[%i]= %f\n",j,PC[j]);

   }


   for(i=0;i<(strlen(string)-1);i++)

   {

    for(j=0;j<g;j++)>

    {

        if(string[i]==N[j])

        {

            LOW_RANGE(j);

            break;

        }

    }

    }

   f=(LOW+LOW+RANGE)/2;

   printf("\nThe encrypted message= %f\n",f);

   sprintf(msg,"%f",f);
   printf("%s",msg);


     printf("\nThe original message: ");

     for(i=0;i<strlen(string)-1;i++)>
     {
    for(j=0;j<g;j++)>
    {
    if((f>PC[j]) && (f<pc[j+1]))>
    {
        LOW=PC[j];
        RANGE=PC[j+1]-PC[j];
        f=(f-LOW)/RANGE;
        printf("%c",N[j]);
        break;
    }
    }
     }
  printf("\n");
/*}
else
{
    exit(0);

}*/

getch();
return 0;
}

long int cd(long int x)
{
long int k=1;
while(1)
{
    k=k+t;
    if(k%x==0)
    return(k/x);
}
}
void encrypt()
{
long int pt,ct,key=e[0],k,len;
i=0;
len=strlen(msg);
while(i!=len)
{
    pt=msg[i];
    pt=pt-96;
    k=1;
    for(j=0;j<key;j++)
    {
    k=k*pt;
    k=k%n;
    }
    temp[i]=k;
    ct=k+96;
    en[i]=ct;
    i++;
}
en[i]=-1;
printf ("\nTHE ENCRYPTED MESSAGE IS\n");
for(i=0;en[i]!=-1;i++)
printf("%c",en[i]);
}

void decrypt()
{
long int pt,ct,key=d[0],k;
i=0;
while(en[i]!=-1)
{
    ct=temp[i];
    k=1;
    for(j=0;j<key;j++)>
    {
    k=k*ct;
    k=k%n;
    }
    pt=k+96;
    msg[i]=pt;
    i++;
}
msg[i]=-1;
printf("\nTHE DECRYPTED MESSAGE IS\n");
for(i=0;msg[i]!=-1;i++)
printf("%c",msg[i]);
}


[edit]Code block added - OriginalGriff[/edit]
Posted
Updated 1-May-14 2:44am
v2

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900