加密:
Private Function JiaMi(ByVal varPass As String) As String '参数varPass是需要加密的文本内容
Dim varJiaMi As String * 20
Dim varTmp As Double
Dim strJiaMi As String
Dim I
For I = 1 To Len(varPass)
varTmp = AscW(Mid$(varPass, I, 1))
varJiaMi = Str$(((((varTmp * 1.5) / 5.6) * 2.7) * I))
strJiaMi = strJiaMi &varJiaMi
Next I
JiaMi = strJiaMi
End Function
解密函数:
Private Function JieMi(ByVal varPass As String) As String '参数varPass是需要解密的密文内容
Dim varReturn As String * 20
Dim varConvert As Double
Dim varFinalPass As String
Dim varKey As Integer
Dim varPasslenth As Long
varPasslenth = Len(varPass)
For I = 1 To varPasslenth / 20
varReturn = Mid(varPass, (I - 1) * 20 + 1, 20)
varConvert = Val(Trim(varReturn))
varConvert = ((((varConvert / 1.5) * 5.6) / 2.7) / I)
varFinalPass = varFinalPass &ChrW(Val(varConvert))
Next I
JieMi = varFinalPass
End Function
扩展资料:注意事项
编写加密程序,将用户输入的一个英文句子加密为加密字符串,然后输出加密字符串。假设句子长度不超过100个字符。
根据给定的句子加密函数原型SentenceEncoding,编写函数SentenceEncoding调用给定的字符加密函数CharEncoding完成句子加密。
然后,编写主程序提示用户输入英文句子,然后调用函数SentenceEncoding对句子加密,最后输出加密后的句子。
字符加密规则为大写蔽铅字母和改禅小写字母均加密为其补码, 我们定义ASCII码值相加为’A’+’Z’即155的两个大写字母互为补码,ASCII码值相加为’a’+’z’即219的两个小写字母互为补码。
空格用@代替,句号以#代替,其它字符用句点代宏歼好替。
函数原型:
void SentenceEncoding(char *soure,char *code)
功能:对待加密字符串source加密后保存加密字符串到code.
参数:char *soure,指向待加密句子的字符串指针;
char *code 指向加密字符串的字符串指针;
字符加密函数代码。
在下面程序的基础上改写一下算法。#include<stdio.h>
#include<stdlib.h>
#include<string.h>
void encfile(char *in_filename,char *pwd,char *out_filename)/*对文件进行加密的具体函数*/
int main(int argc,char *argv[])/*定义main()函腊州数的命令行参数*/
{
char in_filename[30]/*用户输入的要加密的文件名*/
char out_filename[30]/*用户输入加密后保存的文件名*/
char pwd[8]/*用来保存密码*/
if(argc!=4){/*容错处理*/
printf("\nPlease input In-filename:\n")
gets(in_filename)/*得到要加密的文件名*/
printf("Please input your Password:\n")
gets(pwd)/*得到密码*/
printf("Please input Out-filename:\n")
gets(out_filename)/*得到加密后你要的文件名*/
encfile(in_filename,pwd,out_filename)/*函数调用*/
}
else{/*如果命令行参数正确,便直接运行程序*/
strcpy(in_filename,argv[1])
strcpy(pwd,argv[2])
strcpy(out_filename,argv[3])
encfile(in_filename,pwd,out_filename)
}
system("pause")
return 0
}
/*加密子函数开始*/
void encfile(char *in_filename,char *pwd,char *out_file)
{
FILE *fp1,*fp2
register char ch
int j=0
int j0=0
fp1=fopen(in_filename,"rb")/*以二进制只读方式打开要加密的文件*/
if(fp1==NULL){
printf("cannot open in-file.\n")
exit(1)/*如果不能打开要加密的文件,便退出程序*/
}
fp2=fopen(out_file,"wb")
if(fp2==NULL){
printf("cannot open or create out-file.\n")
exit(1)/*如果不能建立加密后野局液的文件,便退出*/
}
while(pwd[++j0])
ch=fgetc(fp1)
/*加颂物密算法开始*/
while(!feof(fp1)){
fputc(ch^pwd[j>=j0?j=0:j++],fp2)/*异或后写入fp2文件*/
ch=fgetc(fp1)
}
fclose(fp1)/*关闭源文件*/
fclose(fp2)/*关闭目标文件*/
}
#include <stdio.h>#define isletter( c ) ( ((c)>='a'&&(c)<='z') || ((c)>='A'&&(c)<='Z') )
void Enc( const char *str, char *out, int key )
{
int i = 0
while( str[i] )
陪者配{
if ( isletter( str[i] ) )
{
out[i] = str[i] + key
if ( ! isletter( out[i]) )
out[i] -= 26
}
else
out[i] = str[i]
i++
}
out[i] = 0
}
void Denc( const char *str, char *out, int key )
{
int i=0
while( str[i] )
{
嫌知 if ( isletter( str[i] ) )
芦指 {
out[i] = str[i] - key
if ( ! isletter( out[i] ) )
out[i] += 26
}
else
out[i] = str[i]
i++
}
out[i] = 0
}
int main()
{
char out[100], out2[100]
Enc( "THE QUICK BROWn fox jumps over THE LAZY DOG", out, 3 )
printf( "%s\n", out )
Denc( out, out2, 3 )
printf( "%s\n", out2 )
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)