Playfair Password Java implementation

Source: Internet
Author: User

This time, Java is used to simulate the process of encrypting and decrypting real Playfair passwords.

Import Java.util.hashset;import Java.util.Scanner; Public classTest1 { Public Static CharA[][] =New Char[5][5]; Public StaticString Encryptkey =""; Public Static void Main(string[] args) {//TODO auto-generated method stubScanner input =NewScanner (System.inch); System. out. println ("Please input the key");        String key = Input.nextline (); System. out. println ("Please input the clear key");        String Clearkey = Input.nextline (); System. out. println ("Please input the Signture key");        String signture = Input.nextline (); System. out. println ("Output the key Matrix");        Keymatrix (key);        Outputmatrix (); System. out. println ("Encrypt");        Encrypt (Clearkey, Signture.touppercase ());    Dncrypt (Encryptkey); }Private Static void Dncrypt(String Encryptkey2) {//TODO auto-generated method stubString Dncryptkey ="";intm=0, n=0, o=0, p=0; for(intI=0; I<encryptkey2.length (); i+=2) { for(intj=0; j<5; J + +) { for(intk=0; k<5; k++) {if(A[j][k] = = Encryptkey2.charat (i))                        {m=j;                    N=k; }if(A[j][k] = = Encryptkey2.charat (i+1)) {o=j;                    P=k; }                }            }if(m==o) {Dncryptkey =dncryptkey + a[m][(n+4)%5] + a[o][(p+4)%5]; }Else if(n==p) {Dncryptkey =dncryptkey + a[(m+4)%5][n] + a[(o+4)%5][P]; }Else{Dncryptkey =dncryptkey + a[o][n] + a[m][p]; }} System. out. println (Dncryptkey); System. out. println (); }Private Static void Encrypt(String Clearkey, String signture) {//TODO auto-generated method stubString Upperclearkey = Clearkey.touppercase (); String Formatkey =""; for(intI=0; I<upperclearkey.length (); i++) {if(Upperclearkey.charat (i) >=' A '&& Upperclearkey.charat (i) <' Z ') {Formatkey+=upperclearkey.charat (i); }        } while(Isoverflow (Formatkey))        {Formatkey = Addsignture (Formatkey, signture); }if(Formatkey.length ()%2!=0) {Formatkey + = Signture;    } getencrypt (Formatkey); }Private Static void Getencrypt(String Formatkey) {//TODO auto-generated method stub        intm=0, n=0, o=0, p=0; for(intI=0; I<formatkey.length (); i+=2) { for(intj=0; j<5; J + +) { for(intk=0; k<5; k++) {if(A[j][k] = = Formatkey.charat (i))                        {m=j;                    N=k; }if(A[j][k] = = Formatkey.charat (i+1)) {o=j;                    P=k; }                }            }if(m==o) {Encryptkey =encryptkey + a[m][(n+1)%5] + a[o][(p+1)%5]; }Else if(n==p) {Encryptkey =encryptkey + a[(m+1)%5][n] + a[(o+1)%5][P]; }Else{Encryptkey =encryptkey + a[o][n] + a[m][p]; }} System. out. println (Encryptkey); System. out. println (); }Private StaticBooleanIsoverflow(String Formatkey) {//TODO auto-generated method stub         for(intI=0; I<formatkey.length ()-formatkey.length ()%2; i+=2) {if(Formatkey.charat (i) = = Formatkey.charat (i+1)) {return true; }        }return false; }Private StaticStringaddsignture(String Formatkey, String signture) {//TODO auto-generated method stubString temp =""; for(intI=0; I<formatkey.length ()-formatkey.length ()%2; i+=2) {if(Formatkey.charat (i) = = Formatkey.charat (i+1) {temp = temp + Formatkey.charat (i) + Signture.charat (0) + Formatkey.charat (i+1); }Else{temp = temp + Formatkey.charat (i) + Formatkey.charat (i+1); }        }returnTemp }Private Static void Outputmatrix() { for(intI=0; i<5; i++) { for(intj=0; j<5; J + +) {System. out. Print (a[i][j]+"  "); } System. out. println (); } System. out. println (); }Private Static void Keymatrix(String key) {//TODO auto-generated method stubhashset<character> HS =NewHashset<character> (); String Formatkey =""; String Upperkey = Key.touppercase (); for(intI=0; I<upperkey.length (); i++) {if(Upperkey.charat (i) >=' A '&& Upperkey.charat (i) <' Z ') {if(!hs.contains (Upperkey.charat (i)))                    {Hs.add (Upperkey.charat (i));                Formatkey+=upperkey.charat (i); }            }        } for(CharI=' A '; i<' Z '; i++) {if(!hs.contains (i))            {formatkey+=i; }        }intm =0; for(intI=0; i<5; i++) for(intj=0; j<5;                J + +) {A[i][j] = Formatkey.charat (m);            m + +; }    }}

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Playfair Password Java implementation

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.