Convert this code from C++ to JAVA #include <stdio.h> #include<iostream.h> #incl
ID: 3862803 • Letter: C
Question
Convert this code from C++ to JAVA
#include <stdio.h>
#include<iostream.h>
#include <conio.h>
#include<string.h>
#include<fstream.h>
#include<stdlib.h>
main()
{
clrscr();
ifstream keyfile,ptfile;
ofstream rfile;
int add,count=0,count1=0;
char ch,pt[100],key[10],PTmat[10][10],entext[100],detext[100],Cmat[10][10];
//====================== OPEN FILES =============
keyfile.open("key.txt");
ptfile.open("plain.txt");
rfile.open("encrypt.txt");
strcpy(pt,"");
strcpy(entext,"");
//======== READING THE KEY FROM THE FILE =======
while(!keyfile.eof())
{
ch=keyfile.get();
key[count]=ch;
count++;
}
count--;
//======== READING THE PLAIN TEXT FROM THE FILE ======
while(!ptfile.eof())
{
ch=ptfile.get();
pt[count1]=ch;
count1++;
}
count1--;
add=count1/count+1;
int space=add*count-count1;
for(int i=count1;i<space+count1;i++)
pt[i]='%';
pt[strlen(pt)+space]='';
//========== GENERETE THE MATRIX =====
int d=0;
for(i=0;i<add;i++)
for(int j=0;j<count;j++)
PTmat[i][j]=pt[d++];
//========== decryption ===============
d=0;
for(int k=0;k<count;k++)
{
if (key[k] >= '0' && key[k] <= '9')
j = key[k]-'0';
for(i=0;i<add;i++)
{
rfile<<PTmat[i][j-1];
entext[d++]=PTmat[i][j-1];
}
}
entext[d--]='';
//============= DECRYPTION =================
d=0;
for(k=0;k<count;k++)
{
if (key[k] >= '0' && key[k] <= '9')
j = key[k]-'0';
for(i=0;i<add;i++)
Cmat[i][j-1]=entext[d++];
}
k=0;
for(i=0;i<add;i++)
for(j=0;j<count;j++)
detext[k++]=Cmat[i][j];
detext[count1-1]='';
//================== PRINT THE RESULTS =====
cout<<"************** <<<<<<<<< The Encryption >>>>>>>>> **************";
cout<<" The Plaintext :";
for(i=0;i<count1;i++)
cout<<pt[i];
cout<<"The Key : (";
for(k=0;k<count;k++)
{
if (key[k] >= '0' && key[k] <= '9')
j = key[k]-'0';
cout<<j;
if(k!=count-1)
cout<<",";
}
cout<<")";
cout<<" The Plaintext Matrix : ";
cout<<" ";
for(i=0;i<count;i++)
cout<<i+1<<" ";
cout<<" <==== The Key ";
for(i=0;i<add;i++)
{
cout<<" ";
for(j=0;j<count;j++)
cout<<PTmat[i][j]<<" ";
cout<<endl;
}
cout<<"The Encrypted Text : ";
cout<<entext;
cout<<" ************** <<<<<<<< The Decryption >>>>>>>>> ****************";
cout<<" The Ciphertext : ";
cout<<entext;
cout<<" The Ciphertext After Divided into Colums: ";
d=0;
for(i=0;i<count1+space;i++)
{
if(d==add)
{
cout<<" ";
d=0;
}
cout<<entext[i];
d++;
}
cout<<" The Ciphertext Matrix With The Same Key : ";
for(k=0;k<count;k++)
cout<<k+1<<" ";
cout<<endl;
for(i=0;i<add;i++)
{
cout<<" ";
for(j=0;j<count;j++)
cout<<Cmat[i][j]<<" ";
cout<<endl;
}
cout<<"The Decrypted Text :";
cout<<detext;
getch();
return 0;
}
The results should be like this (please attach samples run of the program)
e************* KKKKKKKK The Encryption The Plaintext security!security!security! The Plaintext Matrix 1 2 3 4 5 The Key i t y t y s e c u r i t The Encrypted Text rs i etzu !rsi sietcyetcyu!cyu r KKKKKKK The Decryption The Ciphertext rsiet/u!rs i%sietcyetcyu !cyu!rz The Ciphertext After Divided into Colums rs iet% u!rsi. sietcy etcyu! cyu!ry. The Ciphertext Matrix With The Same Key 1 2 3 4 5 S e C r i t y s e C r i. t y s e c u r i t The Decrypted Text :security 'security 'securityExplanation / Answer
package Chegg2;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.Writer;
import java.util.Scanner;
public class Security {
public static void main(String[] args) {
Scanner keyfile = null;
Scanner ptfile = null;
//Writer rfile encrypt.txt;
try{
keyfile = new Scanner(new File("key.txt"));
ptfile = new Scanner(new File("plain.txt"));
}catch(FileNotFoundException e){
e.printStackTrace();
}
int add;int count=0; int count1=0;
char ch;
char pt[] = new char[100];
char key[] = new char[10];
char PTmat[][] = new char[10][10] ;
char entext[] = new char[100];
char detext[] = new char[100];
char Cmat[][] = new char[10][10];
//======== READING THE KEY FROM THE FILE =======
while(keyfile.hasNext()){
ch = (char)keyfile.nextByte();
key[count]=ch;
count++;
}
count--;
//======== READING THE PLAIN TEXT FROM THE FILE ======
while(!ptfile.hasNext())
{
ch=(char)ptfile.nextByte();
pt[count1]=ch;
count1++;
}
count1--;
add=count1/count+1;
int space=add*count-count1;
for(int i=count1;i<space+count1;i++)
pt[i]='%';
pt[pt.length+space]='';
//========== GENERETE THE MATRIX =====
int d=0;
for(int i=0;i<add;i++)
for(int j=0;j<count;j++)
PTmat[i][j]=pt[d++];
//========== decryption ===============
d=0;
int j =0;
for(int k=0;k<count;k++)
{
if (key[k] >= '0' && key[k] <= '9')
j = key[k]-'0';
for(int i=0;i<add;i++)
{
//rfile<<PTmat[i][j-1]; write in file
entext[d++]=PTmat[i][j-1];
}
}
entext[d--]='';
//============= DECRYPTION =================
d=0;
for(int k=0;k<count;k++)
{
if (key[k] >= '0' && key[k] <= '9')
j = key[k]-'0';
for(int i=0;i<add;i++)
Cmat[i][j-1]=entext[d++];
}
int k=0;
for(int i=0;i<add;i++)
for(j=0;j<count;j++)
detext[k++]=Cmat[i][j];
detext[count1-1]='';
//================== PRINT THE RESULTS =====
System.out.println("************** <<<<<<<<< The Encryption >>>>>>>>> **************");
System.out.println(" The Plaintext :");
for(int i=0;i<count1;i++)
System.out.print(pt[i]);
System.out.print("The Key : (");
{
if (key[k] >= '0' && key[k] <= '9')
j = key[k]-'0';
System.out.print(j);
if(k!=count-1)
System.out.print(",");
}
System.out.print(")");
System.out.println(" The Plaintext Matrix : ");
System.out.print(" ");
for(int i=0;i<count;i++)
System.out.print(i+1+" ");
System.out.print(" <==== The Key ");
for(int i=0;i<add;i++)
{
System.out.print(" ");
for(j=0;j<count;j++)
System.out.print(PTmat[i][j]+" ");
System.out.print(" ");
}
System.out.print("The Encrypted Text : ");
System.out.print(entext);
System.out.print(" ************** <<<<<<<< The Decryption >>>>>>>>> ****************");
System.out.print(" The Ciphertext : ");
System.out.print(entext);
System.out.print(" The Ciphertext After Divided into Colums: ");
d=0;
for(int i=0;i<count1+space;i++)
{
if(d==add)
{
System.out.print(" ");
d=0;
}
System.out.print(entext[i]);
d++;
}
System.out.print(" The Ciphertext Matrix With The Same Key : ");
for(k=0;k<count;k++)
System.out.print(k+1+" ");
System.out.print(" ");
for(int i=0;i<add;i++)
{
System.out.print(" ");
for(j=0;j<count;j++)
System.out.print(Cmat[i][j]+" ");
System.out.print(" ");
}
System.out.print("The Decrypted Text :");
System.out.print(detext);
}
}