C++ Beginner Program Snippet: The Password Manager class should have just one me
ID: 3552112 • Letter: C
Question
C++ Beginner Program Snippet:
The Password Manager class should have just one member variable, which will store the encrypted password (a string). Do not store the password unencrypted. The Password Manager class should have the following two internal member functions (not accessible outside of the class): encrypt: this takes a password (a string) and returns the encrypted form of the password. Note: there is no decrypt function (there is no need to decrypt passwords). We will use the following VERY simple encryption algorithm: he XOR operator (A) in C++ takes two char arguments and returns a char. For every character in the input string, apply the XOR operator A with the character 2'. For example: str[i] 2 ': Store all the resulting chars in a string to be returned as the result of the function (do not set the member variable, do not change the argument that is passed in). verify Password: this takes a string (a password) and returns true if it meets the following criteria: it is at least 8 characters long it contains at least one letter it contains at least one digit it contains at least one of these four characters: , ?,! Otherwise it returns false. The Password Manager should have the following member functions that are accessible outside of the class:Explanation / Answer
#include <iostream>
#include <string>
using namespace std;
class PasswordManager{
public: // you can change this to private if you want the functions to private
string encrypt(string p){
string myresult=p;// just to make sure the length is same;
for(int i=0; i<p.size();i++){
myresult[i]= p[i]^'2';
}
return myresult;
}
// you can add private: here if you want the function to private
bool verifyPassword(string p){
if (p.size()>=8) return true; // checks if it is atleast 8 chars long
for(unsigned int i=0; i<p.size(); i++){
if (('a'<=(int)p[i]&&(int)p[i]<='z')||('A'<=(int)p[i]&&(int)p[i]<='Z')||('0'<=(int)p[i]&&(int)p[i]<='9') ) return true;// checks if there is a digit or a number in the string
if (p[i]=='<'||p[i]=='>'||p[i]=='?'||p[i]=='!') return true;
return false; // if it reaches upto here none of the condition got satisfied
}
}
};
int main(){
PasswordManager myManager;
cout<<myManager.verifyPassword("=-=543")<<endl;
string g= myManager.encrypt("dfjkdfj");
cout<<g;
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.