Decrypting Passwords in C++


/ Published in: C++
Save to your folder(s)

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.


Copy this code and paste it in your HTML
  1. #include <iostream>
  2. #include <string.h>
  3. #include <stdlib.h>
  4. #include <sstream>
  5.  
  6. using namespace std;
  7.  
  8. void main(){
  9. //Variable declarations
  10. int cont=0;
  11. char password[100]={0};
  12. int *int_password;
  13. char real_password[50]={0};
  14. char *decrypted_password;
  15. char encrypted_password[100]={0};
  16. int aux2[2]={0};
  17. int aux3[3]={0};
  18. int pos=0;
  19. int len;
  20.  
  21.  
  22. cout<<"Please enter encrypted password: ";
  23. gets_s(password);//got 'password' char array
  24. len=strlen(password);
  25. int_password=new int[len];//initializing pointer to int_password
  26. for(int i=0;i<len;i++){
  27. int_password[i]=password[i]-48;//calculating char to int according to ascii table
  28. }
  29. cout<<endl;
  30. for(int i=0;i<len;i++){
  31. if(password[i]>=48 && password[i]<=57){
  32. cont++;
  33. }
  34. /*if all of the characters are numbers, that means encryption
  35. level was simple mode. if any of them is not, then the advanced mode.*/
  36. }
  37.  
  38. cout<<endl;
  39. if(cont==len){ //is simple mode
  40. cout<<">>Simple-mode encryption type detected."<<endl;
  41. string stringpassword(password);//converting password into a string-type array
  42. cout<<"Detected string: "<<stringpassword<<endl;
  43. cout<<"Length of the string: "<<len<<endl;
  44. int i=0;
  45. cout<<endl;
  46. while(pos<len){
  47. if(int_password[pos]==1){ //encrypted ascii number has 3 digits
  48. int k=0;
  49. aux3[0]=int_password[pos];
  50. cout<<"Digit "<<k+1<<" of aux3: "<<aux3[0]<<endl;
  51. aux3[1]=int_password[pos+1];
  52. cout<<"Digit "<<k+2<<" of aux3: "<<aux3[1]<<endl;
  53. aux3[2]=int_password[pos+2];
  54. cout<<"Digit "<<k+3<<" of aux3: "<<aux3[2]<<endl;
  55. std::stringstream ss;
  56. for(int i=0;i<3;i++) ss<<aux3[i];
  57. int value;
  58. ss>>value;
  59. cout<<"Found digit number "<<i+1<<": "<<value<<endl;
  60. value=value-12;//i now calculate the real ascii number that will correspond to a character
  61. char value2=value;//conversion from INT to CHAR
  62. real_password[i]=value2;//assign the int value 'value' to the i position of the password char array
  63. i++;
  64. pos=pos+3;
  65. }
  66. else{ //encrypted ascii number has 2 digits
  67. //same process if the encrypted ascii number has 2 digits
  68. int k=0;
  69. aux2[0]=int_password[pos];
  70. aux2[1]=int_password[pos+1];
  71. cout<<"Digit "<<k+1<<" of aux2: "<<aux2[0]<<endl;
  72. cout<<"Digit "<<k+2<<" of aux2: "<<aux2[1]<<endl;
  73. std::stringstream ss;
  74. for(int i=0;i<2;i++) ss<<aux2[i];
  75. int value;
  76. ss>>value;
  77. cout<<"Found digit number "<<i+1<<": "<<value<<endl;
  78. value=value-12;
  79. char value2 = value;
  80. real_password[i]=value2;
  81. i++;
  82. pos=pos+2;
  83. k++;
  84. }
  85. }
  86. cout<<endl;
  87. cout<<"Now translating..."<<endl;
  88. cout<<"\t>>Decrypted password: ";
  89.  
  90. for(int c=0;c<i;c++){
  91. cout<<real_password[c];}
  92.  
  93. cout<<endl;//end of the for loop
  94. }//end of first condition
  95. else {
  96. //is advanced mode
  97. cout<<">>Advanced-mode encryption type detected."<<endl;
  98. string stringpassword(password);
  99. cout<<"Detected string: "<<stringpassword<<endl;
  100. cout<<"Length of the string: "<<len<<endl;
  101.  
  102. decrypted_password=new char[len/2];/*on the encryption algorithm, odd positions were filled with trash characters*/
  103. for(int i=0;i<len/2;i++){
  104. decrypted_password[i]=stringpassword[2*i+1]-7;//De-encryption algorithm
  105. }
  106. cout<<"Now translating..."<<endl;
  107. cout<<"\t>>Decrypted password: "; //Print the obtained decrypted password
  108. for(int c=0;c<len/2;c++){
  109. cout<<decrypted_password[c];
  110. }cout<<endl;
  111. }
  112. cout<<"Developed by Santi Pagola."<<endl;
  113. system("pause");
  114. }

URL: http://programmingeiger824.blogspot.com

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.