首页 > 代码库 > 字符串加解密

字符串加解密

/******************************************************************************

  Copyright (C), 2001-2011, Huawei Tech. Co., Ltd.

 ******************************************************************************
  File Name     : 
  Version       : 
  Author        : 
  Created       : 2012/03/12
  Last Modified :
  Description   : 
  Function List :
              
  History       :
  1.Date        : 2012/03/12
    Author      : 
    Modification: Created file

******************************************************************************/
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <iostream>
using namespace std;

//1、对输入的字符串进行加解密,并输出。
//
//2加密方法为:
//当内容是英文字母时则用该英文字母的后一个字母替换,同时字母变换大小写,如字母a时则替换为B;字母Z时则替换为a;
//当内容是数字时则把该数字加1,如0替换1,1替换2,9替换0;
//其他字符不做变化。
//
//3、解密方法为加密的逆过程。


int Encrypt (char password[], char result[])
{
	/* 代码在这里实现 */
	if (NULL == password || NULL == result)
	{
		return 0;
	}
	int iActualCur = 0;
	while ('\0' != password[iActualCur])
	{
		if ('a' <= password[iActualCur] && 'z' >= password[iActualCur])
		{
            result[iActualCur] = 'A' + (password[iActualCur] - 'a' +1)%26;
		}
        else if ('A' <= password[iActualCur] && 'Z' >= password[iActualCur])
        {
            result[iActualCur] = 'a' + (password[iActualCur] - 'A' +1)%26;
        }
        else if ('0' <= password[iActualCur] && '9' >= password[iActualCur])
        {
            result[iActualCur] = '0' + (password[iActualCur] - '0' +1)%10;
        }
        else
        {
            result[iActualCur] = password[iActualCur] ;
        }
        iActualCur++;
	}
    result[iActualCur] = '\0';
	return 0;
}

int unEncrypt (char result[], char password[])
{
	/* 代码在这里实现 */
    if (NULL == password || NULL == result)
    {
        return 0;
    }
    int iActualCur = 0;
    while ('\0' != result[iActualCur])
    {
        if ('a' <= result[iActualCur] && 'z' >= result[iActualCur])
        {
            password[iActualCur] = 'A' + (result[iActualCur] - 'a' + 25)%26;
        }
        else if ('A' <= result[iActualCur] && 'Z' >= result[iActualCur])
        {
            password[iActualCur] = 'a' + (result[iActualCur] - 'A' + 25)%26;
        }
        else if ('0' <= result[iActualCur] && '9' >= result[iActualCur])
        {
            password[iActualCur] = '0' + (result[iActualCur] - '0' +9)%10;
        }
        else
        {
            password[iActualCur] = result[iActualCur] ;
        }
        iActualCur++;
    }
    password[iActualCur] = '\0';
    
	return 0;
}


int main()
{
    char password[] = "Huawei1234";
    char excepted[] = "iVBXFJ2345";
    char actual[101];

    Encrypt (password, actual);
   
    cout<<actual<<endl;
    char UnEncryptPassword[101];

    unEncrypt (actual, UnEncryptPassword);
    cout<<UnEncryptPassword<<endl;
    


    return 0;
}

字符串加解密