软件开发实验题 加密函数

"加密函数。输入待加密字串pSource,和加密秘钥pKey,输出加密后的字串pEncrypted
1加密规则:顺宇取出待加密字串的字符,依次取出秘钥中数宇(秘钥取完后再从头开始农次再取),相加后得到新的字符。
只对a-z,A-2加密,其余宇符加密后不变化。
"输入"I am a student.","1314”,输出"Jbqdwuxeiow."
即『T+1=j,“+3="'2+1=5,m+4=g,^+1="a'+3=’d;依次类推。
1秘钥只能是数宇,判断输入是否合法,非法返回0,合法计算结果pRet,并返回1.
int EncodeFunc(const char* pSource, const char* pKey, char* pEncrypted)
{
return U;
"解密函数,规则与加密函数相反
"输入加密后的字串pEncrypred和秘钥pkey,输出解密后的字符pSource)
int DecodeFunc(const char* pEncrypted, const char* pKey, char* pSource)
return U;

供参考:

#include <stdio.h>
int EncodeFunc(const char* pSource, const char* pKey, char* pEncrypted)
{
    int i, len;
    for (i = 0; pKey[i]; i++)//秘钥只能是数宇,判断是否合法
        if (pKey[i]< '0' || pKey[i] > '9')
            return 0;
    for (len = i, i = 0; pSource[i]; i++)
    {
        if (pSource[i] >= 'a' && pSource[i] <= 'z')
            pEncrypted[i] = (pSource[i] - 'a' + pKey[i%len] - '0') % 26 + 'a';
        else if (pSource[i] >= 'A' && pSource[i] <= 'Z')
            pEncrypted[i] = (pSource[i] - 'A' + pKey[i%len] - '0') % 26 + 'A';
        else
            pEncrypted[i] = pSource[i];
    }
    pEncrypted[i] = '\0';
    return 1;
}

int DecodeFunc(const char* pEncrypted, const char* pKey, char* pSource)
{
    int i, len;
    for (i = 0; pKey[i]; i++)//秘钥只能是数宇,判断是否合法
        if (pKey[i]< '0' || pKey[i] > '9')
            return 0;
    for (len = i, i = 0; pEncrypted[i]; i++)
    {
        if (pEncrypted[i] >= 'a' && pEncrypted[i] <= 'z')
            pSource[i] = (pEncrypted[i] - 'a' - pKey[i%len] + '0' + 26) % 26 + 'a';
        else if (pEncrypted[i] >= 'A' && pEncrypted[i] <= 'Z')
            pSource[i] = (pEncrypted[i] - 'A' - pKey[i%len] + '0' + 26) % 26 + 'A';
        else
            pSource[i] = pEncrypted[i];
    }
    pSource[i] = '\0';
    return 1;
}

int main()
{
    char pSource1[128]="I am a student.", pSource2[128], pKey[128]="1314", pEncrypetd[128];

    if (EncodeFunc(pSource1,  pKey, pEncrypetd)) // 加密
        printf("%s", pEncrypetd);
    else
        printf("pKey error.");

    printf("\n");

    if (DecodeFunc(pEncrypetd, pKey, pSource2))  // 解密
        printf("%s", pSource2);
    else
        printf("pKey error.");
    return 0;
}

  • 以下回答由chatgpt基于相关博客总结生成:
    import random
    
    def Letter_num(Letter):
        '''
        a-z转换为0-25
        '''
        return ord(Letter) - 97
    
    
    def num_Letter(num):
        '''
        0-25转换为a-z
        '''
        return chr(num + 97)
    
    
    def encryption_Letter(P: str, K: int) -> str:
        '''
        加密单个字母
        '''
        C = num_Letter((Letter_num(P) + K) % 26)
        return C
    
    
    def Decrypt_Letter(C: str, K: int) -> str:
        '''
        解密单个字母
        '''
        P = num_Letter((Letter_num(C) - K) % 26)
        return P
    
    
    def encryption_fun(P_char: str, K: int) -> str:
        '''
        加密字符串
        '''
        C_char = ''
        for P in P_char:
            if P.isalpha():
                P = encryption_Letter(P, K)
            C_char = C_char + P
        return C_char
    
    
    def Decrypt_fun(C_char: str, K: int) -> str:
        '''
        解密字符串
        '''
        P_char = ''
        for C in C_char:
            if C.isalpha():
                C = Decrypt_Letter(C, K)
            P_char = P_char + C
        return P_char
    
    
    if __name__ == '__main__':
        K = int(random.random()*26%26)
        # 加密前的明文
        P_text = "let's meet after the party.if you understand this sentence, the program runs successfully.abcd efghjgkl mnopqr stu vwxy z"
        print('原文为:',P_text)
        print('K:',K)
        # 加密后的密文
        C_text = encryption_fun(P_text, K)
        print('密文为:', C_text)
        # 解密后的明文
        PP_text = Decrypt_fun(C_text, K)
        print('解密后是:', PP_text)