收藏 分享(赏)

PythonContextCH03.pptx

上传人:weiwoduzun 文档编号:4301425 上传时间:2018-12-21 格式:PPTX 页数:29 大小:231.27KB
下载 相关 举报
PythonContextCH03.pptx_第1页
第1页 / 共29页
PythonContextCH03.pptx_第2页
第2页 / 共29页
PythonContextCH03.pptx_第3页
第3页 / 共29页
PythonContextCH03.pptx_第4页
第4页 / 共29页
PythonContextCH03.pptx_第5页
第5页 / 共29页
点击查看更多>>
资源描述

1、Python Programming in Context,Chapter 3,Objectives,To introduce the string data type To demonstrate the use of string methods and operators To introduce simple cryptographic algorithms,String,A sequence of characters Quote delimited single “ double triple,Figure 3.1,Operators,Concatenation + Repetit

2、ion * Indexing Slicing : ,String Methods,upper lower center count index find replace,Character Functions,ord char Convert between characters and numbers,Listing 3.1,def letterToIndex(ch):alphabet = “abcdefghijklmnopqrstuvwxyz “idx = alphabet.find(ch)if idx 25:print (error: , idx, is too large)letter

3、 = elif idx 0:print (error: , idx, is less than 0)letter = else:letter = alphabetidxreturn letter,Cryptography,Encoding and Decoding Messages Ciphertext Plaintext Encryption Decryption,Figure 3.2,Transposition Cipher,Rail Fence Even Odd Shuffle,Figure 3.3,Listing 3.2,def scramble2Encrypt(plainText):

4、evenChars = “oddChars = “charCount = 0for ch in plainText: if charCount % 2 = 0: evenChars = evenChars + chelse:oddChars = oddChars + chcharCount = charCount + 1cipherText = oddChars + evenCharsreturn cipherText,Figure 3.4,Listing 3.3,def scramble2Decrypt(cipherText):halfLength = len(cipherText) / 2

5、oddChars = cipherText:halfLength evenChars = cipherTexthalfLength: plainText = “for i in range(halfLength): plainText = plainText + evenCharsiplainText = plainText + oddCharsiif len(oddChars) len(evenChars):plainText = plainText + evenChars-1return plainText,Listing 3.4,def encryptMessage():msg = in

6、put(Enter a message to encrypt: )cipherText = scramble2Encrypt(msg)print(The encrypted message is: , cipherText),Substitution Cipher,Requires a key Substitute one letter for another throughout the entire message,Figure 3.5,Figure 3.6,Listing 3.5,def substitutionEncrypt(plainText,key):alphabet = “abc

7、defghijklmnopqrstuvwxyz “plainText = plainText.lower()cipherText = “for ch in plainText:idx = alphabet.find(ch)cipherText = cipherText + keyidxreturn cipherText,Creating a Key,Random reordering of the alphabet Base key on short word or phrase,Listing 3.6,def removeChar(string,idx):return string:idx

8、+ stringidx+1:,Listing 3.7,def keyGen():alphabet = “abcdefghijklmnopqrstuvwxyz“key = “for i in range(len(alphabet):ch = random.randint(0,25-i)key = key + alphabetchalphabet = removeChar(alphabet,ch)return key,Listing 3.8,def removeDupes(myString):newStr = “for ch in myString:if ch not in newStr:newS

9、tr = newStr + chreturn newStr,Listing 3.9,def removeMatches(myString,removeString):newStr = “for ch in myString:if ch not in removeString:newStr = newStr + chreturn newStr,Listing 3.10,def genKeyFromPass(password):key = abcdefghijklmnopqrstuvwxyzpassword = removeDupes(password)lastChar = password-1l

10、astIdx = key.find(lastChar)afterString = removeMatches(keylastIdx+1:,password)beforeString = removeMatches(key:lastIdx,password)key = password + afterString + beforeStringreturn key,Vignere Cipher,Use a different key for each letter Vignere Square Use a special key for shift,Listing 3.11,def vignere

11、Index(keyLetter,plainTextLetter):keyIndex = letterToIndex(keyLetter)ptIndex = letterToIndex(plainTextLetter)newIdx = (ptIndex + keyIndex) % 26return indexToLetter(newIdx),Listing 3.12,def encryptVignere(key,plainText):cipherText = “keyLen = len(key)charNum = 0for i in range(len(plainText):ch = plainTextiif ch = :cipherText = cipherText + chelse:cipherText = cipherText + vignereIndex(keyi%keyLen,ch)return cipherText,

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 网络科技 > Python

本站链接:文库   一言   我酷   合作


客服QQ:2549714901微博号:道客多多官方知乎号:道客多多

经营许可证编号: 粤ICP备2021046453号世界地图

道客多多©版权所有2020-2025营业执照举报