Mastering C Programming: Transposition Cipher Implementation

Page 1


Transposition Cipher (loop)

A very simple transposition cipher encrypt(S) can be described by the following rules:

1. If the length of S is 1 or 2, then encrypt(S) is S.

2. If S is a string of N characters s1 s2 s3... sN and k = IN/2j, then enc(S)= encrypt(sk sk−1... s2 s1)+ encrypt(sN sN−1... sk+1) where + indicates string concatenation. For example, encrypt("Ok") = "Ok" and encrypt("12345678") = "34127856".

Write a program to implement this cipher, given an arbitrary text file input up to 16 MB in size. Start with the template program found at provided in the file loop.data.zip as a basis for your program. In this program, you will see a mostly complete function to read a file into a dynamically allocated string as required for this problem.

size t getstr( char **str, FILE *input ) { size t chars to read = BLOCK SIZE; size t length = 0; II ...snipped... see template file size t chars = 0; while( ( chars = fread( *str + length, 1, chars to read, input ) ) ) { II you fill this out } II ...snipped... see template file return length; }

Read through the code carefully, make sure you understand it, and complete the inner part of the while loop. Look up realloc and the <string.h> header. If you have any questions about the provided code or don’t know why something is structured the way it is, please ask about it on Piazza.

You will also see an empty function “encrypt”, which you should fill out.

void encrypt( char *string, size t length ) { II you fill this out

Resource Limits

For this problem you are allotted 3 seconds of runtime and up to 32 MB of RAM.

Input Format

Lines 1... : The wholefile(can be any number of lines) should be read inas a string.

What is a Transposition Cipher?

A transposition cipher is a method of encryption where the positions of the characters are shifted according to a certain system.

Why Use a Transposition Cipher?

Simple yet effective for learning basic encryption techniques. Easy to implement and understand.

Objective:

Implement a simple transposition cipher to encrypt text files.

Requirements:

Encrypt text files up to 16 MB.

Use a given template to read the file and encrypt the content.

Resource Limits:

Runtime: 3 seconds

RAM: 32 MB

Input Format:

The whole file is read in as a single string.

Output Format:

First line: Total number of characters in the string. Following lines: Enciphered string.

Sample Input:

Test early and often!

Sample Output:

getstr Function:

Purpose: Read the file into a dynamically allocated string.

Key Points:

Uses fread to read the file in blocks.

Dynamically reallocates memory to accommodate the entire file.

while Loop:

Read the file block by block.

Append each block to the string and reallocate memory if necessary.

Output Explanation

matrix LANG: C

#include <stdio.h>

#include <stdlib.h>

#include <string.h>

typedef struct Matrix_s {

size_t R, C; int *index; } Matrix;

Matrix* allocate_matrix( size_t R, size_t C ) {

Matrix *matrix = malloc( sizeof( Matrix ) );

matrix->R = R;

matrix->C = C;

matrix->index = malloc( R * C * sizeof( int ) ); return matrix; }

void destroy_matrix( Matrix *matrix ) { free( matrix->index ); free( matrix ); }

typedef enum { REGULAR = 0,

TRANSPOSE = 1

}Transpose;

// Allowing reading a matrix in as either regular or transposed

Matrix* read_matrix( FILE *input,Transpose orient ) {

size_t R, C;

fscanf( input, “%zu %zu”, &R, &C );

Matrix *matrix = NULL;

if( orient == REGULAR ) {

matrix = allocate_matrix( R, C );

for( size_t r = 0; r < matrix->R; ++r ) {

for( size_t c = 0; c < matrix->C; ++c ) {

fscanf( input, “%d”, &matrix->index[c + r * C] );

if( orient ==TRANSPOSE ) {

matrix = allocate_matrix( C, R );

for( size_t r = 0; r < matrix->C; ++r ) {

for( size_t c = 0; c < matrix->R; ++c ) {

fscanf( input, “%d”, &matrix->index[r + c * R] );

fprintf( stderr, “Error: unknown orientation %d.\n”, orient );

exit( EXIT_FAILURE );

return matrix;

void print_matrix( FILE *output, Matrix *matrix ) { fprintf( output, “%zu %zu\n”, matrix->R, matrix->C );

for( size_t r = 0; r < matrix->R; ++r ) {

for( size_t c = 0; c < matrix->C - 1; ++c ) {

fprintf( output, “%d “, matrix->index[c + r * matrix->C] );

fprintf( output, “%d\n”, matrix->index[matrix->C - 1 + r * matrix->C] );

Matrix* product_matrix( Matrix *a, Matrix *b ) {

if( a->C != b->C ) {

printf( “Error: tried to multiply (%zux%zu)x(%zux%zu)\n”, a->R, a->C, b->C, b->R );

exit( EXIT_FAILURE );

Matrix *prod = allocate_matrix( a->R, b->R );

size_t nRows = prod->R, nCols = prod->C, nInner = a->C;

for( size_t r = 0; r < nRows; ++r ) {

for( size_t c = 0; c < nCols; ++c ) {

prod->index[c + r * nCols] = 0; for( size_t i = 0; i < nInner; ++i ) {

prod->index[c + r * nCols] += a->index[i + r * nInner] * b->index[i + c * nInner];

return prod; } int main(void) { FILE *fin = fopen( “matrix2.in”, “r” );

if( fin == NULL ) {

printf( “Error: could not open matrix2.in\n” );

exit( EXIT_FAILURE ); }

Matrix *a = read_matrix( fin, REGULAR );

Matrix *b = read_matrix( fin,TRANSPOSE );

fclose( fin );

Matrix *c = product_matrix( a, b );

FILE *output = fopen( “matrix2.out”, “w” );

if( output == NULL ) {

printf( “Error: could not open matrix2.out\n” );

print_matrix( output, c ); fclose( output ); destroy_matrix( a ); destroy_matrix( b ); destroy_matrix( c ); return 0;

Key Takeaways:

•Understanding the transposition cipher.

•Implementing file reading and dynamic memory allocation in C.

•Writing recursive functions for encryption.

Next Steps:

Experiment with different encryption techniques. Optimize the code for larger files.

Turn static files into dynamic content formats.

Create a flipbook
Issuu converts static files into: digital portfolios, online yearbooks, online catalogs, digital photo albums and more. Sign up and create your flipbook.