vendredi 13 mars 2015

How would you change this from #C To Python?


Ok so my son has a project, and I am trying to help him but I do not know python I wrote the program in C roughly but i do not know how to change it over to python to help him. I would like to know if anyone would know how to get it into python so i can help step him threw but without giving him the answer outright.




his paper has these instructions


The purpose of this short project is to decrypt a file that has been encrypted. The encryption method used is a polyalpabetic substitution method aslo known as the Vigenere Cipher. To find the answer to this problem you must read; then understand , and then reverse the procedure that was used to encrypt the file.




Encryption Procedure:




  1. An 26 X 26 encryption matrix of capital letters was created.




  2. When a file was read character by character a modulus of the location of the character in the file was calculated using the number 26. This provided a row index for the encryption matrix. The position of the character in the alphabet provided the column index. The character in the encryption matrix at the row and column was used to replace the input character. In reading the input file no substitutions were made for spaces or for punctuation. The case of the letters was preserved.





    Instructions:



  3. Write code to read the content of the file “encrypt.txt” into a two dimensional [26][26] array.




  4. Write code to read and decrypt the content of the file "word.txt". Write your answer to a file called "final_translation.txt".




    int main() { FILE* infile; FILE* outfile;



    int a = 0;
    int b = 0;
    int cntr = 0;
    int upper=0;
    char newchar;
    char nchar;
    char ematrix[26][26];// Define matrix array

    infile = fopen("encrypt.txt", "r" );// Open file to read in matrix array

    for( a = 0 ; a < 26 ; a++ )
    {
    b = 0;
    while( b < 26 )
    {
    nchar = fgetc(infile);// Read characters into array
    if( nchar != '\n' )
    {
    ematrix[a][b] = nchar;
    b++;
    }

    } // end of while ( < 26 )

    } // end of for( a < 26 )

    for( a = 0 ; a < 26 ; a++ )// Print out ematrix array
    {
    for(b = 0 ; b < 26 ; b++ )
    printf("%2c ", ematrix[a][b] );
    printf("\n");

    }

    printf("\n\n\n");


    infile = fopen("words.txt", "r" );// Open file to decrypt
    outfile = fopen("final_translation.txt", "w+");// Open file to ouput final decryption



    while( !feof(infile))// Read in characters to decrypt
    {
    nchar=fgetc(infile);

    a = cntr % 26;// Determines what column to read from

    if((isalpha(nchar)) && (islower(nchar)))// Check if alpha or for lower case characters
    {
    nchar = toupper(nchar);// Change lowercase to uppercase
    upper++;
    }

    b=0;

    while(ematrix[a][b] != nchar)// Determine what row to read from
    {
    b++;
    }

    newchar=('A'+ b);

    if(isspace(nchar))// Change back to a space
    newchar = ' ';

    else if(ispunct(nchar))// Change to currect punctuation
    newchar = nchar;

    printf("%c", newchar);// Print out decrypted text
    cntr++;
    }

    printf("\n\n");

    fclose(infile);// Close file
    fclose(outfile);// Close file

    return(0);


    }






Aucun commentaire:

Enregistrer un commentaire