* 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}