求python中的恺撒密码的加密,解密,以及破解的程序

求python中的恺撒密码的加密,解密,以及破解的程序,第1张

凯撒密码作为一种最为古老的对称加密芦汪体制,在古罗马的时候都已经很流行,他的基本思想是:通过把字母移动一定的位数来实现加密和解密。明文中的陪颂仔所有字母都在字母表上向后(或向前)按照一个固定数目进行偏移后被替换成密文。例如,当偏移量是3的时候,所有的字母樱慎A将被替换成D,B变成E,以此类推X将变成A,Y变成B,Z变成C。由此可见,位数就是凯撒密码加密和解密的密钥。

如下代码是以偏移量为13展开计算的。123

源代码如下:

sr1="abcdefghijklmnopqrstuvwxyz"sr2=sr1.upper()

sr=sr1+sr1+sr2+sr2

st="The Zen of Python"sResult=""for j in st:if j==" ":

sResult = sResult +" "

continue

i=sr.find(j)if(i>-1):

sResult=sResult+sr[i+13]print sResult12345678910111213

运行结果为:

Gur Mra bs Clguba

#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 )

}


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/yw/8278923.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-04-14
下一篇 2023-04-14

发表评论

登录后才能评论

评论列表(0条)

保存