public classcryptosystem { public static void main(String []args) {charoutput[]=
ID: 3612469 • Letter: P
Question
public classcryptosystem
{
public static void main(String []args)
{charoutput[]={'A','B','C','D','E','F','G','H','I','J','K','L','M',
'N','O','P','Q','R','S','T','U','V','W','X','Y','Z'};
char [] encr=new char[26];
char []outword=new char [20];
String key,inword;
char c;
boolean good=true,wrap;
int i,j,len,start,stop,n,encrptr,beg,end,k;
Random number = new Random();
Scanner in=new Scanner(System.in);
do
{
System.out.print("Enter a key ");
good=true;
key=in.next();
len=key.length();
if(len<5||len>26)
{good=false;
len=0;
}
for(i=0;i<len-1;i++)
for(j=i+1;j<len;j++)
if(key.charAt(i)==key.charAt(j))
good =false;
if(!good)
System.out.println("invalid key ");
}while(!good);
n=number.nextInt(25)+1;
//System.out.print("Enter n"); //for testing
//n=in.nextInt(); //for testing
start=n-1;
stop=(start+len-1)%26;
//System.out.println("stop "+stop+" start"+start); //for testing
if(stop<start)
wrap=true;
else
wrap=false;
encrptr=0;
if(!wrap)
for(i=0;i<26;i++)
{//System.out.println("i="+i);
if(encrptr<start||encrptr>stop)
{ if(!inkey(output[i],key,len))
{ encr[encrptr++]=output[i];
// System.out.println("1 "+encr[encrptr-1]+" "+encrptr);
}
}
else
{for(j=0;j<len;j++)
{encr[encrptr++]=key.charAt(j);
// System.out.println("2 "+encr[encrptr-1]+""+encrptr);
}
i--;
}
}
else
for(i=0;i<26+26-start;i++)
{//System.out.println("i="+i+"Encrypt stop start "+encrptr+" "+stop+""+start);
if(encrptr>stop&&encrptr<start)
{ if(!inkey(output[i],key,len))
{ encr[encrptr++]=output[i];
// System.out.println("3 "+encr[encrptr-1]+""+encrptr);
}
}
else
{ //System.out.println("here "+i);
if(i!=0)
{ beg=0;
end=26-n+1;
Explanation / Answer
please rate - thanks these are the functions you need public static boolean inkey(char a, String key,int len) {int k; for(k=0;kRelated Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.