Java implements the Virginia encryption/decryption algorithm

Source: Internet
Author: User
Tags key string

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 ());
}
}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.