现在的位置: 首页 > 综合 > 正文

ceasar算法对一个文件加密

2013年09月01日 ⁄ 综合 ⁄ 共 1200字 ⁄ 字号 评论关闭

/*
    CAesar Ancrypt Algorithims
*/

/*
    Author : Guo Junling
    Date : 2005.4.13
*/

#include <iostream>
#include <fstream>

using namespace std ;

//Function : Encrypt
//Parameters : char , int
//destination : Encrypt a char
//Return : char ( Encrypted )
char encrypt( char &c , int d ) ;

int main()
{
    /*distance is the encrypt distance*/
    int distance =0 ;

    /*read char from file and store it in char_Input*/
    char c_Input ;

    /*Encrypt c_Input and store it in c_output*/
    char c_Output ;
   
    /*Input distance*/
    cout << " Please input the distance you want to use in encryption: " << endl ;
    cin >> distance ;
   
    /*open a file to be encrypted*/
    ifstream in("E7input.txt" , ios::in) ;
    if( !in )
    {
        cerr << " Error in opening file E7input " << endl ;
    }

    /* open a file for output */
    ofstream out( " E7output.txt " ,ios::out ) ;
    if( !out )
    {
        cerr << " Error in opening file E7output " << endl ;
    }
    while( in.get( c_Input ) )
    {
        if( ( ( c_Input >= 'a') && ( c_Input <= 'z' ) )|| ( (c_Input >= 'A' )&&( c_Input <= 'Z' ) ) )
            c_Output = encrypt( c_Input , distance ) ;

        else
            c_Output = c_Input ;
        out.put( c_Output ) ;
    }
    return 0 ;
}

/*Definition of function encrypt*/
char encrypt( char &c , int d )
{
    char t ;
    /*d is less than 26*/
    d %= 26 ;
    t = c + d ;
    if( t > 'z' )
        t -= 26 ;
    if ( ( c <= 'Z' ) && ( c + d > 'Z' ) )
        t -= 26 ;
    return t ;
}

抱歉!评论已关闭.