讲解:Security:CSE231 DumbcryptPython

用Python代写加解密算法Dumbcrypt,一个类似Affine Cipher和Caesar Cipher的变种算法。Assignment Overview (learning objectives)This assignment will give you more experience on the use of:integers (int)conditionalsiterationstringThe goal of this project is to implement a different cryptographic technique by combining two cryptographic techniques. I call it dumbcrypt. Dumbcrypt is a combination of Affine Cipher and Caesar Cipher. The user will enter a sentence, a string composed of letters, numbers, and punctuation. Notice that spaces are not allowed in the input string because spaces provide too many clues to someone trying to crack your coded message. Your job is to encrypt the sentence using a combination of Affine and Caesar Cipher. The letters and numbers will be encrypted and decrypted using Affine Cipher; the punctuation using Caesar Cipher.Assignment BackgroundAffine Cipher - https://en.wikipedia.org/wiki/Affine\_cipherAffine Cipher is a bit different than Caesar Cipher in that the Affine Cipher uses an encryption function to calculate the integer that corresponds to the cipher text letter. The encryption function for a letter variable named x isE(x) = (Ax+N) mod M Where A and N are keys of the cipher and M is size of the alphabet (for example, M=26 for English letters; M = 36 for English letters plus digits). The decryption of Affine Cipher only works when A and M are co-primes of each other.N will be an input to the program and will be fixed for the whole run of the program. N is sometimes referred to as the “rotation” but is effectively a key.Co-primes are a pair of numbers whose greatest common divisor (GCD) is 1. Only one co-prime is needed (of possibly many). For this project we choose the smallest greater than one so everyone is using the same onemaking testing feasible.Algorithm:Calculate M, the number of characters in the alphabet (Hint: use len())Calculate A using get_smallest_co_prime(M)For the character to be encrypted find its index in the alphabet: x in the formula is the indexApply the formula to get the index of the cipher character: E(x) = (Ax+N) mod MUsing the index get the cipher character from the alphabetReturn the character.Decryption is a little more complicated than encryption. The decryption function isD(x) = A^-1(x-N) mod M Where A^-1 is the modular multiplicative inverse (https://en.wikipedia.org/wiki/Modular\_multiplicative\_inverse). We provide the function multiplicative_inverse(A,M) that calculates the multiplicative inverse, A^-1 , for you. The function checks all the numbers x from 1 to M and for every number x checks if (A*x) mod M is 1. Once you have the A^-1, you can use the decryption function to calculate the integer that corresponds to plaintext letter.The decryption algorithm is the same as encryption with two modifications:Algorithm:Calculate M, the number of characters in the alphabet (Hint: use len())Calculate A using get_smallest_co_prime(M)Calculate A-1 using the function multiplicative_inverse(A,M) .For the character to be encrypted find its index in the alphabet: x in the formula is the indexApply the formula to get the index of the cipher character: D(x) = A^-1(x-N) mod M Note the formula difference from encryption: the parentheses are different and it has subtraction.Using the index get the cipher character from the alphabetReturn the character.Caesar Cipher - https://en.wikipedia.org/wiki/Caesar\_cipherCaesar Cipher is a simplified form of Affine cipher which follows similar encryption and decryption algorithm. CaeSecurity代写:CSE231 Dumbcrypt代写Python实验sar Cipher uses a single key to calculate the integer that corresponds to the ciphertext letter. The encryption function isE(x) = (x+N) mod M The decryption function isD(x) = (x-N) mod M The algorithms for Caesar Cipher are similar to the Affine Cipher algorithmsProject DescriptionYour program must meet the following specifications:You have to use the eight functions in the provided proj04.py skeleton. You have to implement and use the following:a. check_co_prime(num, M): Takes two numbers as parameters. Returns True if num and M are co-primes, otherwise returns False. (Hint: used GCD)b. get_smallest_co_prime(M): Takes one number M as a parameter: Returns the smallest coprime of M that is greater than 1. For example, if M = 26, the smallest co- prime greater than one is 3. If M = 210, the smallest co-prime greater than one is 11. (Hint: use check_co_prime)c. caesar_cipher_encryption(ch,N,alphabet): Takes three inputs: a character to encrypt, the rotation N, and the alphabet. Returns the cipher text character using the Caesar Cipher.d. caesar_cipher_decryption(ch,N, alphabet): Takes three inputs: a character to encrypt, the rotation N, and the alphabet. Returns the plain text character using the Caesar Cipher.e. affine_cipher_encryption(ch,N, alphabet): Takes three inputs: a character to encrypt, the rotation N, and the alphabet. Returns the cipher text character using the Affine Cipher.f. affine_cipher_decryption(ch,N,alphabet):Takesthreeinputs:a character to encrypt, the rotation N, and the alphabet. Returns the plain text character using the Affine Cipher.g. main(): Takes no input. Returns nothing. First prompts for rotation, N. Then prompts for a command (d,e,q), prompts for a string, decrypts or encrypts the string depending on the command using the encryption and decryption functions described above. Remember to use a different encryption/decryption algorithm for punctuation.Assignment DeliverableThe deliverable for this assignment is the following file:proj04.py - the source code for your Python program Be sure to use the specified file name and to submit it for grading via Mimir before the project deadline.Assignment Notes and Other RequirementsTo clarify the project specifications, sample output is appended to the end of this document.Convert all letters to lower case before encryption or decryption.Items 1-9 of the Coding Standard will be enforced for this project.We provide a proj04.py program for you to start with.You may import the GCD function from a module such as math.You are not allowed to use advanced data structures such as lists, sets, dictionaries and classes.You are not allowed to use the chr() function.There are two alphabets provided: numbers + letters and string.punctuationThe mod operator in the encryption formulas is the % operator in Python.The string method isalnum() returns True if the string is composed of letters and digits.The N in the formulas is often referred to as the “rotation” and is the first value prompted for. It effectively is the key for the encryption and decryption.Error checking (Hint: do error checking after everything else is working.)a. Re-prompt if the rotation is not an int.b. Print an error message and ignore any commands that are not ‘e’, ‘d’ or ‘q’.c. Print an error message if any of the text is not a letter, digit or punctuation. Note that a space will generate an error (spaces provide too many clues for cracking codes).A strings.txt file of strings used is provided to help with testing.I found else with the for statement to be useful to not print plain and cipher texts if there was an error with a character.转自:http://ass.3daixie.com/2019012256014380.html

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 214,717评论 6 496
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 91,501评论 3 389
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 160,311评论 0 350
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 57,417评论 1 288
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 66,500评论 6 386
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 50,538评论 1 293
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 39,557评论 3 414
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 38,310评论 0 270
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 44,759评论 1 307
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,065评论 2 330
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,233评论 1 343
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 34,909评论 5 338
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 40,548评论 3 322
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,172评论 0 21
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,420评论 1 268
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 47,103评论 2 365
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,098评论 2 352

推荐阅读更多精彩内容