??加密其实并不是那么神秘,尝试后,加密应用起来也是很简单的,虽然自己一时想不到经典的加密算法出来。
??加密算法种类:
??常见加密算法:
??对于RC4加密算法,密钥长度达到128位时,暴力破解基本很难破解了。下面是C++的实现:
encryptdecode.h
#ifndef __ENCRYPTDECODE_H__
#define __ENCRYPTDECODE_H__
#include<stdio.h>
#include<string.h>
class EncryptDecode{
private:
void encrypt_decode_init(unsigned char *s,char *key,unsigned int keyLen);
void encrypt_decode(unsigned char *s,unsigned char*Data,unsigned long textLen);
};
#endif
encryptdecode.cpp
#include "EncryptDecode.h"
void EncryptDecode::encrypt_decode_init(unsigned char*s,char*key,unsigned int keyLen)
{
int i=0,j=0;
char k[256]={0};
unsigned char tmp=0;
for(i=0;i<256;i++)
{
s[i]=i;
k[i]=key[i%keyLen];
}
for(i=0;i<256;i++)
{
j=(j+s[i]+k[i])%256;
tmp=s[i];
s[i]=s[j];
s[j]=tmp;
}
}
void EncryptDecode::encrypt_decode(unsigned char*s,unsigned char*Data,unsigned long textLen)
{
int i=0,j=0,t=0;
unsigned long k=0;
unsigned char tmp;
for(k=0;k<textLen;k++)
{
i=(i+1)%256;
j=(j+s[i])%256;
tmp=s[i];
s[i]=s[j];
s[j]=tmp;
t=(s[i]+s[j])%256;
Data[k] ^= s[t];
}
}
main.cpp
#include "encryptdecode.h"
#include <stdio.h>
int main(int argc, char *argv[])
{
unsigned char s_box[256] = "";
unsigned char buf[512] = "这是一个RC4加密解密算法测试";
char key[256] = "";
int keylen = strlen(key);
//加密
encrypt_decode_init(s_box,key,keylen);
encrypt_decode(s_box,buf,strlen(buf));
printf("Encrypt:%s\n",buf);
//解密(s_box在加密中已更改,需重新获得s_box)
encrypt_decode_init(s_box,key,keylen);
encrypt_decode(s_box,buf,strlen(buf));
printf("Decode:%s\n",buf);
return 0;
}
版权声明:本文为博主[原创]文章,未经博主允许可以转载,注明博客出处:[http://blog.csdn.net/FreeApe]
原文地址:http://blog.csdn.net/freeape/article/details/47362547