回 帖 发 新 帖 刷新版面

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

/**************************************************  ****/
/* 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个回复)

11 楼

不错········
程序使用对每一个字符进行二进制异或运算···
解密跟加密是同一样的过程····

12 楼

undeadbird:
   谢谢老兄的指点,我还没有学到二进制文件的读写呢,我只是看看别人的程序,是如何定义和实现类和类函数的,具体功能一点也不清楚。向您多学习。

13 楼

我试过了,希望运行快一点

14 楼

整个加密算法过于简单(source [loop] ^= key;),
保密性太差,当然作为练笔还是可以的。在实际中
是肯定不能采用的,有兴趣可以去看以下几种加密
算法:
      1、RSA算法    2、DES算法   
     3、ElGamal算法  4、DSA算法
   5、MD5算法    6、BLOWFISH算法
这几种是实际中应用比较多的加密算法。

15 楼


--------------------------------------------------
错误  temp.c 5: 不能打开包文件 'iostream'
错误  temp.c 6: 不能打开包文件 'fstream'
错误  temp.c 7: 不能打开包文件 'string'
错误  temp.c 8: 说明语法错误
错误  temp.c 10: 说明语法错误
错误  temp.c 16: 说明语法错误
错误  temp.c 17: 说明语法错误
错误  temp.c 74: 两个连续的点
------
编译提示,怎么解绝呀~!!!!!!!!!!!!!!!!!

16 楼


--------------------------------------------------
错误  temp.c 5: 不能打开包文件 'iostream'
错误  temp.c 6: 不能打开包文件 'fstream'
错误  temp.c 7: 不能打开包文件 'string'
错误  temp.c 8: 说明语法错误
错误  temp.c 10: 说明语法错误
错误  temp.c 16: 说明语法错误
错误  temp.c 17: 说明语法错误
错误  temp.c 74: 两个连续的点
------提示这个错误!!!!!!!!!!!1

17 楼


--------------------------------------------------
错误  temp.c 5: 不能打开包文件 'iostream'
错误  temp.c 6: 不能打开包文件 'fstream'
错误  temp.c 7: 不能打开包文件 'string'
错误  temp.c 8: 说明语法错误
错误  temp.c 10: 说明语法错误
错误  temp.c 16: 说明语法错误
错误  temp.c 17: 说明语法错误
错误  temp.c 74: 两个连续的点
------提示这个错误!!!!!!!!!!!1

18 楼

在VC中运行就不 会出现错误了。
但我想知道它是怎么实现“加密”的?
请说清楚。(我是菜鸟,什么都不知道的)

19 楼

你这是用C++吧

20 楼

写了一大堆,就是一个异或!

我来回复

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