Skip to main content
留学咨询

CSC322课业解析

By May 15, 2020No Comments

题意: 用C语言编写一个名为密码机的系统程序,支持代码的加密和解密功能解析: Part A: 基础命令程序能识别下列5条指令
encrypt 基于凯撒密码对用户输入加密
decrypt 基于凯撒密码对用户输入解密
encode 基于ASCII对用户输入编码
decode 基于ASCII对用户输入解码
exit 退出程序Part B: 凯撒密码:encrypt/decrypt凯撒密码是一种加密方式,将26个字母的对应关系全体向前或向后偏移n个单位。例如明文是abc,加密规则是向后偏移3个单位,那么密文就是def。需要注意的是,后置位的字母超出范围后将对应到前置位的字母上。在这个例子中,明文xyz对应的密文是abc。
实现方法:一次给26个字母按增序编码,例如0、1、2……24、25;
明文a 加密公式E(3,a)=E(3,0)=(3+0)%26=3,根据对应关系加密结果是d。
密文a 解密公式D(3,a)=D(3,0)=(0-3)%26=23,根据对应关系解密结果是x。
由于需要处理数字符号等字符,需要把字符集扩充到ASCII,例如”A”是65,”0″是48,“a”是97。Part C: 编码和解码(奖励分)把用户输入转换成二进制码,方法是先把它转为ASCII码,把对应的ASCII码再转换为二进制编码。要注意的是最多使用8位编码,如果缺省补0。例如107对应的二进制数是1101011,但是用0补全缺省,改为01101011。涉及知识点:ASCII码,C语言编程更多可加微信讨论微信号:IT_51zuoyejunpdf全文CSC 322 Systems Programming Fall 2019Lab Assignment L1: Cipher-machineDue: Monday, September 231 GoalIn the first lab, we will develop a system program (called cipher-machine) which can encryptor decrypt a code using C language. It must be an errorless program, in which user repeatedlyexecutes pre-defined commands and quits when he or she wants to exit. For C beginners, thisproject will be a good initiator to learn a new programming language. Students who alreadyknow C language will also have a good opportunity to warm up their programming abilitiesand get prepared for the rest of labs.2 IntroductionA. Basic CommandsThe system program provides three basic commands shown in Table 1. The program runsa related function or exits when a user enters a command as seen in Figure 1. Note thatcommands need parameters. Misspelled command should be handled as an exception, andthe program must show an error message. Note that program must be in running and showsthe next prompt.Table 1. Basic CommandsCommand Descriptionencrypt Encrypt user’s input based on Caesar’s cipher. See the section II-B.decrypt Decrypt user’s input based on Caesar’s cipher. See the section II-B.encode Encode user’s input to get a binary code based on ASCII code. See thesection II-C. (extra)decode Decode user’s binary input to get a character code. See the section II-C.(extra)exit Exit the programThe program should run on Linux machine at cs.oswego.edu. Once it is compiled andexecuted, it draws user to its own computing environment by showing prompt following theformat: your_loginID $. Figure 1 shows how the prompt looks when the logID is jwlee.When a command is correctly executed, output will be printed out immediately. The logIDwill be printed by using printf function.jwlee@altair~ > cc lab1-jwlee.c -o lab1jwlee@altair~ > ./lab1jwlee $ exot[Error] Please enter the command correctly!jwlee $ encrypt(“hello”)ebiiljwlee $ exitjwlee@altair~ >B. Caesar’s Cipher: encrypt/decryptCaesar’s cipher is one of cryptography methods, which has a long history, back to JuliusCaesar’s Roman Empire. He used this simple yet efficient method in sending and receivingprivate messages with his subordinates and political comrades. The method is based on asubstitution cipher technique in which substitute an alphabet letter to a different alphabet letterby a certain rule. Look at the list of alphabets in the Figure 2. In this plaintext eachcharacter has a certain positional number, for example “A” has 1, meaning the first character.In the same manner, “B” has 2, “C” has 3, and so on. To get the ciphertext, all positionalnumber is shifted right by a proposed number, 3 in the Figure 2. By the shifting rule, “A”is now in the 4th position, “B” is in the 5th position.The shifting rule has issues when it shifts alphabets at the end. See Y and Z. After shifting 3,Y which was in 25th position should move to 28th position, but it is out of range of alphabet.Caesar solved it by modular operation, which means the positional number after shifting ismodularized by the size of alphabets. For simplicity, let say A is in 0, B is in 1, and so on.Then the encryption formula E to get cipher text C from plain text P is below:C = E(k, P) = (P + k) (mod 26)In the formula k stands for the number of shifts and k is 3 in our example. Similarly, a ciphertext can be decrypted by the following formula D:P = D(k, C) = (C – k) (mod 26)A cipher message can be decrypted by person who knows k. It makes the method secure. Forexample, the following sentence is encrypted to a cipher message, which will be sent:Actual message: SEE ME AFTER CLASSSending message: VHH PH DIWHU FODVVBecause you are given k above, you can understand the sending message, VHH PH DIWHUFODVV and will see me after class. But who don’t know it cannot see me, right?However, Caesar’s Cipher covers only 26 alphabet letters. Numbers cannot be encrypted. Inthis program, thus you extend it to cover characters in ASCII code1. Each ASCII character isencoded to an 8-bit number (7 bits are used in the traditional code). For example, “A” is 65,“B” is 66, the number “0” is 48, “1” is 49. Lowercases are also differentiated, for example,“a” is 97, “b” is 98. Moreover, it covers other special characters such as “!”, “+”, and so on.Because it encrypts more than 26 characters, it definitely enhances the secure level!plaintextciphertextIn your program, you will encrypt a plaintext to get a ciphertext when k is 3. And you willdecrypt a given ciphertext to get a plaintext. Your program will run as shown in Figure 3.Note that it must follow the correct syntax below:encrypt(plaintext)decrypt(ciphertext)Any typos in entering the command should be caught and returns an error message. It includesthe format of command.jwlee $ encrypt(I have a key)L#kdyh#d#nh|jwlee $ encrypt(see me at 3)vhh#ph#dw#6jwlee $ decrpyt(qr#l#fdq#phhw#dw#<)no i can meet at 9jwlee $ encr(see you soon)[Error] Please enter the command correctly!jwlee $ decrypt “rndb”[Error] Please enter the command correctly!jwlee $Figure 3. encrypt/decrypt exampleAfter the output, the system must print prompt so that user enters a next command. Are youready? JRRG OXFN!!C. [Extra work] Encoding/Decoding: encode/decodeIt is not mandatory but optional, which means those who develop this command will earnextra credits up to 6 points per command. The commands are executed when you want toencode a text to a binary code and decode a binary code to a readable text. The encoding anddecoding are commonly used in editing video or audio data. The idea is to convert data (ofimage, video, audio, etc.) to a format which can be read and managed by machine. Since themachine is a passive device which can use two characters - 0 and 1, the format is usuallywritten in the binary code. There is variation for the format, but you implement a simpleencoder/decoder, which convert a character into its ASCII code’s binary code. You can usethe ASCII code in the last page. See the example of encode and decode commands in Figure4. The text “key” has three ASCII codes of 107, 101, and 121. The encoder converts them intoa binary number (01101011, 01100101, 01111001) and concatenate them. Note that thetraditional ASCII code has 7 bits code, but you will consider 8-bit code which enables 8 bitsUnicode. Thus, after convert 107 to 1101011, and make it 01101011 by putting 0 in thebeginning.jwlee $ encode(key)011010110110010101111001jwlee $ encode(7-eleven)0011011100101101011001010110110001100101011101100110010101101110jwlee $ encod(location)[Error] Please enter the command correctly!jwlee $ decode “011100”[Error] Please enter the command correctly!jwlee $ decode(here)[Error] Please enter the command correctly!jwlee $The commands have the following syntax:encode(text)decode(binary code)You should check misspelled commands and incorrect syntax. And you should check binarycode input for decode command, since it decodes “binary code” to a text. Look at the last caseof decode(here) in the Error! Reference source not found.. It is incorrect, because “here” isnot a binary code. After the output, the system must print prompt so that user enters a nextcommand.Each command is worth 6 points, thus those who develop both commands will get 12 points.3 Hints in ImplementationA. Reading user inputThere are several ways to get your input: scanf, sscanf, gets, fgets, and so on. Butthe best way to handle exceptions such as non-numerical inputs and excess of inputs is to get astring and parse it into required forms. For needs, the good choice is fgets but not limited touse oth er functions in this lab. Find full descriptions of the functions using man.B. Parsing user inputOnce user enters a command, the system must parse it to recognize the type of command andinput. Tokenizer enables to parse commands and return the information needed for furtherprocessing. String library provides necessary functions such as strtok. Find fulldescriptions of the functions using man.4 RequirementsA. Developing environmentThe program should be implemented in C only. The program in another language will not begraded. And the program should be executable in CS Linux machine. The program whichcannot be compiled or executed in the CS Linux machine will be considered as incompleteprogram and will lose most points.B. Handling exceptionsThe program must detect errors while running. The errors may occur when user violates thesyntax of the command or enters incorrect inputs (characters, more than required inputs, orsee more in the section II). When the program detects any error cases, it should handleproperly by giving some error messages to the user, and still be runnable. The program,which fails to detect errors and to properly handle them, will lose points.C. Readability of source codeThe source code should be easy to read with good indentation and proper amount of comments.D. Creating a readme fileThe program should be submitted along with a readme file which has information such asyour ID, name, etc. Students who work on the extra work should notify to instructor throughthe readme file. It also may provide a special instruction to run this program if exists. Thesource file and readme file should be named following the format below. Those who are notfollowing the naming rule will also be taken off penalty points.lab1-your_loginID.clab1-your_loginID_readme.txtE. Submitting by dueThe program should be submitted to Blackboard within the two weeks after the project isposted. Due is September 23rd. All submission by 11:59 PM on that day will be acceptedwithout any penalty. On the due date, Blackboard may be suffering of too much networktraffics and be unstable. There is no excuse about the issue, therefore you are stronglyrecommended to submit earlier than the due date.5 GradingA. Grading criteriaThe lab is assigned 30 points, which is 15% of the final grade. It will be graded byevaluating the requirement. Any missing and unsatisfiable criteria will take off points. Thetentative and brief criteria are below.• Compilation: 5 points• Execution: 20 points• Error detection & others: 5 pointsThe extra work will give you up to 12 bonus points, which are worth of 40%.B. Late penaltyLate submission will take off 10% per day per week after due date. Thus, submissionafter 10 weeks will not be accepted in any circumstances.6 Academic IntegrityAny dishonest behaviors will not be tolerated in this class. Any form of plagiarism andcheating will be dealt with according to the guidelines on the Academic Integrity Policy on-line at http://www.oswego.edu/integrity. For more information about university policies,see the following online catalog at:http://catalog.oswego.edu/content.php?catoid=2&navoid=47#stat_inte_inteStudent who is against the honor code will not have any credits in this project.1 ASCII code with binary (not covering UNICODE)  

admin

Author admin

More posts by admin