/***************************************************** * Section : 15 * Machine No. : N * Roll No. : 19CS100XY * Name : Aritra Hazra * Assignment No : 6A * Description : Encrypt and Decrypt Character Strings *****************************************************/ #include #include #define MAX 100000 #define ASCII_BIT_WIDTH 8 #define CHAR_BIT_WIDTH 6 #define ENC_MAX MAX * ASCII_BIT_WIDTH / CHAR_BIT_WIDTH + 1 /* function to print charaters using given encodings */ void printEncryptString (int code) { if ( (code >= 0) && (code < 26) ) printf("%c", code+'A'); else if ( (code > 25) && (code < 52) ) printf("%c", code-26+'a'); else if ( (code > 51) && (code < 62) ) printf("%c", code-52+'0'); else if (code == 62) printf("+"); else if (code == 63) printf("-"); else /* unreached part */ printf("?"); } /* function to compute specified encodings for a given character */ int getCodeChar (char c) { if ( (c >= 'A') && (c <='Z') ) return (c-'A'); else if ( (c >= 'a') && (c <='z') ) return (c-'a'+26); else if ( (c >= '0') && (c <='9') ) return (c-'0'+52); else if (c=='+') return (62); else if (c=='-') return (63); else /* unreached part */ return (-1); } int main () { char str[MAX], encStr[ENC_MAX]; int i, j, k, strBin[ASCII_BIT_WIDTH * MAX], encStrBin[CHAR_BIT_WIDTH * MAX], asciiChar, lengthStr, fullPartLength, lastPartLength, codeChar; printf("\nEnter Original String: "); gets(str); lengthStr = strlen(str); /* compute Binary encodings from the ascii values of each charater of a string */ for (i=0; i 0) { strBin[j] = asciiChar % 2; asciiChar /= 2; j--; } while (j >= ASCII_BIT_WIDTH * i) { strBin[j] = 0; j--; } } /* encode in own format from Binary code using specified encoding scheme */ fullPartLength = (lengthStr * ASCII_BIT_WIDTH) / CHAR_BIT_WIDTH; lastPartLength = (lengthStr * ASCII_BIT_WIDTH) % CHAR_BIT_WIDTH; printf("The Encrypted String: "); for (i=0; i=CHAR_BIT_WIDTH * i; j--) { codeChar += strBin[j] * k; k *= 2; } printEncryptString(codeChar); } if (lastPartLength) { for (j = CHAR_BIT_WIDTH * fullPartLength + lastPartLength - 1, k=1, codeChar = 0; j>=CHAR_BIT_WIDTH * fullPartLength; j--, k*=2) codeChar += strBin[j] * k; printEncryptString(codeChar); } printf("\n\n"); printf("Enter Encrypted String: "); gets(encStr); lengthStr = strlen(encStr); /* compute Binary encodings from own encode values of each charater of a string */ for (i=0; i 0) { encStrBin[j] = codeChar % 2; codeChar /= 2; j--; } while (j >= CHAR_BIT_WIDTH * i) { encStrBin[j] = 0; j--; } } /* decode to string format from Binary code using ascii values */ fullPartLength = (lengthStr * CHAR_BIT_WIDTH) / ASCII_BIT_WIDTH; lastPartLength = (lengthStr * CHAR_BIT_WIDTH) % ASCII_BIT_WIDTH; printf("The Original String: "); for (i=0; i=ASCII_BIT_WIDTH * i; j--) { codeChar += encStrBin[j] * k; k *= 2; } printf("%c", codeChar); } if (lastPartLength) { for (j = ASCII_BIT_WIDTH * fullPartLength + lastPartLength - 1, k=1, codeChar = 0; j>=ASCII_BIT_WIDTH * fullPartLength; j--, k*=2) codeChar += encStrBin[j] * k; printf("%c", codeChar); } printf("\n\n"); return 0; }