-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathencrypt.cpp
More file actions
68 lines (65 loc) · 1.72 KB
/
encrypt.cpp
File metadata and controls
68 lines (65 loc) · 1.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
/*
* =====================================================================================
*
* Filename: encrypt.cpp
*
* Description:
*
* Version: 1.0
* Created: 05/08/14 10:21:50
* Revision: none
* Compiler: gcc
*
* Author: wangxinalex (), wangxinalex@gmail.com
* Organization:
*
* =====================================================================================
*/
#include <stdlib.h>
#include <string.h>
#include <string>
#include <iostream>
#include "md5.h"
#include "encrypt.h"
using namespace std;
/*
* === FUNCTION ======================================================================
* Name: encrypt
* Description: encrypt the plain text using the md5 key
* =====================================================================================
*/
string encrypt(string text, string key){
MD5 md5;
md5.reset();
md5.update(key);
string sec_key = md5.toString().substr(0,KEY_LEN);
for(size_t i = 0; i < text.size();i++){
text[i]+=sec_key[i%KEY_LEN];
}
return text;
}
/*
* === FUNCTION ======================================================================
* Name: decrypt
* Description: decrypt the encrypted text
* =====================================================================================
*/
string decrypt(string text, string key){
MD5 md5;
md5.reset();
md5.update(key);
string sec_key = md5.toString().substr(0,KEY_LEN);
for(size_t i = 0; i < text.size();i++){
text[i]-=sec_key[i%KEY_LEN];
}
return text;
}
/*
int main(){
string s("open nfsjia 1");
string encrypted=encrypt(s,ENCRYPT_KEY);
cout << encrypted<<endl;
string plain = decrypt(encrypted,ENCRYPT_KEY);
cout << plain<<endl;
return 0;
}*/