字符串加密函数
① 如何对字符串进行MD5加密,用c语言实现,给出源代码和加密函数
UINT4 state[4]; /*存储原始信息的bits数长度,不包括填充的bits,最长为2^64 bits*/ UINT4 count[2]; /*存放输入的信息的缓冲区,512bits*/ unsigned char buffer[64];} MD5_CTX;static void MD5Transform(UINT4[4], unsigned char[64]); static void Encode(unsigned char *, UINT4 *, unsigned int); static void Decode(UINT4 *, unsigned char *, unsigned int);/*用于bits填充的缓冲区,当欲加密的信息的bits数被512除其余数为448时,需要填充的bits的最大值为512=64*8*/ static unsigned char PADDING[64] = { 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};/*接下来的这几个宏定义是md5算法规定的,就是对信息进行md5加密都要做的运算*/
② 如何对字符串进行MD5加密,用C语言实现,给出源代码和加密函数
/*四个32bits数,用于存放最终计算得到的消息摘要.当消息长度>512bits时,也用于存放每个512bits的中间结果*/ UINT4 state[4]; /*存储原始信息的bits数长度,不包括填充的bits,最长为2^64 bits*/ UINT4 count[2]; /*存放输入的信息的缓冲区,512bits*/ unsigned char buffer[64];} MD5_CTX;static void MD5Transform(UINT4[4], unsigned char[64]); static void Encode(unsigned char *, UINT4 *, unsigned int); static void Decode(UINT4 *, unsigned char *, unsigned int);/*用于bits填充的缓冲区,当欲加密的信息的bits数被512除其余数为448时,需要填充的bits的最大值为512=64*8*/ static unsigned char PADDING[64] = { 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};/*接下来的这几个宏定义是md5算法规定的,就是对信息进行md5加密都要做的运算*/ #define F(x, y, z) (((x) & (y)) | ((~x) & (z))) #define G(x, y, z) (((x) & (z)) | ((y) & (~z))) #define H(x, y, z) ((x) ^ (y) ^ (z)) #define I(x, y, z) ((y) ^ ((x) | (~z))) #define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32-(n)))) #define FF(a, b, c, d, x, s, ac) {\ (a) += F ((b), (c), (d)) + (x) + (UINT4)(ac);\ (a) = ROTATE_LEFT ((a), (s));\ (a) += (b);\}#define GG(a, b, c, d, x, s, ac) {\ (a) += G ((b), (c), (d)) + (x) + (UINT4)(ac);\ (a) = ROTATE_LEFT ((a), (s));\ (a) += (b);\}#define HH(a, b, c, d, x, s, ac) {\ (a) += H ((b), (c), (d)) + (x) + (UINT4)(ac);\ (a) = ROTATE_LEFT ((a), (s));\ (a) += (b);\}#define II(a, b, c, d, x, s, ac) {\ (a) += I ((b), (c), (d)) + (x) + (UINT4)(ac);\
③ 如何利用MySQL数据库自带加密函数进行加密
首先,先介绍下加密函数,PASSWORD(string)函数可以对字符串string进行加密,代码如下:
SELECT
PASSWORD('you');
如下图所示:
执行第一步的SQL语句,查询结果是一串字符串,并且PASSWORD(string)函数加密是不可逆转,
如下图所示:
另外一个加密函数MD5(string),主要针对普通的数据进行加密,代码如下:
SELECT
MD5('hai');
如下图所示:
最后一个加密函数ENCODE(string,pass),可以使用字符串pass来加密字符串string。首先要创建一个数据库表t_pass_info,代码如下:
CREATE
TABLE
t_pass_info(
id
int(10),
pass_info
blob
);
如下图所示:
然后,向这个数据库表插入一条数据,代码如下:
INSERT
INTO
t_pass_info(id,pass_info)
VALUES
(1,ENCODE('dong','bb'));
如下图所示:
6
查看插入数据库表t_pass_info记录,代码如下:
SELECT
*
FROM
t_pass_info;
如下图所示:
7
MySQL自带还有一个解密函数DECODE(str,pass_str),可以使用字符串pass_str来为str解密,代码如下:
SELECT
DECODE(ENCODE('dong','aa'),'aa');
如下图所示:
④ ASP字符串加密解密
<%
'ASP 字符串加密函数EncryptText()
'strEncryptionKey:加密key字符串,用以区别不同模块加密算法
'strTextToEncrypt:欲加密字符串
Function EncryptText(ByVal strEncryptionKey, ByVal strTextToEncrypt)
Dim outer, inner, Key, strTemp
For outer = 1 To Len(strEncryptionKey)
key = Asc(Mid(strEncryptionKey, outer, 1))
For inner = 1 To Len(strTextToEncrypt)
strTemp = strTemp & Chr(Asc(Mid(strTextToEncrypt, inner, 1)) Xor key)
key = (key + Len(strEncryptionKey)) Mod 256
Next
strTextToEncrypt = strTemp
strTemp = ""
Next
EncryptText = strTextToEncrypt
End Function
response.write EncryptText("mima","zifuchuan")
%>
⑤ C++ string字符串如何加密
C++ string类重载了[]运算符,因此,可以象数组一样方便的引用string中的每一个元素,进行数据修改。如,字符串简单加密方法,A-B, B-C, C-D。。。Z-A的实现代码如下:
#include<iostream>
#include<string>
usingnamespacestd;
intmain()
{
strings;
cin>>s;//输入字符串
for(inti=0;i<s.length();i++)//遍历字符串s.length()为输入串的长度
{
if(s[i]>='A'&&s[i]<'Z')//处理A-Y
s[i]+=1;
elseif(s[i]=='Z')//处理Z
s[i]='A';
//非大写字符,不处理
}
cout<<s<<endl;//输出字符串
return0;
}
运行结果:
AsDfGXyZ
BsEfHYyA
⑥ C语言字符串加密
问题不小,你表面用的是C
但是,好多地方不符合C的语法
,,比如:
for
(int
i=0;
str[i]
!=
'\0';
i++)
还有,就是你好像没有弄清楚
,你要做什么似的,有好多无用的东西,
就像你的函数里的,key
,虽然你提到key了,但是你根本没有使用key,你只是使用45来进行加密,,还有就是一个文件
的大小,是不确定的,你用一个100个字符的字符串来存,有点那个不安全了,,如果稍长一点就会出问题,产生运行时错误。其实你这个加密和解密是一个可逆过程,用一个函数,就可以了,具体你想要的也不是太明白,就给你弄了一个简单一点加密和解密程序
,输入输出不是同一个文件
,不知道是不是你想要的。
#include
#include
#include
void
Decrypt()
{
char
fname[FILENAME_MAX];
char
fname2[FILENAME_MAX];
FILE*
fp;
FILE*
fp1;
int
key;
char
c;
printf("输入要加/解密文件的路径:\n");
scanf("%s",
fname);
printf("请输入密钥:\n");
scanf("%d",&key);
strcpy(fname2,fname);
strcat(fname2,".txt");
if(
(fp
=
fopen(fname,"r+"))
==
NULL)
{
printf("error");
exit(1);
}
if(
(fp1
=
fopen(fname2,"w+"))
==
NULL)
{
printf("error");
exit(1);
}
while(
(c
=
fgetc(fp))
!=
EOF)
{
c
=
c^key;
fputc(c,fp1);
}
fcloseall();
}
int
main()
{
Decrypt();
return
0;
}
如果想看一些好一点的加密算法
,我这里有一些,联系我发给你
,,
⑦ 如何加密C++ string字符串
#include
#define MAX 100//字符串的最大长度
using namespace std;
void encrypter ( char *, int );
void decrypter (char *, int);
int main()
{
char string[MAX] = "\0";
int key;
/* Encrypter */
cout<<"**** ENCRIPTER ****"<<endl;
cout<<"Insert the secret sentence: ";
gets(string);//读取字符串
cout<<"Insert the incripting code : ";
cin>>key;//读取加密码
encrypter ( string, key );//加密
cout<<"The incrypted sentence is: "<<string<<endl<<endl;
/* Decrypter */
cout<<"**** DECRYPTER ****"<<endl;
cout<<"Insert the code: ";
cin>>key;//输入加密码
decrypter( string, key );//解密
cout<<"The decrypted sentence is: "<<string<<endl<<endl;
return 0;
}
/* 加密函数 */
void encrypter ( char *pstr, int key )
{
while ( *pstr != '\0' )
{
*pstr += key;
pstr++;
}
}
/* 解密函数 */
void decrypter ( char *pstr, int key )
{
while ( *pstr != '\0' )
{
*pstr -= key;
pstr++;
}
}
⑧ C语言字符串按要求加密 求教
void encryp(char *plain,char *cipher)这个函数你写复杂了,如下就可以了——
void encryp(char *plain,char *cipher){
int i;
for(i=0;plain[i];i++)
cipher[i]=plain[i]-24;
cipher[i]='\0';
}
这样加密就完结了。
⑨ C语言怎么加密字符
#include<stdio.h>
#include<string.h>
intmain()
{
charstr[]="00000",str2[]="00000",*p=str,*p2=str2;
printf("输入5个字母:");
while(*p!=0)
{
scanf("%c",p);
if(*p=='
')
continue;
if(*p<'A'||(*p>'Z'&&*p<'a')||*p>'z')//输入验证,必须是字母
{
printf("只能输入字母,请重新输入
");
p=str;
p2=str2;
fflush(stdin);//输入有错重新输入前清空缓冲区。fflush属于c扩展函数,正常使用没问题,如需在linuxggc上使用,考虑多次调用getchar函数来清空
}
else
{
*p2=(*p)+4;
if(*p2>90&&*p2<97)//大写字母加4,最大位不超出
*p2='A'+(*p2-90)-1;
if(*p2>122)//小写字母加4,最大位不超出
*p2='a'+(*p2-122)-1;
p2++;
p++;
}
}
printf("原字符串为:%s
加密后的字符串为:%s
",str,str2);
return0;
}
⑩ c语言编写字符串加密函数 不要写得太难啊
#include<stdio.h>
#include<ctype.h>
char*encrypt(char*text){
charc;
char*p=text;
for(;*text;++text){
c=*text;
if(isdigit(c))
*text='0'+'9'-c;
elseif(islower(c)){
c=c+3;
if(c>'z')
c=c-26;
*text=c;
}elseif(isupper(c)){
c=c+3;
if(c>'Z')
c=c-26;
*text=c;
}
}
returnp;
}
intmain(){
chartext[100];
printf("输入明文:");
scanf("%s",text);
printf("密文:%s ",encrypt(text));
getchar();
}