Yahoo Answers is shutting down on May 4th, 2021 (Eastern Time) and beginning April 20th, 2021 (Eastern Time) the Yahoo Answers website will be in read-only mode. There will be no changes to other Yahoo properties or services, or your Yahoo account. You can find more information about the Yahoo Answers shutdown and how to download your data on this help page.

help on caesar cipher program?

can you create a java program to write a program that performs a shift encryption and decryption based on a given shift. Your program should start by prompting the user to specify a shift (this is analogous to a password). Then the user should be prompted to enter a string of text to either encrypt or decrypt. Lastly, your program should print the encrypted/decrypted text.

2 Answers

Relevance
  • 5 years ago

    To encrypt a character:

    char plain = [get plain input from someplace]

    plain = Character.toLowerCase(plain);

    char cipher = (plain - 'a' + shift)%26 + 'a';

    [store or output the char in cipher somewhere]

    Decryption uses the same calculation, but adds 26-shift instead of shift:

    plain = (cipher - 'a' + 26 - shift)%26 + 'a';

  • roger
    Lv 7
    5 years ago

    here it is in C

    you can translate to Java

    #include <stdio.h>

    #include <ctype.h>

    void crypt(char *,char *,int);

    int main(void){

    // caesar cypher programme

    char i[]="testingxxx";

    char o[100],o2[100];

    int j=0;

    int off=3;

    crypt(i,o,off);

    printf("%s\n",o);

    crypt(o,o2,-off);

    printf("%s\n",o2);

    return 0;

    }

    void crypt(char * i ,char * o ,int off){

    while(*i ){

    *o++ ='A'+( (toupper(*i++)-'A'+26+off)%26);

    }

    *o=0;

    return;

    }

Still have questions? Get your answers by asking now.