3

さて、私は大学のクラス用のプログラムを作成しています。これは、構造を使用して、ユーザー名とパスワードとともにユーザー情報のデータベースの構築をシミュレートする単純なプログラムです。ラボの追加クレジットとして、パスワードを暗号化できます。これは特別なことではありません....MD5 やそのような高度なものは使用していません。

私がする必要があるのは、大文字を小文字に、小文字を大文字に、そして最後に、10進数の整数を16進数に変換するのに問題がある部分を切り替えることができることです。

プログラム全体ではなく、関連する部分だけを投稿するようにします。

構造は次のとおりです。

struct Info
{
    string sFname;
    string sLname;
    string sUname;
    string sPword;
    string sAddress;
    string sEmail;
    string sPhone;
};

注: これは構造体の動的配列です。

Info *Users;
        Users = new Info[size];

そして、これまでに持っているパスワードを暗号化するコードは次のとおりです。

//string length for controlling loops
strlen = Users[iUsrCount].sPword.length();

        //temp string to hold the encrypted version of password
        string temp;

        //switch uppercase characters to lowercase and vice versa, and convert
        //decimal integers into hexadecimal
        for(int i=0; i<strlen; i++)
        {
                cout << "\n\nInside encryption for loop iteration " << i << "\n\n";

                if(islower(Users[iUsrCount].sPword[i]))
                {
                        temp += toupper(Users[iUsrCount].sPword[i]);
                        continue;
                }
                else if(isupper(Users[iUsrCount].sPword[i]))
                {
                        temp += tolower(Users[iUsrCount].sPword[i]);
                        continue;
                }
                else if(isdigit(Users[iUsrCount].sPword[i]))
                {
                        char charNum = Users[iUsrCount].sPword[i];
                        int iDec = charNum - '0';



                        //get integer
                        while((i+1) < strlen && isdigit(Users[iUsrCount].sPword[i+1]))
                        {
                                i++;
                                iDec = iDec * 10 + Users[iUsrCount].sPword[i] - '0';
                                cout << " " << iDec << " ";
                        }

                        char hexTemp[10];

                        //convert
                        sprintf(hexTemp, "%x", iDec);

                        temp += hexTemp;

                        //debugging cout to make sure hexes are properly calculated
                        cout << " " << hexTemp << " ";
                        continue;
                }
        }
                //debugging cout to make sure that password is properly encrypted
                cout << endl << endl << temp << endl << endl;

                strlen = temp.length();

                //overwrite the plain text password with the encrypted version
                for(int i=0; i<strlen; i++)
                        Users[iUsrCount].sPword[i] = temp[i];

                //debugging cout to make sure copy was successful
                cout << endl << endl << Users[iUsrCount].sPword;

したがって、パスワードが次の場合:

456Pass45word87

暗号化すると、次のようになります。

1c8pASS2dWORD57

文字列を逆にする必要もありますが、それはかなり簡単です。

私の2つの質問はこれです:

  1. これを行う簡単な方法はありますか?

  2. 私の人生では、パスワードを復号化するための正しいアルゴリズムを理解することはできません。これは、ユーザーがログインしたときに、コンピューターが入力内容をプレーンテキストバージョンのパスワードと比較できるようにする必要があります。

注: 私はプロのコーダーではありません。私はいたるところを見てきましたが、私の特定の状況で本当に役立つものは何も見つかりません.

そして、私はインターネットのなすがままに身を投じます。:D

4

3 に答える 3

1
  1. これを行う簡単な方法はありますか?

-- あなたの方法は私にはうまくいきます。あなたの道は明確で簡単です。

  1. 私の人生では、パスワードを復号化するための正しいアルゴリズムを理解することはできません。これは、ユーザーがログインしたときに、コンピューターが入力した内容をプレーンテキストバージョンのパスワードと比較できるようにする必要があります。

-- パスワードを解読しようとしないでください。ユーザーがログインするときに、暗号化されたパスワードを使用して比較を行います。

于 2013-11-04T05:26:04.810 に答える
0

ブーストと C++11 を使用してこれを行う簡単な方法がありますが、バニラ C++98 を気にしない場合は、STL と単項関数を使用してこれを行うことができます

#include <iostream>
#include <algorithm>
#include <string>
#include <cstdlib>
#include <sstream>
#include <cctype>
#include <vector>
#include <iterator>
using namespace std;

//modified from here:
//https://stackoverflow.com/a/313990/866930
char flipcase (char in) {
    if (in<='Z' && in>='A')
        return in-('Z'-'z');
    else if (in<='z' && in>='a') {
        return in+('Z'-'z');
    }
    return in;
}

int main() {
    string test = "456Pass45word87";
    transform(test.begin(), test.end(), test.begin(), flipcase);//this flips uppercase to lowercase and vice-versa

    string nums, text;
    vector<string> parts;

    //break the string into its constituent number and textual parts and operate
    //on them accordingly.
    //for text, store all until a non-text character is reached. Reset.
    //for numbers, keeping iterating until the first non-digit character
    //is reached. Store, then reset.
    for (int i = 0; i < test.length(); i++) {
        if (isdigit(test[i])) {
            nums += test[i];
            if (!text.empty()) {
                parts.push_back(text);//store the text
            }
            text.clear();//reset the memory in it
        }
        else {
            text += test[i];

            if (!nums.empty()) {                
                int n = atoi(nums.c_str());
                stringstream ss;
                //now reinsert back into the string stream and convert it to a hexadecimal:
                ss << std::hex << n;

                parts.push_back(ss.str());//now store it
                nums.clear();//clear the number string
            }
        }
    }

    //at this point the vector contains the string broken into different parts
    //that have been correctly modified.
    //now join all the strings in the vector into one:
    //adapted from here:
    //https://stackoverflow.com/a/5689061/866930
    ostringstream oss;
    copy(parts.begin(), parts.end(), ostream_iterator<string>(oss,""));//we want no character delimiter between our strings
    cout << oss.str();

    return 0;
}

これは以下を出力します:

1c8pASS2dWORD57

望んだ通りに。

ノート:

文字列を反復処理するときに反復子を使用できますが、それは自分でできることです。

参考文献:

https://stackoverflow.com/a/313990/866930

https://stackoverflow.com/a/5689061/866930

http://www.cplusplus.com/reference/algorithm/transform/

于 2013-11-04T06:13:42.553 に答える
0

暗号化のために、パラメータとして次の設定があります。

//switch uppercase characters to lowercase and vice versa, and convert
//decimal integers into hexadecimal

したがって、復号化するには、サイクルを逆にするだけです。

  • 16 進数を 10 進数に変換する
  • 大文字と小文字を反対に切り替える

復号化されたバージョンが得られます。

詳細が必要な場合はお知らせください。

于 2013-11-04T10:25:21.910 に答える