There are two ways to add decryption to Virginia.
The first is to check the table: the first act plaintext, the first column key, the remaining for the corresponding ciphertext
The second method is the conversion method: convert characters to zero-based numbers, encrypt/decrypt numbers, and convert to characters.
This article is to use C + + to implement the second method, and for easy operation, the use of the MFC framework (attached to the project download)
The core code is as follows:
//cipher.h Additional files for placement of related algorithms, which are independent of MFC and can be ported directly to projects that support CStringint*cstringtoint (CString str) {//convert CString to zero_based integer intSize=Str. GetLength (); int*asc_space=New int[Size];//use new in order to return without being eliminated for(intI=0; i<size;i++) {Asc_space[i]=int(str. GetAt (i)); if(asc_space[i]> -&&asc_space[i]< the)//A- Z conversionasc_space[i]-= $; Else if(asc_space[i]> the&&asc_space[i]<123)//A- Z conversionasc_space[i]-= the; } returnAsc_space;}voidEncode (CString key,cstring plain,cstring &cipher) { //Virginia Encryption Algorithm int*keycode=Cstringtoint (key); int*plaincode=Cstringtoint (plain); int*ciphercode=Cstringtoint (plain); intKeysize=key. GetLength (); intPlainsize=Plain. GetLength (); intflag=0; CString Blank (' ', plainsize+1);//Reserve a byte to put inCipher=blank;//Create a blank cipher for(intI=0; i<plainsize;i++) { if(flag>=keysize)//Rotate Keyflag=0; Ciphercode[i]= (Plaincode[i]+keycode[flag])% -;//the digital code of the cipherCipher. SetAt (I,'A'+ciphercode[i]);//Generate ciphertext++Flag; } cipher. SetAt (i,' /');//At the endAdd toDelete Keycode,plaincode,ciphercode;//free space for Cstringtoint () application}voidDecode (CString key,cstring &plain,cstring cipher) { //encryption is slightly modified to decrypt the algorithm int*keycode=Cstringtoint (key); int*plaincode=cstringtoint (cipher); int*ciphercode=cstringtoint (cipher); intKeysize=key. GetLength (); intCiphersize=cipher. GetLength (); intflag=0; CString Blank (' ', ciphersize+1); Plain=Blank; for(intI=0; i<ciphersize;i++) { if(flag>=keysize) Flag=0; //+26 is to correct the case of ciphertext digital code less than the key digital codePlaincode[i]= (ciphercode[i]-keycode[flag]+ -)% -; Plain. SetAt (i,'a'+Plaincode[i]); ++Flag; } plain. SetAt (i,' /'); Delete Keycode,plaincode,ciphercode;}
//MFC Onbutton () response
voidCvigenerecipherdlg::onencrypt ()//Encrypt button{updatedata (); if(M_key. IsEmpty () | |M_plain. IsEmpty ()) MessageBox ("the key and clear text cannot be empty! "); Else{Encode (m_key,m_plain,m_cipher); Call the cryptographic function UpdateData (false); M_ctredit1.setreadonly (TRUE);//prevent The key from being modifiedM_ctredit2.setreadonly (TRUE);//prevent modification of plaintextM_ctredit3.setreadonly (TRUE);//Prevent redaction changesm_ctredit4.setreadonly (FALSE); } }voidCvigenerecipherdlg::ondecrypt ()//Decrypt Button{updatedata (); if(M_key. IsEmpty () | |M_cipher. IsEmpty ()) MessageBox ("Keys and ciphertext cannot be empty! "); Else{Decode (m_key,m_explainted,m_cipher); UpdateData (false); M_explainted=""; M_ctredit1.setreadonly (FALSE); M_ctredit2.setreadonly (FALSE); M_ctredit3.setreadonly (FALSE); M_ctredit4.setreadonly (TRUE); }}
Test results such as:
Project DOWNLOAD Link:
Http://files.cnblogs.com/zoffy/VigenereCipher.zip
Vigenère Cipher Virginia plus decryption algorithm