回 帖 发 新 帖 刷新版面

主题:一个小的加解密程序,供学习。

/**************************************************  ****/
/* This program will encrypt and decrypt messages. The*/
/* user will simply have to remeber a secret key.     */
/**************************************************  ****/
#include <iostream>
#include <fstream>
#include <string>
using namespace std;

// Function Prototypes ---------------------------------
int GetUserChoice ();

void Encryption ();
void Decryption ();

string XorString ( string source, int key );
// -----------------------------------------------------

// Main Function ---------------------------------------
int main ()
{
    
    // Variable to hold our choice
    int choice;
    
    // Create an infinite loop -------------------------
    while ( 1 ) {
        
        // GetUserChoice will return what the user wants to do
        // We can assign the returned value to our variable
        choice = GetUserChoice ();
        
        // Now we can check what the user wants
        if ( choice == 1 ) Encryption ();
        else if ( choice == 2 ) Decryption ();
        
        // If the choice was 3 that means we can break from our loop
        else if ( choice == 3 ) break;
        
    }
    // -------------------------------------------------
        
    return 0;
    
}

// GetUserChoice Function ------------------------------
int GetUserChoice ()
{
    
    // Variable for the choice
    int choice;
    
    // Here we will print our menu and prompt for a number
    // This number will represent what the user wants to do
    // Once we have the number this function will return it
    cout << "Message Encryption" << endl;
    cout << "----------------------" << endl;
    cout << "1) Encrypt Message" << endl;
    cout << "2) Decrypt Message" << endl;
    cout << "3) Exit" << endl << endl;
    cout << "Make a selection: ";
    cin  >> choice;
    
    // Now we will check to make sure the choice was valid
    if ( choice != 1 && choice != 2 && choice != 3 ) {
        
        cout << endl;
        cout << "Please enter 1, 2, or 3" << endl;
        cout << "Press enter to exit...";
        
        // Since the user entered an invalid selection we
        // will just exit the program by returning our programs
        // exit code.. 3
        
        cin.get ();
        return 3;
        
    }
    
    // If it makes it to this point that means the input was valid so we can return it
    return choice;
    
}
// -----------------------------------------------------

// Here is where we will encrypt the string ------------
string XorString ( string source, int key )
{
    
    // Here we loop through each character, using binary xor on it.
    for ( unsigned int loop = 0; loop < source.size (); loop ++ ) {
        
        source [loop] ^= key;
        
    }
    
    return source;
    
}

// Here we will get the file name, key, and message from the user ----
void Encryption ()
{
    
    // Variables for the file name, key and message ----
    char file_name [255];
    
    string message;
    string encrypted_message;
    
    int key;
    // -------------------------------------------------
    
    cout << endl;
    cout << "Enter file name: ";
    cin  >> file_name;
    
    cout << "Enter secret key: ";
    cin  >> key;
    
    cout << "Enter your secret message." << endl;
    cout << "Message: ";
    
    // We need this to ignore anything that might be left over
    // and ruin our message
    cin.ignore ( 255, '\n' );
    
    getline ( cin, message );
    
    // Now we have all of the info we can encrypt and write --
    // the string to the file --------------------------------
    encrypted_message = XorString ( message, key );
    
    ofstream file ( file_name );
    
    file << encrypted_message;
    
    file.close ();
    // -------------------------------------------------------
    
    cout << "Done..." << endl << endl;
    
}
// -------------------------------------------------------------------

// Decryption will work the same way. Instead of asking the user for the message, ---
// we print it out. -----------------------------------------------------------------
void Decryption ()
{
    
    // Variables for the file name, key and message ----
    char file_name [255];
    
    string message;
    
    int key;
    // -------------------------------------------------
    
    cout << endl;
    cout << "Enter file name: ";
    cin  >> file_name;
    
    cout << "Enter secret key: ";
    cin  >> key;
    
    // Now we will check to see if the file exists -----
    ifstream file ( file_name );
    
    if ( !file.is_open () ) {
        
        cout << "Failed to load " << file_name << "!" << endl;
        cout << "Press enter to exit...";
        
        cin.get ();
        
        exit ( EXIT_FAILURE );
        
    }
    // -----------------------------------------------
    
    getline ( file, message );
    
    file.close ();
    
    cout << "Message: " << XorString ( message, key ) << endl;
    
    cout << "Done..." << endl << endl;
    
}

回复列表 (共21个回复)

沙发

问下 怎样实现的 加密

板凳

解密也来一个啊

3 楼

加密也来一个啊
就一个也太孤单了

4 楼

后半部分应该是解密的。

5 楼

我在我的电脑上进行上面程序调试时,出现
h:\microsoft visual studio\vc98\include\iostream(16) : fatal error C1001: INTERNAL COMPILER ERROR
compiler file 'msc1.cpp', line 1786)
         Please choose the Technical Support command on the Visual C++
         Help menu, or open the Technical Support help file for more information
Error executing cl.exe.
请问是为什么.可以帮我解答吗?多谢!

加密与解密程序.exe - 1 error(s), 0 warning(s)

6 楼

[quote]fatal error C1001: INTERNAL COMPILER ERROR[/quote]
提示:内部编译器出现问题,你再检查一下你的编程环境,设在windows consol下面,才能编译运行纯DOS程序。

7 楼

不能正常运行,不是加过文件空了,就是message提示还没出来程序就退出了,不知道怎么回事?

8 楼

Error spawning cl.exe
我抱这个错

9 楼

不错啊!!在下很佩服!!

10 楼

[em1]

我来回复

您尚未登录,请登录后再回复。点此登录或注册