Jose Vidal shared this file. Want to do more with it?
importjava.util.Scanner;
/**
*RandomCypher.java
*@authorJoseMVidal<jmvidal@gmail.com>
*Created on Feb16,2012
*
*/
publicclassRandomCypher{
/**
*@param args
*/
publicstaticvoidmain(String[] args){
char c ='a';
finalintLEN=26;//26 letters in English
char[] alphabet =newchar[LEN];//just for printing
char[] cypher =newchar[LEN];
//first, put the alphabet in
for(int i =0; i < cypher.length; i++){
cypher[i]=(char)('a'+ i);
alphabet[i]=(char)('a'+ i);
}
//now shuffle
for(int i =0; i <100; i++){
int a =(int)(Math.random()*LEN);
int b =(int)(Math.random()*LEN);
char temp = cypher[a];
cypher[a]= cypher[b];
cypher[b]= temp;
}
System.out.println("Our cypher is:");
System.out.println(alphabet);
System.out.println(cypher);
Scanner keyboard =newScanner(System.in);
String message ="";
do{
System.out.println("Enter your message:");
message = keyboard.nextLine();
char[] msg = message.toLowerCase().toCharArray();
System.out.println("The encrypted message is:");
for(int i =0; i < msg.length; i++){
char letter = msg[i];
if(letter <'a'|| letter >'z')
System.out.print(letter);
else
System.out.print(cypher[(int)(letter -'a')]);
}
System.out.println("");
}while(! message.equals(""));
}
}
We use cookies to provide, improve, protect and promote our services. Visit our Privacy Policy and Privacy Policy FAQs to learn more. You can manage your personal preferences, including your ‘Do not sell or share my personal data to third parties’ setting using the “Customize cookies” button below.