Page 3 of 4 FirstFirst 1234 LastLast
Results 21 to 30 of 32

Thread: Beginner's Programming Challenge 18

  1. #21
    Join Date
    Apr 2008
    Location
    Melbourne, Australia
    Beans
    328
    Distro
    Ubuntu 10.04 Lucid Lynx

    Re: Beginner's Programming Challenge 18

    Lucky i remembered this was in my bookmarks!

    well I needed a refresh in C, so here's mine
    (if i can be bothered i will make it compile with -ansi -Wall -pedantic)

    PHP Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>

    // prototypes
    int encypt(char *inputchar *key);
    int decrypt(char *EncStringchar *key);
    int getString(char *stringunsigned lengthchar *prompt);
    void usage();
    // end

    #define FAILURE 0
    #define SUCCESS 1

    #define INPUT_LENGTH 20
    #define PROMPT_LENGTH 30

    #define ENCRYPT 100
    #define DECRYPT 200

    int main(int argcchar **argv) {
        
        
    char input[INPUT_LENGTH];
        
    char key[INPUT_LENGTH];
        
    char prompt[PROMPT_LENGTH];
        
    int option 0;
        
        if(
    argc == 2) {
            if(
    strcmp("encrypt",argv[1]) == 0option ENCRYPT;
            else if(
    strcmp("decrypt",argv[1]) == 0option DECRYPT;
            else { 
    usage(); return EXIT_FAILURE; }
        }else{ 
            
    usage();
            return 
    EXIT_FAILURE;
        }
        
        
    sprintf(prompt,"Enter text :"PROMPT_LENGTH);
        
    getString(inputINPUT_LENGTHprompt);

        
    sprintf(prompt,"Enter Key :"PROMPT_LENGTH);
        
    getString(keyINPUT_LENGTHprompt);
        
    printf("\n");
         
       switch(
    option){
            case 
    ENCRYPTencrypt(input,key); break;
            case 
    DECRYPTdecrypt(input,key); break;
        }
        return 
    EXIT_SUCCESS;
    }

    int encrypt(char *inputchar *key) {
        
        
    int i 0keyOffset 0;
        
    char p '\0';

        for(; 
    strlen(input); i++){
            if(
    isalpha(input[i]) && isalpha(key[keyOffset])) {
                
    //p = (c + k) % 26
                
    = ((input[i] - 'A') + (key[keyOffset] - 'A')) % 26;
                
    += 'A';
            }else 
    input[i];
            
            
    // print char as it comes out
            
    printf("%c"p);
            if((
    keyOffset ++) > strlen(key)) keyOffset 0;
        }
        
    printf("\n");
        return 
    SUCCESS;    
    }
    int decrypt(char *encStringchar *key){
        
        
    int i 0keyOffset 0;
        
    char c '\0';
        
        for(; 
    strlen(encString); i++){
            if(
    isalpha(encString[i]) && isalpha(key[keyOffset])) {
                
    //c = (p - k) % 26
                
    = ((encString[i] - 'A') - (key[keyOffset] - 'A')) % 26;
                if(
    0+= 26;
                
    += 'A';
            }else 
    encString[i];

            
    printf("%c"c);
            if((
    keyOffset ++) > strlen(key)) keyOffset 0;
        }
        
    printf("\n");
        return 
    SUCCESS;
    }

    int getString (char *stringunsigned lengthchar *prompt){

        
    char temp[INPUT_LENGTH 2];    
        
    printf("%s"prompt);
        
    fgets(temp, (length 2), stdin); // add 2 for '\n' and '\0'
        // can do ALL types of input validation here.
        // for this example, didnt worry about...

        
    temp[strlen(temp) -1] = '\0'// override \n with \0
        
    strcpy(stringtemp); // make result available to calling function

        
    return SUCCESS;
    }
    void usage() {
        
    printf("Usage, ./a.out <encrypt | decrypt>\n");

    Code:
    $gcc main.c
    Its probably way bigger than it has to be, but nevertheless
    Last edited by ja660k; February 23rd, 2011 at 03:59 PM. Reason: left out compilation rules; allthough this is self evident :S
    "Oh Ubuntu, you are my favorite linux based operating system" Dr. Sheldon Cooper.

  2. #22
    Join Date
    Feb 2011
    Location
    Carnaval's land.
    Beans
    Hidden!
    Distro
    Ubuntu 19.04 Disco Dingo

    Re: Beginner's Programming Challenge 18

    Wow, not so beginner programming, eh?

  3. #23
    Join Date
    Jul 2007
    Location
    Austin, TX
    Beans
    Hidden!
    Distro
    Ubuntu 10.04 Lucid Lynx

    Re: Beginner's Programming Challenge 18

    I have also made a small UI in Swing

    Niceeee

  4. #24
    Join Date
    Dec 2007
    Location
    Behind you!!
    Beans
    978
    Distro
    Ubuntu 10.04 Lucid Lynx

    Re: Beginner's Programming Challenge 18

    At the request of the OP, the beginners team will be judging this challenge.
    Good luck to all who entered.

    Bodsda
    computer-howto
    Linux is not windows
    Fluxbox & Flux menu how to
    Programming is an art. Learn it, Live it, Love it!


  5. #25
    Join Date
    Feb 2007
    Location
    St. Louis, MO
    Beans
    4,930
    Distro
    Ubuntu 20.04 Focal Fossa

    Re: Beginner's Programming Challenge 18

    I chose Java. My version encrypts and decrypts.

    PHP Code:
    /*
     Program to encrypt OR decrypt a word with a Vigenere cipher 
     */

    import java.io.*;

    // main Class
    public class VigenereCipher
    {
        public static 
    void mainString[] args )
        {
            
    // create variable of type VigenereCipher
            
    VigenereCipher m = new VigenereCipher();

            
    // transition to non-static method
            
    m.getUserInput();
        }
        
        private 
    void getUserInput()
        {
            
    BufferedReader in = new BufferedReader( new InputStreamReaderSystem.in ) );

            
    String choice = new String();
            
    String word = new String();
            
    String password = new String();
            
    String newPassword = new String();
            
    String cipherText = new String();
            
            
    System.out.println"Vigenere Cipher Program\nPlease ONLY use capital letters A-Z\n\n" );
            
            
    // decide if the user wants to encrypt or decrypt a word
            
    System.out.print( "Encrypt or Decrypt (E/D)?: " );
            try
            {
               
    choice in.readLine(); 
            }
            catch( 
    IOException e ){}
            
            
    // encrypt choice
            
    if( choice.charAt) == 'e' || choice.charAt) == 'E' )
            {
                
    // get input from user
                
    System.out.print( "Enter word to be encrypted: " );
                try
                {
                    
    word in.readLine(); 
                }
                catch( 
    IOException e ){}

                
    // get cipher key from user
                
    System.out.print( "Enter password: " );
                try
                {
                    
    password in.readLine();
                }
                catch( 
    IOException e ){}
                
                
    // call the createNewPassword method
                
    newPassword createNewPasswordwordpassword );
            
                
    // call the method to create the encrypted text
                
    cipherText encryptCipherTextwordnewPassword );
            
                
    System.out.println"Encrypted text: " cipherText );
            }
            
            
    // decrypt choice
            
    if( choice.charAt) == 'd' || choice.charAt) == 'D' )
            {
                
    // get input from user
                
    System.out.print( "Enter word to be decrypted: " );
                try
                {
                    
    word in.readLine(); 
                }
                catch( 
    IOException e ){}

                
    // get cipher key from user
                
    System.out.print( "Enter password: " );
                try
                {
                    
    password in.readLine();
                }
                catch( 
    IOException e ){}
                
                
    // call the createNewPassword method
                
    newPassword createNewPasswordwordpassword );
            
                
    // call the method to decrypt the text
                
    cipherText decryptCipherTextwordnewPassword );
            
                
    System.out.println"Decrypted text: " cipherText );
            }
        }

        
    // method to create the encoded text from the word and password
        
    private String createNewPasswordString wordString password )
        {
            
    // create new password that will be of the same length as the word to be encrypted and repeat
            
    String newPassword = new String();
            
            
    // needed ints
            
    int i 0;
            
    int numPasswordLength 0;
            
            
    // first must repeat the password to be as long as the word to be encrypted
            // ex:
            // word:         ATTACKATDAWN
            // password:     LEMON
            // new password: LEMONLEMONLE

            // the numPasswordLength will be the number of times the password can divide into the word + the remainder
            
    numPasswordLength = ( word.length() / password.length() ) * password.length() + ( word.length() % password.length() );

            
    // create the new password
            
    if( (int)( word.length() / password.length() ) >= )
            {
                
    // concatenate the number of time password fits into word
                
    for( 0< (int)( word.length() / password.length() ); i++ )
                {
                    
    newPassword += password;
                }

                
    // concatenate the remainder using modulo arithmetic
                
    for( 0< (int)( word.length() % password.length() ); i++ )
                {
                    
    newPassword += password.charAt);
                }

            }
            else 
    // the password is longer than the word
            
    {
                for( 
    0word.length(); i++ )
                {
                    
    newPassword += password.charAt);
                }
            }
            
            
    // return the new password to the calling method
            
    return newPassword;
        }
        
        private 
    String encryptCipherTextString wordString password )
        {
            
    // String to contain the cipher
            
    String cipherText = new String();
            
            
    // integers
            
    int i 0;        
            
    int result 0;

            
    // create the cipher text using the Vigenere algorithm
            
    for( 0word.length(); i++ )
            {
                
    // use modulo arithmetic to create the cipher, acsii for A is 65, subtract 65 to make zero based
                
    result = ( ( ( word.codePointAt) - 65 password.codePointAt) - 65 ) % 26 ) );
                
                
    // add cipher characters to String, add 65 to go away from 0 reference
                
    cipherText += Character.toString( (char)( result 65 ) );
            }
            
            
    // return the cipher back to the calling method
            
    return cipherText;
        }

        private 
    String decryptCipherTextString wordString password )
        {
            
    // String to contain the encrypted text
            
    String decipherText = new String();
            
            
    // integers
            
    int i 0;        
            
    int result 0;

            
    // create the cipher text using the Vigenere algorithm
            
    for( 0word.length(); i++ )
            {
                
    // use modulo arithmetic to decipher, acsii for A is 65, subtract 65 to make zero based
                
    result = ( ( ( ( word.codePointAt) - 65 ) - ( password.codePointAt) - 65 ) ) % 26 ) );

                
    // check to see if result is negative, if yes add 26
                
    if( result )
                {
                    
    // add cipher characters to String, add 65 to go away from zero reference
                    // add an additional 26 to alleviate negative number
                    
    decipherText += Character.toString( (char)( result 65 26 ) );
                }
                else
                {
                    
    // add cipher characters to String, add 65 to go away from zero reference
                    
    decipherText += Character.toString( (char)( result 65 ) );
                }
            }
            
            
    // return the cipher back to the calling method
            
    return decipherText;
        }

    Code for compiling
    Code:
    javac -cp . VigenereCipher.java
    Last edited by stchman; March 8th, 2011 at 08:20 AM.
    Windows, only good for gaming.

  6. #26
    Join Date
    May 2007
    Location
    Munich Germany
    Beans
    100
    Distro
    Ubuntu 15.10 Wily Werewolf

    Re: Beginner's Programming Challenge 18

    Does anybody ever judge this contest?
    Intel i5 6500 | MSI z170a Gaming M5 | Corsair Vengence LPX 16Gb DDR4 @ 2400 | MSI GTX 970 gaming 4G | Ubuntu 15.10 Wily Werewolf / Windows 7

    http://ubuntutechnical.wordpress.com/

  7. #27
    Join Date
    Sep 2009
    Location
    Canada, Montreal QC
    Beans
    1,809
    Distro
    Ubuntu 11.10 Oneiric Ocelot

    Re: Beginner's Programming Challenge 18

    Don't bother with this thread, no one is even visiting it!
    I know not with what weapons World War III will be fought, but World War IV will be fought with sticks and stones.
    Freedom is measured in Stallmans.
    Projects: gEcrit

  8. #28
    Join Date
    Feb 2007
    Location
    St. Louis, MO
    Beans
    4,930
    Distro
    Ubuntu 20.04 Focal Fossa

    Re: Beginner's Programming Challenge 18

    Has a winner even been determined?
    Windows, only good for gaming.

  9. #29
    Join Date
    Apr 2008
    Beans
    286

    Re: Beginner's Programming Challenge 18

    I'm supposed to be judging this, but I forgot about it. I'll try go through the entries ASAP.

  10. #30
    Join Date
    May 2007
    Location
    Munich Germany
    Beans
    100
    Distro
    Ubuntu 15.10 Wily Werewolf

    Re: Beginner's Programming Challenge 18

    finally

    Hope you can judge fast
    Intel i5 6500 | MSI z170a Gaming M5 | Corsair Vengence LPX 16Gb DDR4 @ 2400 | MSI GTX 970 gaming 4G | Ubuntu 15.10 Wily Werewolf / Windows 7

    http://ubuntutechnical.wordpress.com/

Page 3 of 4 FirstFirst 1234 LastLast

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •