crytopals(一)

crytopals(一)

前言

本篇是我第一周参加网络安全的培训所记录的学习笔记,内容有关python基础学习和cryptopals上第一道题

一、Python基础

1.输出字符串

print(520)
print(3+1)
#输出字符串
print('hello world')
print("hello world")
#将数据输出文件中,注意点:1、所指定的盘符存在;2.使用file=fp
fp=open('D:/text.txt','a+')#a+ 如果文件不存在就创建,存在就在文件内容的后面追加
print('hello world',file=fp)
fp.close()
#不进行换行输出
print('hello','world','Python')

2.转义字符

#转义字符
print('hello\nworld')#\ +转义功能的首字母   n-->newline的首字母表示换行
print('hello\tworld')
print('helloooo\tworld')#t 水平制表符 四格为一个制表位
print('hello\rworld')#r world将hello进行了覆盖
print('hello\bworld')#b 是退一格,将o退没了

print('http:\\\\www.baidu.com')
print('老师说:\'大家好\'')

#原字符,不希望字符串中的转义字符起作用,就使用原字符,就是在字符串之前加上r或R
print(r'hello\nworld')
#注意事项:最后一个字符不能是一个反斜杠
#print(r'hello\nworld\')
print(r'hello\nworld\\')

3.保留字

#保留字
import keyword
print(keyword.kwlist)

二、crytopals(一)第一题

1.crytopals地址

2.题目要求将十六进制转为base64

首先要了解base64是什么,可以参考什么是base64

3.简单了解base64的编码原理

1.按ASCII码编码.

2.将字符串按字节数分开,每三个字节为一组. 3 byte * 8 bit === 24bit.

3.在这组中,将24位数据按照每6位为一组,再次分成4组.

4.然后将这四组的6位数的高位各补两个0,将其转为十进制数.

5.进行查表,得到对应的字符,就是对应的Base64转化的字符.

4.python代码实现

from enum import Enum
b64_encoding_table="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"
class Status(Enum):
    START_NEW=0
    TAKE_2=1
    TAKE_4=2
def hex_to_base64(hexdata):
    b64data=""
    sixbits=0
    status=Status.START_NEW
    for hexchar in hexdata:
        dec=int(hexchar,16)
        if status==Status.START_NEW:
            sixbits=dec
            status=Status.TAKE_2
        elif status==Status.TAKE_2:
            sixbits=(sixbits<<2)|(dec>>2)
            b64data+=b64_encoding_table[sixbits]
            sixbits=(dec&0x3)
            status=Status.TAKE_4
        elif status==Status.TAKE_4:
            sixbits=(sixbits<<4)|dec
            b64data+=b64_encoding_table[sixbits]
            status=Status.START_NEW
    if status==Status.TAKE_2:
        sicbits<<=2
        b64data += b64_encoding_table[sixbits]
        b64data+="="
    elif status==Status.TAKE_4:
        sicbits<<=4
        b64data += b64_encoding_table[sixbits]
        b64data+="=="
    return b64data
def main():
    print(hex_to_base64("49276d206b696c6c696e6720796f757220627261696e206c696b65206120706f69736f6e6f7573206d757368726f6f6d"))
if __name__=='__main__':
    main()

三、总结

由于是刚接触加密,还有许多地方不熟练,后面要抓紧学习,对python的了解还不深,最后的代码也是通过查找一步一步所实现的。

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值