/ Published in: C++
This is the second and last part of the encryption algorithm that I uploaded last week. It's the other-way-round program: it decrypts a password typed by user.
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
#include <iostream> #include <string.h> #include <stdlib.h> #include <sstream> using namespace std; void main(){ //Variable declarations int cont=0; char password[100]={0}; int *int_password; char real_password[50]={0}; char *decrypted_password; char encrypted_password[100]={0}; int aux2[2]={0}; int aux3[3]={0}; int pos=0; int len; cout<<"Please enter encrypted password: "; gets_s(password);//got 'password' char array len=strlen(password); int_password=new int[len];//initializing pointer to int_password for(int i=0;i<len;i++){ int_password[i]=password[i]-48;//calculating char to int according to ascii table } cout<<endl; for(int i=0;i<len;i++){ if(password[i]>=48 && password[i]<=57){ cont++; } /*if all of the characters are numbers, that means encryption level was simple mode. if any of them is not, then the advanced mode.*/ } cout<<endl; if(cont==len){ //is simple mode cout<<">>Simple-mode encryption type detected."<<endl; string stringpassword(password);//converting password into a string-type array cout<<"Detected string: "<<stringpassword<<endl; cout<<"Length of the string: "<<len<<endl; int i=0; cout<<endl; while(pos<len){ if(int_password[pos]==1){ //encrypted ascii number has 3 digits int k=0; aux3[0]=int_password[pos]; cout<<"Digit "<<k+1<<" of aux3: "<<aux3[0]<<endl; aux3[1]=int_password[pos+1]; cout<<"Digit "<<k+2<<" of aux3: "<<aux3[1]<<endl; aux3[2]=int_password[pos+2]; cout<<"Digit "<<k+3<<" of aux3: "<<aux3[2]<<endl; std::stringstream ss; for(int i=0;i<3;i++) ss<<aux3[i]; int value; ss>>value; cout<<"Found digit number "<<i+1<<": "<<value<<endl; value=value-12;//i now calculate the real ascii number that will correspond to a character char value2=value;//conversion from INT to CHAR real_password[i]=value2;//assign the int value 'value' to the i position of the password char array i++; pos=pos+3; } else{ //encrypted ascii number has 2 digits //same process if the encrypted ascii number has 2 digits int k=0; aux2[0]=int_password[pos]; aux2[1]=int_password[pos+1]; cout<<"Digit "<<k+1<<" of aux2: "<<aux2[0]<<endl; cout<<"Digit "<<k+2<<" of aux2: "<<aux2[1]<<endl; std::stringstream ss; for(int i=0;i<2;i++) ss<<aux2[i]; int value; ss>>value; cout<<"Found digit number "<<i+1<<": "<<value<<endl; value=value-12; char value2 = value; real_password[i]=value2; i++; pos=pos+2; k++; } } cout<<endl; cout<<"Now translating..."<<endl; cout<<"\t>>Decrypted password: "; for(int c=0;c<i;c++){ cout<<real_password[c];} cout<<endl;//end of the for loop }//end of first condition else { //is advanced mode cout<<">>Advanced-mode encryption type detected."<<endl; string stringpassword(password); cout<<"Detected string: "<<stringpassword<<endl; cout<<"Length of the string: "<<len<<endl; decrypted_password=new char[len/2];/*on the encryption algorithm, odd positions were filled with trash characters*/ for(int i=0;i<len/2;i++){ decrypted_password[i]=stringpassword[2*i+1]-7;//De-encryption algorithm } cout<<"Now translating..."<<endl; cout<<"\t>>Decrypted password: "; //Print the obtained decrypted password for(int c=0;c<len/2;c++){ cout<<decrypted_password[c]; }cout<<endl; } cout<<"Developed by Santi Pagola."<<endl; system("pause"); }
URL: http://programmingeiger824.blogspot.com