基础知识

我们用一个提取字符串内单词首字母的程序先热热身,比如输入:

程序把缩写OMG输出来。

# ---------- PROBLEM : ACRONYM GENERATOR ----------
# You will enter a string and then convert it to an acronym
# with uppercase letters like this
'''
Convert to Acronym : Random Access Memory
RAM
'''

# Ask for a string
str1 = input("Convert to Acronym : ")

# Convert the string to all uppercase
str1 = str1.upper()

# Covert the string into a list
listOfWords = str1.split()
print(listOfWords)
# Cycle through the list
for word in listOfWords:

	# Print the 1st letter of the word
	print(word[0], end='')


第二个程序是为了解决小甲鱼的烦恼,事情是这样哒:

小甲鱼好不容易鼓起勇气给仰慕已久的女神发了条表白信息,

最添彩那句就是“我的爱意有如长江之水滔滔不绝,如黄河泛滥一发不可收拾“,

不曾想却被心怀叵测、唯一戏份就是搞破坏滴第三者把这条信息给拦截下来了,不仅进行了可耻滴偷窥还卑鄙滴把”爱“字篡改成了”恨“字。

收到信的美眉很生气


后果很严重!小甲鱼百般解释千般赔罪,女神才答应不把他拉黑,这样的事说啥也不能重演啦!!!

内个,这个桥段是不是看着眼熟,不用回忆啦,某类言情剧或小说里随便一抓哒~~~

痛定思痛的小甲鱼决定委托我们写一个字符串加密程序,这样传输过程中信息就算被偷看被拦截也不知所谓,更谈不上做篡改。

加密方法是每个字符变成自己的unicode或跟unicode有关的数字串密文,密文到达目的地后还得要解密,绝不能惊扰美眉对仰慕者情书“随便一瞄”“淡淡一笑”再优雅滴“随手一放”的姿态。

# ---------- PROBLEM : SECRET STRING ----------

# Input string to be encrypted and initiate values
orig_message = input("Enter a string: ")

# initiate value for encrypted string
secret_message = ''

# cycle through each character in the string
for char in orig_message:

	tempCode = str(ord(char) - 27)
	if len(tempCode) == 1:
		tempCode = '0' + tempCode

	# store each character code in secret message
	secret_message += tempCode

# Print encrypted message
print("Secret message: ", secret_message)

# Cycle through two character code at a time
norm_string = ""

for i in range(0, len(secret_message)-1, 2):

	# Convert the code into characters and add them to the string
	char_code = int(secret_message[i] + secret_message[i+1])

	norm_string += chr(char_code + 27)

# Print the string
print("Original message : ", norm_string)

 

凯撒加密是一种简单且广为人知的加密技术。它把明文中的所有字母都在字母表上向后(或向前)按照一个固定数目进行偏移,而后被替换成密文。

传说在战场上凯撒用这种密文来跟他的将军们进行通信,“Attack at Dawn!"的密文根据偏移量不同而不同,偏移量为3时,加密结果是“Dwwdfn dw Gdzq”。

这样,就算密信被混入军团里的奸细窃取到,不知道偏移量他跟看天书没区别。


但那是当时,现在这样可不中。

现在都是把凯撒加密和其他更复杂的加密方法结合起来使用,不然一会儿就会被破解哒,虽然我们就是写了一个凯撒加密解密字符串的程序。

# ---------- PROBLEM : CAESAR'S CIPHER ----------
# Receive a message and then encrypt it by shifting the
# characters by a requested amount to the right
# A becomes D, B becomes E for example
# Also decrypt the message back again


# Receive the message to encrypt and the number of characters to shift
message = input("Enter your message: ")
key = int(input("How many characters should we shift(-26 ~ 26) "))

# Prepare your secret message
secret_message = ''

# Cycle through each character in the message
for char in message:

	# if it is a letter
	if char.isalpha():

		# Get unicode and add the shift amount
		char_code = ord(char) + key

		# if uppercase
		if char.isupper():

			# if greater than 'Z'
			if char_code > ord('Z'):
				char_code -= 26

			# if less than 'A'
			if char_code < ord('A'):
				char_code += 26

		# if lowercase
		if char.islower():

			# if greater than 'z'
			if char_code > ord('z'):
				char_code -= 26

			# if less than 'a'
			if char_code < ord('a'):
					char_code += 26

		# Convert from code to letter and add to message
		secret_message += chr(char_code)

	# if not a letter leave the character as is
	else:
		secret_message += char

# print the encrypted message
print("Encrypted message: ", secret_message)

# To decrypt the only thing that changes is the sign of the key
key = -key

orig_message = ""

for char in secret_message:

	# if it is a letter
	if char.isalpha():

		# Get unicode and add the shift amount
		char_code = ord(char) + key

		# if uppercase
		if char.isupper():

			# if greater than 'Z'
			if char_code > ord('Z'):
				char_code -= 26

			# if less than 'A'
			if char_code < ord('A'):
				char_code += 26

		# if lowercase
		if char.islower():

			# if greater than 'z'
			if char_code > ord('z'):
				char_code -= 26

			# if less than 'a'
			if char_code < ord('a'):
				char_code += 26

		# Convert from code to letter and add to message
		orig_message += chr(char_code)

	# if not a letter leave the character as is
	else:
		orig_message += char

print("Decrypted message: ", orig_message)



详情请见视频:
 

1 1 1 1 1 1 1 1 1 1 Rating 2.63 (4 Votes)