Java Simple encryption

Source: Internet
Author: User
Tags decrypt


* The basic operation of all encryption techniques is to use the Encrypt () and decrypt () methods for message
* Decomposition and composition, where the message is a space-delimited string, except encrypt () and decrypt ()
* Outside each encryption class also requires the encode () and Decode () methods to follow a specific algorithm for each word
* rules are encoded,. such as Caesar Cipher and transpose Clipher
* As you can imagine because encryption has the same method, first define a generic class cipher class
* Encapsulation of common methods.

 Packagecase.encryption;ImportJava.util.*; Public Abstract classCipher {/*** The basic operation of all cryptographic techniques is to decompose and combine messages with the Encrypt () and decrypt () methods, where the message is a space-delimited string, except for encrypt () and decrypt () * Each encryption class also requires the encode () and Decode () methods to encode each word according to a specific algorithm * rule. For example Caesar Cipher and transpose Clipher * According to the assumption that since encryption has the same method, first define a generic class Cipher class * encapsulating the common method. *  * */   Publicstring Encrypt (string s) {StringBuffer result=NewStringBuffer (); StringTokenizer words=NewStringTokenizer (s);  while(Words.hasmoretokens ()) {Result.append (Encode (Words.nexttoken () )+""); }      returnresult.tostring (); }   Publicstring Decrypt (string s) {StringBuffer result=NewStringBuffer (); StringTokenizer words=NewStringTokenizer (s);  while(Words.hasmoretokens ()) {Result.append (decode (Words.nexttoken () )+""); }      returnresult.tostring (); }   Public Abstractstring Encode (string word);  Public Abstractstring decode (string woid); }
 Packagecase.encryption; Public classCaesarextendscipher{@Override Publicstring Encode (string word) {//TODO auto-generated Method StubStringBuffer result =NewStringBuffer ();  for(intK=0;k<word.length (); k++)        {            CharCh=Word.charat (k); CH=(Char) (' A ' + (ch-' a ' +3)%26);//? Regular Expressionsresult.append (CH); }        returnresult.tostring (); } @Override Publicstring decode (string word) {//TODO auto-generated Method StubStringBuffer result =NewStringBuffer ();  for(intK=0;k<word.length (); k++)        {            CharCh=Word.charat (k); CH=(Char) (' A ' + (ch-' a ' +23)%26);//? Regular Expressionsresult.append (CH); }        returnresult.tostring (); }}
 Packagecase.encryption; /** This example uses the simplest reversal translocation method, and can also use other translocation methods */  Public classTransposeextendscipher{@Override Publicstring Encode (string word) {//TODO auto-generated Method StubStringBuffer result =NewStringBuffer (); returnresult.reverse (). toString (); } @Override Publicstring decode (string word) {//TODO auto-generated Method Stub        returnencode (word); }    }

1  Packagecase.encryption;2 3  Public classTestencrypt {4      Public Static voidMain (string[] args) {5Caesar Caesar =NewCaesar ();6String plain = "This is the secret message.";7String secret=Caesar.encrypt (plain);8String decrypt=Caesar.decrypt (secret);9System.out.println ("*********caesar Cipher encryption**********");TenSystem.out.println ("PlainText:" +Plain); OneSystem.out.println ("Encrypted:" +secret); ASystem.out.println ("Decrypted:" +decrypt); -          -Transpose ts=Newtranspose (); theString tsecret=Ts.encrypt (plain); -String tdecrypt=Ts.decrypt (secret); -System.out.println ("**********transpose Cipher encryption**********"); -System.out.println ("PlainText:" +Plain); +System.out.println ("Encrypted:" +Tsecret); -System.out.println ("Decrypted:" +tdecrypt); +     } A  at}

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.