PHP,C++ 通用加解密,需要md5,base64 支持
时间过太久,代码从哪里下载的往了,不过我自己做过修改,其中C++ 可能包含部分 C++11 的库函数,编译不过的话,移除即可
个人觉得 base64 大家可以想法子把它美化一下
PHP
<?php
//md5 函数使用小写
/**
* 可逆的字符串加密函数
* @param int $txtStream 待加密的字符串内容
* @param int $password 加密密码
* @return string 加密后的字符串
*/
function enCrypt($txtStream,$password){
//密锁串,不能出现重复字符,内有A-Z,a-z,0-9,/,=,+,_,
$lockstream = 'GHI9+JKLxyz012MNOPYbcRSTUVW8/ABCw3DEZaFXefghijklm7=nopqrsdQtuv456';
//随机找一个数字,并从密锁串中找到一个密锁值
$lockLen = strlen($lockstream);
$lockCount = rand(0,$lockLen-1);
$randomLock = $lockstream[$lockCount];
//结合随机密锁值生成MD5后的密码
$password = md5($password.$randomLock);
//开始对字符串加密
$txtStream = base64_encode($txtStream);
$tmpStream = '';
$i=0;$j=0;$k = 0;
for ($i=0; $i<strlen($txtStream); $i++) {
$k = ($k == strlen($password)) ? 0 : $k;
$j = (strpos($lockstream,$txtStream[$i])+$lockCount+ord($password[$k]))%($lockLen);
$tmpStream .= $lockstream[$j];
$k++;
}
return $tmpStream.$randomLock;
}
/**
* 可逆的字符串解密函数
* @param int $txtStream 待加密的字符串内容
* @param int $password 解密密码
* @return string 解密后的字符串
*/
function deCrypt($txtStream,$password){
//密锁串,不能出现重复字符,内有A-Z,a-z,0-9,/,=,+,_,
$lockstream = 'GHI9+JKLxyz012MNOPYbcRSTUVW8/ABCw3DEZaFXefghijklm7=nopqrsdQtuv456';
$lockLen = strlen($lockstream);
//获得字符串长度
$txtLen = strlen($txtStream);
//截取随机密锁值
$randomLock = $txtStream[$txtLen - 1];
//获得随机密码值的位置
$lockCount = strpos($lockstream,$randomLock);
//结合随机密锁值生成MD5后的密码
$password = md5($password.$randomLock);
//开始对字符串解密
$txtStream = substr($txtStream,0,$txtLen-1);
$tmpStream = '';
$i=0;$j=0;$k = 0;
for($i=0; $i<strlen($txtStream); $i++){
$k = ($k == strlen($password)) ? 0 : $k;
$j = strpos($lockstream,$txtStream[$i]) - $lockCount - ord($password[$k]);
while($j < 0){
$j = $j + ($lockLen);
}
$tmpStream .= $lockstream[$j];
$k++;
}
return base64_decode($tmpStream);
}
echo enCrypt("3","123456789");
?>
C++
//与 PHP 通用加解密
std::string phpEnCode(std::string& word, std::string& pass)
{
std::string lock_stream = "GHI9+JKLxyz012MNOPYbcRSTUVW8/ABCw3DEZaFXefghijklm7=nopqrsdQtuv456";
int lock_len = lock_stream.length();
srand((unsigned int)time(NULL));
int lock_num = rand() % lock_len;
char random = lock_stream[lock_num];
pass += random;
pass = MD5(pass).toString();
word = base64_encode(word);
std::string res = "";
int j = 0, k = 0;
for (int i = 0; i< (int)word.length(); ++i)
{
k = (k == pass.length()) ? 0 : k;
char y = pass[k];
j = (lock_stream.find(word[i]) + lock_num + (int)y) % lock_len;
res += lock_stream[j];
k++;
}
res += random;
return std::move(res);
}
std::string phpDnCode(std::string& word, std::string& pass)
{
std::string lock_stream = "GHI9+JKLxyz012MNOPYbcRSTUVW8/ABCw3DEZaFXefghijklm7=nopqrsdQtuv456";
int lock_len = lock_stream.length();
char random = word[word.length() - 1];
int lock_num = (int)lock_stream.find(random);
pass += random;
pass = MD5(pass).toString();
std::string res = "";
int j = 0, k = 0;
for (int i = 0; i < (int)word.length() - 1; ++i)
{
k = (k == pass.length()) ? 0 : k;
char y = pass[k];
j = lock_stream.find(word[i]) - (int)y - lock_num;
while (j < 0) j += lock_len;
res += lock_stream[j];
k++;
}
res = base64_decode(res);
/*size_t nnn = res.find('\001');
if (nnn != std::string::npos) res = res.substr(0, nnn);*/
return std::move(res);
}
cmd5.h
/*
*兼容网页版 MD5 的 C++ 加密
*用法:
* 1.字符串加密:
* #include <string>
* string cc = "Login:aa|asdaf";
* cout << MD5(cc).toString() << endl;
* 2.文件加密使用函数:
* cout << FileDigest("D:\\key.txt") << endl;
*另附一个最简单的,在原位置逐一异或加解密函数
* void EnCode(char* source, char* pass)
*/
#ifndef MD5_H
#define MD5_H
#include <string>
#include <fstream>
/* Type define */
typedef unsigned char byte;
typedef unsigned long ulong;
using std::string;
using std::ifstream;
/* MD5 declaration. */
class MD5 {
public:
MD5();
MD5(const void *input, size_t length);
MD5(const string &str);
MD5(ifstream &in);
void update(const void *input, size_t length);