Base64是网络上最常见的用于传输8Bit字节码的编码方式之一,它是一种基于64个可打印字符来表示二进制数据的方法,可用于在HTTP环境下传递较长的标识信息。
Base64要求把每三个8Bit的字节转换为四个6Bit的字节(3*8 = 4*6 = 24),然后把6Bit再添两位高位0,组成四个8Bit的字节,也就是说,转换后的字符串理论上将要比原来的长1/3。
1.Convert hex to base64 (编程将十六进制的字符串转换成base64编码):
编程将如下的十六进制字符串:
49276d206b696c6c696e6720796f757220627261696e206c696b65206120706f69736f6e6f7573206d757368726f6f6d
转换成如下的base64编码的字符串:
SSdtIGtpbGxpbmcgeW91ciBicmFpbiBsaWtlIGEgcG9pc29ub3VzIG11c2hyb29t
注意:之后的实验会用到本题的程序。
2.Fixed XOR (将两个等长的十六进制字符串进行异或)。
编程实现两个等长的十六进制字符串的异或,如果你的程序能正确运行,就可以将如下的前面两个字符串异或成第三个字符串:
1c0111001f010100061a024b53535009181c
... after hex decoding, and when XOR'd against:
686974207468652062756c6c277320657965
... should produce:
746865206b696420646f6e277420706c6179
3.Single-byte XOR cipher (一个字符串被单字符加密,请找出该单字符)
如下是一个被单字符异或后得到的十六进制的字符串,请找出该单字符(即密钥),并解密该密文。
1b37373331363f78151b7f2b783431333d78397828372d363c78373e783a393b3736
提示:利用英语字母频率,穷举法。
4.如下密文中包含一个含60个字符的字符串是利用单字符异或加密得到的,请找出该字符串。
print('<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<第一题>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>')
import base64
def hex_base64_encode(hex_string):
change_string=''
for i in range(len(hex_string)):
if i%2==1:
change_string=change_string+chr( int(hex_string[i-1],16)*16+int(hex_string[i],16) )
print('base64加密后为:', str(base64.b64encode(change_string.encode('utf-8')),'utf-8'))
def hex_text(hex_string):
change_string=''
for i in range(len(hex_string)):
if i%2==1:
change_string=change_string+chr( int(hex_string[i-1],16)*16+int(hex_string[i],16) )
print('hex变string为:', change_string)
hex_string='49276d206b696c6c696e6720796f757220627261696e206c696b65206120706f69736f6e6f7573206d757368726f6f6d'
hex_base64_encode(hex_string)
print('\n\n<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<第二题>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>')
'''
def hex_bin(hex_num):
init_hex_bin=bin(int(hex_num,16))
if (len(init_hex_bin)-2)%4!=0:
init_hex_bin=(4-(len(init_hex_bin)-2)*0+init_hex_bin[1:]
return init_hex_bin
def hex_bin(num):
hex_bin_list={'0':'0000', '1':'0001','2':'0010', '3':'0011', '4':'0100', '5':'0101','6':'0110', '7':'0007',
'8':'1000','9':'1001', 'a':'1010', 'b':'1011', 'c':'1100', 'd':'1101', 'e':'1110', 'f':'1111'}
return 0
'''
hex_string1='1c0111001f010100061a024b53535009181c'
hex_string2='686974207468652062756c6c277320657965'
hex_string3=''
for i in range(len(hex_string1)):
hex_string3=hex_string3+hex(int(hex_string1[i],16)^int(hex_string2[i],16))
hex_string3=hex_string3.split('0x')
hex_string3=''.join(hex_string3)
print('异或后的字符串为:',hex_string3)
# -*- coding: utf-8 -*-
def hex_string(hex_string):
change_string=''
for i in range(len(hex_string)):
if i%2==1:
change_string=change_string+chr( int(hex_string[i-1],16)*16+int(hex_string[i],16) )
return change_string
CHARACTER_FREQ = {
'a': 0.0651738, 'b': 0.0124248, 'c': 0.0217339, 'd': 0.0349835, 'e': 0.1041442, 'f': 0.0197881, 'g': 0.0158610,
'h': 0.0492888, 'i': 0.0558094, 'j': 0.0009033, 'k': 0.0050529, 'l': 0.0331490, 'm': 0.0202124, 'n': 0.0564513,
'o': 0.0596302, 'p': 0.0137645, 'q': 0.0008606, 'r': 0.0497563, 's': 0.0515760, 't': 0.0729357, 'u': 0.0225134,
'v': 0.0082903, 'w': 0.0171272, 'x': 0.0013692, 'y': 0.0145984, 'z': 0.0007836, ' ': 0.1918182
}
def get_score(string):
score=0
for char in string:
char=char.lower()
if char in CHARACTER_FREQ:
score+=CHARACTER_FREQ[char]
return score
def single_xor(candidate_key,hex_string):
result=""
for i in hex_string:
b=chr(candidate_key^ord(i))
result+=b
return result
def Traversal_singlechar(hex_string):
candidate=[]
for candidate_key in range(256):
plaintext=single_xor(candidate_key,hex_string)
score=get_score(plaintext)
result={
'score':score,
'plaintext':plaintext,
'key':candidate_key
}
candidate.append(result)
return sorted(candidate,key=lambda c:c['score'])[-1]
def main():
#hex_string1=raw_input("input hex1:\n").decode('hex')
string='1b37373331363f78151b7f2b783431333d78397828372d363c78373e783a393b3736'
hex_string1=hex_string(string)
result=Traversal_singlechar(hex_string1)['plaintext']
key=chr(Traversal_singlechar(hex_string1)['key'])
print('<<<<<<<<<<<<<<<<<<<<<<<<<<<base64第3题>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n')
print ('密钥为:',key)
print ('解密结果为',result)
if __name__ == '__main__':
main()
#coding=utf-8
import re
wenben=[]
for i in open("4.txt","r").readlines():
wenben+=[i.replace("\n","")] #序列相加
score=0
for k in wenben:
for i in range(0,129):
tmp=[]
for j in re.findall(".{2}",k):#任意两个字符的字符串
tmp += chr(i^int(j,16))
tmpstr = "".join(tmp)
num=0
num=len(re.findall(r'[a-zA-Z ]',tmpstr))#一定注意不要落下空格
if num>score:
score=num#用于更新用
ansstr=tmpstr
c=k
key=chr(i)
print("<<<<<<<<<<<<<<<<<<base64第4题>>>>>>>>>>>>>>>>>>>>>>>>\n")
print ('hex字符串为:',c)
print ('密钥为:',key)
print ('解密的结果为',ansstr)