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,