第八周(1) 项目4-字符串加密

【项目-字符串加密】 
  一个文本串可用事先编制好的字符映射表进行加密。例如,设字符映射表为:

abcdefghijklmnopqrstuvwxyz
ngzqtcobmuhelkpdawxfyivrsj
 
 
  • 1
  • 2

   则字符串“lao he jiao shu ju jie gou”被加密为“enp bt umnp xby uy umt opy”。 

   设计一个程序,实现加密、解密算法,将输入的文本进行加密后输出,然后进行解密并输出。


#include <stdio.h>
#include "sqString.h"

SqString A,B; //用于存储字符映射表

SqString EnCrypt(SqString p)
{
    int i=0,j;
    SqString q;
    while (i<p.length)
    {
        for (j=0; p.data[i]!=A.data[j]&&j<A.length; j++);
        if (j>=A.length)            //在A串中未找到p.data[i]字母
            q.data[i]=p.data[i];
        else                        //在A串中找到p.data[i]字母
            q.data[i]=B.data[j];
        i++;
    }
    q.length=p.length;
    return q;
}

SqString UnEncrypt(SqString q)
{
    int i=0,j;
    SqString p;
    while (i<q.length)
    {
        for (j=0; q.data[i]!=B.data[j]&&j<B.length; j++);
        if (j>=B.length)            //在B串中未找到q.data[i]字母
            p.data[i]=q.data[i];
        else                    //在B串中找到q.data[i]字母
            p.data[i]=A.data[j];
        i++;
    }
    p.length=q.length;
    return p;
}

int main()
{
    SqString p,q;
    StrAssign(A,"abcdefghijklmnopqrstuvwxyz");  //建立A串
    StrAssign(B,"ngzqtcobmuhelkpdawxfyivrsj");  //建立B串
    char str[MaxSize];
    printf("输入原文串:");
    gets(str);                                  //获取用户输入的原文串
    StrAssign(p,str);                           //建立p串
    printf("加密解密如下:\n");
    printf("  原文串:");
    DispStr(p);
    q=EnCrypt(p);                               //p串加密产生q串
    printf("  加密串:");
    DispStr(q);
    p=UnEncrypt(q);                         //q串解密产生p串
    printf("  解密串:");
    DispStr(p);
    printf("\n");
    return 0;
}
 
 
  • 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

下面的解决有bug,出错原因在于,for循环中的比较,没有考虑找不到时会越界(见注释)。 

#include <stdio.h>
#include "sqString.h"

SqString A,B; //用于存储字符映射表

SqString EnCrypt(SqString p)
{
    int i=0,j;
    SqString q;
    while (i<p.length)
    {
        for (j=0; p.data[i]!=A.data[j]; j++); //循环条件少加了&&j<A.length
        if (j>=p.length)            //在A串中未找到p.data[i]字母
            q.data[i]=p.data[i];
        else                        //在A串中找到p.data[i]字母
            q.data[i]=B.data[j];
        i++;
    }
    q.length=p.length;
    return q;
}

SqString UnEncrypt(SqString q)
{
    int i=0,j;
    SqString p;
    while (i<q.length)
    {
        for (j=0; q.data[i]!=B.data[j]; j++);//循环条件少加了&&j<B.length
        if (j>=q.length)            //在B串中未找到q.data[i]字母
            p.data[i]=q.data[i];
        else                    //在B串中找到q.data[i]字母
            p.data[i]=A.data[j];
        i++;
    }
    p.length=q.length;
    return p;
}

int main()
{
    SqString p,q;
    StrAssign(A,"abcdefghijklmnopqrstuvwxyz");  //建立A串
    StrAssign(B,"ngzqtcobmuhelkpdawxfyivrsj");  //建立B串
    char str[MaxSize];
    printf("\n");
    printf("输入原文串:");
    gets(str);                                  //获取用户输入的原文串
    StrAssign(p,str);                           //建立p串
    printf("加密解密如下:\n");
    printf("  原文串:");
    DispStr(p);
    q=EnCrypt(p);                               //p串加密产生q串
    printf("  加密串:");
    DispStr(q);
    p=UnEncrypt(q);                         //q串解密产生p串
    printf("  解密串:");
    DispStr(p);
    printf("\n");
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值