Java Ma simple implementation and encryption verification code application, java Ma

Source: Internet
Author: User

Java Ma simple implementation and encryption verification code application, java Ma

I recently watched a movie "Imitation games". In the movie "Imitation games", Alan Turing deciphered Ma and made me curious about Ma. So I began to read the materials to study it, but after all, wisdom is limited, so I use Java to implement a simple Ma.

If anyone is interested in Ma, can you read this blog https://www.0xaa55.com/forum.php? Mod = viewthread & tid = 1208 & extra = page % 3D1 & page = 1.

Ma encrypts each character into an ascii code into another character:

The simple Ma implementation here is just entertainment. The main implementation principle is to enter Scanner cin = new Education (System. in); then read msg = cin. nextLine (); then, each character is cyclically traversed to add 26 to it. To increase the complexity, determine if (msg. charAt (I)-count) <'A ')Encodemsg + = (char) (msg. charAt (I) + 26-count );Else
Encodemsg + = (char) (msg. charAt (I)-count); this achieves a simple Ma, which is actually the ascii code of the variable characters.

// Implementation

Static void encode ()
{
String msg = "";
String encodemsg = "";
Int count = 0;
System. out. printf ("% s", "Input the message :");
Using cin = new Login (System. in); // enter in the console
Msg = cin. nextLine (); // obtain the information entered in the console
For (int I = 0; I <msg. length (); I ++) // cyclically traverse every character of information
{
System. out. println ("msg =" + msg. charAt (I ));
If (Character. isSpace (msg. charAt (I) // determines whether the Character is blank
{
Encodemsg + = "";

}
Else if (msg. charAt (I)-count) <'A') // determines if the ascii code of msg. charAt (I)-count is less than a, + 26

Encodemsg + = (char) (msg. charAt (I) + 26-count );

Else
Encodemsg + = (char) (msg. charAt (I)-count );

Count ++;



}
System. out. println (encodemsg );
}

Note: I have a small idea to encrypt the verification code with this simple Ma, which is conducive to the security protection of the verification code.

The verification code I implemented here is to randomly generate several digits or letters through the random () class, also combined with the ascii code, not to mention the Code directly:

// Generate random numbers and letters,
Public static String getStringRandom (int length ){

String val = "";
Random random = new Random ();

// Parameter length, which indicates the number of random numbers generated
For (int I = 0; I <length; I ++ ){
// Determine whether the output is a number or a letter
String charOrNum = random. nextInt (2) % 2 = 0? "Char": "num ";
// Output letters or numbers
If ("char". equalsIgnoreCase (charOrNum )){
// Whether the output is uppercase or lowercase letters
System. out. println ("n 2 =" + random. nextInt (2 ));
Int temp = random. nextInt (2) % 2 = 0? 65: 97;
Val + = (char) (random. nextInt (26) + temp );
} Else if ("num". equalsIgnoreCase (charOrNum )){
Val + = String. valueOf (random. nextInt (10 ));
}
}
Return val;
}

Then, use the Ma method for encryption. You only need to replace the console input in the previous MA method with the random number generated by getStringRandom (4.

Note: At the beginning of my blog writing, I am not very familiar with this typographical style and feel ugly.

 

 

 

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.