2 shunfurh shunfurh 于 2017.09.08 17:36 提问

Delta Encoding and Decoding

A very simple encryption scheme which represents a big improvement over a substitution cipher is a delta cipher. Here's how it works: The letters A through Z are given a numerical values starting with 0 and ending with 25. These values aren't necessarily given in that order, but the mapping is one to one. (The trivial mapping where A has value 0, B has value 1, etc. is the default cipher. It's what you use when you don't have anything better.)
In order to encrypt a message, the differences between successive values are used.

  1. All characters except letters are transferred to the encrypted message unchanged.

  2. When one letter is preceeded by something other than a letter, it is treated as though it had been preceeded by an A.

  3. Case both does and does not matter in this encryption scheme. If a letter is originally uppercase, the encrypted version should be uppercase. If it is lowercase, the encrypted version should be lowercase. However, A and a are the same letter.

  4. When one letter is preceeded by another, the difference between the two values is the value of the first is subtracted from the second. The letter whose value is equivalent to this difference modulo 26 will replace the second letter in the new text.

The input is merely a sequence of a commands, one per line, and is terminated by an EOF. No line contains more than 10000 characters. There are three command words: ENCRYPT, DECRYPT, and CIPHER. The case of the letters in these words is not important, and each word will be followed by at least one space. Any line which begins with any word other than one of these three commands requires the response "Command not understood."

The ENCRYPT and DECRYPT commands are self-explanatory. The encrypt command is followed by plain text which needs to be encrypted and the decrypt command is followed by encrypted text which must be put into plain text. Whatever follows one of these two command words should be either encrypted or decrypted using the current cipher. If no cipher has yet been given, you should use the default cipher.
The CIPHER command should be followed by the twenty-six letters of the alphabet. The order of these letters is important and that order determines the value for each character - the first letter has value 0; the second letter has value 1; etc. There may be spaces and punctuation; these should be ignored; however, this line must have exactly twenty-six letters on it with no repetitions. Any problem requires the response, "Bad cipher. Using default." (And, as indicated, you should actually use the default). If the cipher is understandable, you need to echo it back to the user as shown below, and then use it for all subsequent commands.

Sample Input

ENCRYPT The quick brown fox leaped over the lazy dog.
CIPHER qwertyuiopasdfghjklzxcvbnm
ENCRYPT The quick brown fox leaped over the lazy dog.
DECRYPT Xsf juwgv fuyzb rcd oaomza nguw xsf olpd evu.
ENCRIPT This won't work!
CIPHER qAZwSXedCRfv tgbyhnujmikolp
ENCRYPT The quick brown fox leaped over the lazy dog.
DECRYPT Vsh puwgb gibeb rgc ocilda kbiw vsh owag ehh.
CIPHER qwertyuopasdfghjklzxcvbnm
ENCRYPT The quick brown fox leaped over the lazy dog.
DECRYPT Tox qeoui bqxir fjj ltwppz ohjn tox lpzz dls.

Sample Output

RESULT: Tox qeoui bqxir fjj ltwppz ohjn tox lpzz dls.
Good cipher. Using QWERTYUIOPASDFGHJKLZXCVBNM.
RESULT: Xsf juwgv fuyzb rcd oaomza nguw xsf olpd evu.
RESULT: The quick brown fox leaped over the lazy dog.
Command not understood.
Good cipher. Using QAZWSXEDCRFVTGBYHNUJMIKOLP.
RESULT: Vsh puwgb gibeb rgc ocilda kbiw vsh owag ehh.
RESULT: The quick brown fox leaped over the lazy dog.
Bad cipher. Using default.
RESULT: Tox qeoui bqxir fjj ltwppz ohjn tox lpzz dls.
RESULT: The quick brown fox leaped over the lazy dog.

1个回答

caozhy
caozhy   Ds   Rxr 2017.09.24 08:32
已采纳
Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!
其他相关推荐
字符串编码(charset,encoding/decoding)问题原理
字符串编码(charset, encoding/decoding)问题原理 编码问题很重要,关于编码问题的文章也很多。我本来没有兴趣重复这个主题。 一个朋友问我有没有比较好的编码介绍文章。我记得以前看过几篇很不错的,但是当时搜索到的时候,就比较费劲,一时想不起来。于是,我就说,我攒一篇
delta coding入门
增量编码(delta coding)通过维护增量的方式,保存数据,这样能够达到更好的压缩比,对于保存连续的或者数据段在一个稳定范围内出现的场景,效果更好。 下面是我用go代码简单实现package mainimport ( "fmt" )func main() { buf := []int{1,2,3,5,5,6} delta_encode(buf) fmt.Pri
json Encoding/Decoding Examples
json Encoding/Decoding Examples
pudub 异常分析 Decoding failed. ffmpeg returned error code: 1
今天测试pudub这个库 怎么运行都出错,出现如下错误!raise CouldntDecodeError("Decoding failed. ffmpeg returned error code: {0}\n\nOutput from ffmpeg/avlib:\n\n{1}".format(p.returncode, p_err))pydub.exceptions.CouldntDecodeEr...
c#中Encoding的不同编码方式的区别
最近了解了一下C#中Encoding的不同编码方式的区别,和大家分享一下,如果有不对的地方还请各位批评指教。   简单的说,为什么需要编码? 比如,我们的计算机中需要表示字母'a','b'等等字母,然而这些字母如何在计算机内存中表示?众所周知,在计算机内存中数据是以二进制来表示的,这样,我们就需要将这些需要表示的字母和数字或者符号转换成能在计算机中表示的二进制表示,这就是编码的意义所在。
用Python实现URL Encoding和Decoding
前些日子在一个论坛上看到网友拿03版《天龙八部》和13版《天龙八部》作对比。在比较两个版本的片尾曲的时候,提到了03版的片尾曲《宽恕》。帖子中提到,这首歌由王菲演唱、林夕作词、赵季平(电视剧《关西无极刀》片头曲的作者)作曲。 记得高中时看《天龙》的时候就挺喜欢这首歌的,于是就用火狐浏览器下载了这首歌。但是下载得到的文件名是:%E5%AE%BD%E6%81%95.mp3。用浏览器在网上下载包含
51 java URLEncoding URLDecoding
import java.net.URLDecoder; import java.net.URLEncoder; public class URLDecoderTest { public static void main(String[] args) throws Exception { // TODO Auto-generated method stub String keywor
java URL encoding and decoding
HTML编码规则是:  字符"a"-"z","A"-"Z","0"-"9",".","-","*",和"_" 都不被编码,维持原值,  空格" "被转换为加号"+"。  所有其他的字符都被认为是不安全的,首先都根据指定的编码scheme被转换为1个或者多个字节  然后每个字节都被表示成"%xy"格式的由3个字符组成的字符串,xy是字节的2位16进制的表达,推荐的编码scheme为UTF-8
GSM SMS basic introduction
This document includes basic introduction for SMS decoding/encoding
encoding / decoding
<br />1.  Players and how they support encoding / decoding<br />http://www.matroska.org/technical/guides/playback/windows/index.html