EncryptionAlgorithmProgram:
Public class MTOC
{
// Enter the plaintext and key, and use the entered key to encrypt the plaintext.
Public static void main (string [] ARGs)
{Int I;
Char [] C = new char [100];
Char [] k1 = new char [100];
// Input
System. Out. Print ("enter a mingwen string :");
String M = myinput. readstring ();
System. Out. Print ("enter a key string :");
String K = myinput. readstring ();
// Construct a key table
For (I = 0; I <K. Length (); I ++)
{
If (K. charat (I)> = 'A' & K. charat (I) <= 'Z ')
K1 [I] = (char) (K. charat (I)-97 );
If (K. charat (I)> = 'A' & K. charat (I) <= 'Z ')
K1 [I] = (char) (K. charat (I)-65 );
}
// Encryption
For (I = 0; I <M. Length (); I ++)
{
If (M. charat (I)> = 'A' & M. charat (I) <= 'Z ')
C [I] = (char) (M. charat (I)-97 + K1 [I % K. Length ()]) % 26 + 97 );
If (M. charat (I)> = 'A' & M. charat (I) <= 'Z ')
C [I] = (char) (M. charat (I)-65 + K1 [I % K. Length ()]) % 26 + 65 );
}
// Output ciphertext
For (I = 0; I <C. length; I ++)
System. Out. Print (C [I]);}
}
Decryption algorithm program:
Public class ctom
{
// Enter the password and key, and use the key to decrypt the ciphertext
Public static void main (string [] ARGs)
{Int I;
Char [] M = new char [100];
Char [] k1 = new char [100];
// Input
System. Out. Print ("Enter the miwen string :");
String c = myinput. readstring ();
System. Out. Print ("Enter the key string :");
String K = myinput. readstring ();
// Construct a key table
For (I = 0; I <K. Length (); I ++)
{
If (K. charat (I)> = 'A' & K. charat (I) <= 'Z ')
K1 [I] = (char) (K. charat (I)-97 );
If (K. charat (I)> = 'A' & K. charat (I) <= 'Z ')
K1 [I] = (char) (K. charat (I)-65 );
}
// Decrypt
For (I = 0; I <C. Length (); I ++)
{
If (C. charat (I)> = 'A' & C. charat (I) <= 'Z ')
M [I] = (char) (C. charat (I)-97-k1 [I % K. Length ()] + 26) % 26 + 97 );
If (C. charat (I)> = 'A' & C. charat (I) <= 'Z ')
M [I] = (char) (C. charat (I)-65-k1 [I % K. Length ()] + 26) % 26 + 65 );
}
// Output plaintext
For (I = 0; I <M. length; I ++)
System. Out. Print (M [I]);}
}
Myinput. Java class:
// Myinput. Java: contain the methods for reading int, double, and
// String values from the keyboard
Import java. Io .*;
Public class myinput
{
// Read a string from the keyboard
Public static string readstring ()
{
Bufferedreader br
= New bufferedreader (New inputstreamreader (system. In), 1 );
// Declare and initialize the string
String string = "";
// Get the string from the keyboard
Try
{
String = Br. Readline ();
}
Catch (ioexception ex)
{
System. Out. println (Ex );
}
// Return the string obtained from the keyboard
Return string;
}
// Read an int value from the keyboard
Public static int readint ()
{
Return integer. parseint (readstring ());
}
// Read a double value from the keyboard
Public static double readdouble ()
{
Return double. parsedouble (readstring ());
}
// Read a byte value from the keyboard
Public static byte readbyte ()
{
Return byte. parsebyte (readstring ());
}
// Read a short value from the keyboard
Public static short readshort ()
{
Return short. parseshort (readstring ());
}
// Read a long value from the keyboard
Public static long readlong ()
{
Return long. parselong (readstring ());
}
// Read a float value from the keyboard
Public static float readfloat ()
{
Return float. parsefloat (readstring ());
}
}
Encryption Algorithm Program:
Public class MTOC
{
// Enter the plaintext and key, and use the entered key to encrypt the plaintext.
Public static void main (string [] ARGs)
{Int I;
Char [] C = new char [100];
Char [] k1 = new char [100];
// Input
System. Out. Print ("enter a mingwen string :");
String M = myinput. readstring ();
System. Out. Print ("enter a key string :");
String K = myinput. readstring ();
// Construct a key table
For (I = 0; I <K. Length (); I ++)
{
If (K. charat (I)> = 'A' & K. charat (I) <= 'Z ')
K1 [I] = (char) (K. charat (I)-97 );
If (K. charat (I)> = 'A' & K. charat (I) <= 'Z ')
K1 [I] = (char) (K. charat (I)-65 );
}
// Encryption
For (I = 0; I <M. Length (); I ++)
{
If (M. charat (I)> = 'A' & M. charat (I) <= 'Z ')
C [I] = (char) (M. charat (I)-97 + K1 [I % K. Length ()]) % 26 + 97 );
If (M. charat (I)> = 'A' & M. charat (I) <= 'Z ')
C [I] = (char) (M. charat (I)-65 + K1 [I % K. Length ()]) % 26 + 65 );
}
// Output ciphertext
For (I = 0; I <C. length; I ++)
System. Out. Print (C [I]);}
}
Decryption algorithm program:
Public class ctom
{
// Enter the password and key, and use the key to decrypt the ciphertext
Public static void main (string [] ARGs)
{Int I;
Char [] M = new char [100];
Char [] k1 = new char [100];
// Input
System. Out. Print ("Enter the miwen string :");
String c = myinput. readstring ();
System. Out. Print ("Enter the key string :");
String K = myinput. readstring ();
// Construct a key table
For (I = 0; I <K. Length (); I ++)
{
If (K. charat (I)> = 'A' & K. charat (I) <= 'Z ')
K1 [I] = (char) (K. charat (I)-97 );
If (K. charat (I)> = 'A' & K. charat (I) <= 'Z ')
K1 [I] = (char) (K. charat (I)-65 );
}
// Decrypt
For (I = 0; I <C. Length (); I ++)
{
If (C. charat (I)> = 'A' & C. charat (I) <= 'Z ')
M [I] = (char) (C. charat (I)-97-k1 [I % K. Length ()] + 26) % 26 + 97 );
If (C. charat (I)> = 'A' & C. charat (I) <= 'Z ')
M [I] = (char) (C. charat (I)-65-k1 [I % K. Length ()] + 26) % 26 + 65 );
}
// Output plaintext
For (I = 0; I <M. length; I ++)
System. Out. Print (M [I]);}
}
Myinput. Java class:
// Myinput. Java: contain the methods for reading int, double, and
// String values from the keyboard
Import java. Io .*;
Public class myinput
{
// Read a string from the keyboard
Public static string readstring ()
{
Bufferedreader br
= New bufferedreader (New inputstreamreader (system. In), 1 );
// Declare and initialize the string
String string = "";
// Get the string from the keyboard
Try
{
String = Br. Readline ();
}
Catch (ioexception ex)
{
System. Out. println (Ex );
}
// Return the string obtained from the keyboard
Return string;
}
// Read an int value from the keyboard
Public static int readint ()
{
Return integer. parseint (readstring ());
}
// Read a double value from the keyboard
Public static double readdouble ()
{
Return double. parsedouble (readstring ());
}
// Read a byte value from the keyboard
Public static byte readbyte ()
{
Return byte. parsebyte (readstring ());
}
// Read a short value from the keyboard
Public static short readshort ()
{
Return short. parseshort (readstring ());
}
// Read a long value from the keyboard
Public static long readlong ()
{
Return long. parselong (readstring ());
}
// Read a float value from the keyboard
Public static float readfloat ()
{
Return float. parsefloat (readstring ());
}
}