攻防世界-Crypto-新手区

base64

base64编码之后的字符串具有的特点:

*字符串只可能包含A-Z,a-z,0-9,+,/,=字符

*字符串长度是4的倍数

* =只会出现在字符串最后,可能没有或者一个等号或者两个等号

工具https://www.sojson.com/base64.html

python代码:(注意文件不要命名为base64.py,否则import base64错误)

import base64

cipher = "Y3liZXJwZWFjZXtXZWxjb21lX3RvX25ld19Xb3JsZCF9"

plaintext = base64.b64decode(cipher)

print(plaintext)


Caesar

工具https://www.qqxiuzi.cn/bianma/kaisamima.php

python代码

def caesar(cipher):

        for j in range(26):

                str_list = list(cipher)

                i = 0

                while i < len(cipher):

                        if not str_list[i].isalpha():

                                str_list[i] = str_list[i]

                        else:

                                 a = "A" if str_list[i].isupper() else "a"

                                str_list[i] = chr((ord(str_list[i]) - ord(a) + j) % 26 + ord(a))

                        i = i + 1

                        print(''.join(str_list))

if __name__ == '__main__':

        cipher = "oknqdbqmoq{kag_tmhq_xqmdzqp_omqemd_qzodkbfuaz}"

        caesar(cipher)


Morse

特征:

* 用点(.)和划(-)来编码范围0-9、A-Z的字符,字母不区分大小写

* 两个字母之间的空格用斜杠(/)或者三个点(.)或者一个划(-)表示,两个单词之间的间隔是七个点(.)

* 根据摩斯编码的原理,CTF中也有出现过变种的摩斯编码,比如点(.)和划(-)用数字0和1来表示等此类变种的思路

工具http://ctf.ssleye.com/morse.html

python代码

table ={'a': ".-", 'b': "-...", 'c': "-.-.", 'd': "-..", 'e': ".", 'f': "..-.", 'g': "--.", 'h': "....", 'i': "..", 'j': ".---", 'k': "-.-", 'l': ".-..", 'm': "--", 'n': "-.", 'o': "---", 'p': ".--.", 'q': "--.-", 'r': ".-.", 's': "...", 't': "-", 'u': "..-", 'v': "...-", 'w': ".--", 'x': "-..-", 'y': "-.--", 'z': "--..", '0': '-----', '1': '.----', '2': '..---', '3': '...--', '4': '....-', '5': '.....', '6': '-....', '7': '--...', '8': '---..', '9': '----.', ',': '--..--', '.': '.-.-.-', ':': '---...', ';': '-.-.-.', '?': '..--..', '=': '-...-', "'": '.----.', '/': '-..-.', '!': '-.-.--', '-': '-....-', '_': '..--.-', '(': '-.--.', ')': '-.--.-', '$': '...-..-', '&': '. . . .', '@': '.--.-.'}

def morse(cipher):

        msg = ''

        codes = cipher.split(' ')

        for code in codes:

                if code == '':

                        msg += ' '

                else:

                        UNCODE = dict(map(lambda t: (t[1], t[0]), table.items()))

                        msg += UNCODE[code]

                return msg

if __name__ == '__main__':

        cipher ="11 111 010 000 0 1010 111 100 0 00 000 000 111 00 10 1 0 010 0 000 1 00 10 110"

        cipher = cipher.replace('1', '-')

        cipher = cipher.replace('0', '.')

        plaintext = morse(cipher)

        print(plaintext)


混合编码

步骤:①base64     ②unicode     ③base64     ④unicode(要将/改为&#)


Railfence-----栅栏密码

工具http://www.atoolbox.net/Tool.php?Id=777


不仅仅是Morse

步骤

①Morse编码(python代码中要把“cipher = cipher.replace('1', '-')   cipher = cipher.replace('0', '.')”改为“cipher=cipher.repalce('/',' ')”     

培根密码(倍康尼密码、Bacon's cipher):仅仅由ab构成;求解方式如下所示

工具https://tool.bugku.com/peigen/

python代码

import re

table ={'a': 'aaaaa', 'b': 'aaaab', 'c': 'aaaba', 'd': 'aaabb', 'e': 'aabaa', 'f': 'aabab', 'g': 'aabba', 'h': 'aabbb', 'i': 'abaaa', 'j': 'abaab', 'k': 'ababa', 'l': 'ababb', 'm': 'abbaa', 'n': 'abbab', 'o': 'abbba', 'p': 'abbbb', 'q': 'baaaa', 'r': 'baaab', 's': 'baaba', 't': 'baabb', 'u': 'babaa', 'v': 'babab', 'w': 'babba', 'x': 'babbb', 'y': 'bbaaa', 'z': 'bbaab'}

def bacon(cipher):

        msg = ''

        codes = re.findall(r'.{5}', cipher)

        for code in codes:

                if code == '':

                        msg += ' '

                else:

                        UNCODE = dict(map(lambda t: (t[1], t[0]), table.items()))

                        msg += UNCODE[code]

        return msg

if __name__ == '__main__':

        cipher = 'aaaaabaabbbaabbaaaaaaaabaababaaaaaaabbabaaabbaaabbaabaaaababaabaaabbabaaabaaabaababbaabbbabaaabababbaaabbabaaabaabaabaaaabbabbaabbaabaabaaabaabaabaababaabbabaaaabbabaabba'

        plaintext = bacon(cipher)

        print(plaintext)


幂数加密/云影密码

python代码

#二进制幂数加密/云影密码

a=str(input("密文:"))

a=a.split("0")

flag=''

for i in range(0,len(a)):

        str = a[i]

        list=[]

        sum=0

        for j in str:

                list.append(j)

                length = len(list)

        for k in range(0,length):

                sum+=int(list[k])

        flag+=chr(sum+64)

print("明文为:",flag)


easy_RSA(已知p、q、e,求d 或 已知p、q、d,求e)

python代码

def rsa_moder(n):

        base=2

        while base<n:

                if n%base==0:

                        return base,n//base

                base+=1

def rsa_get_euler(prime1,prinme2):#求欧拉函数

        return (prime1-1)*(prime2-1)

def rsa_get_key(e,euler):

        k=1

        while True:

                if (((euler*k)+1)%e)==0:

                        return (euler*k+1)//e

                k+=1

def get_rsa_e_d(n,e=None,d=None):

        if e is None and d is None:

                return 0

        arg=e

        if arg is None:

                arg=d

        primes=rsa_moder(n)

        p=primes[0]

        q=primes[1]

        d=rsa_get_key(arg,rsa_get_euler(p,q))

        return d

p=int(input("p="))

q=int(input("q="))

n=p*q

e=int(input("e="))

d=get_rsa_e_d(n,e,None)

print("d=",d)


easychallenge

.pyc文件

* 由py文件经过编译后,生成的文件

* pip install uncompyle6  (安装uncompyle6库,用于反编译)

* 进入.pyc文件所在的文件夹,然后uncomplyle6 -o . easychallenge.pyc,即可在该文件夹中得到.py文件

得到的easychallenge.py文件

# uncompyle6 version 3.6.5

# Python bytecode 2.7 (62211)

# Decompiled from: Python 3.5.2 (v3.5.2:4def2a2901a5, Jun 25 2016, 22:18:55) [MSC v.1900 64 bit (AMD64)]

# Embedded file name: ans.py

# Compiled at: 2018-08-09 11:29:44

import base64

def encode1(ans):

        s = ''

        for i in ans:

                x = ord(i) ^ 36

                x = x + 25

                s += chr(x)

        return s

def encode2(ans):

        s = ''

        for i in ans:

                x = ord(i) + 36

                x = x ^ 36

                s += chr(x)

        return s

def encode3(ans):

        return base64.b32encode(ans)

flag = ' '

print 'Please Input your flag:'

flag = raw_input()

final = 'UC7KOWVXWVNKNIC2XCXKHKK2W5NLBKNOUOSK3LNNVWW3E==='

if encode3(encode2(encode1(flag))) == final:

        print 'correct'

else:

        print 'wrong'

根据上面的easychallenge.py文件,写求逆程序(easychallenge_resolve.py)

import base64

def decode1(s):

        ans=''

        for x in s:

                x=ord(x)-25

                i=x^36

                ans+=chr(i)

        return ans

def decode2(s):

        ans=''

        for x in s:

                x=x^36

                i=x-36     

                ans+=chr(i)

        return ans

def decode3(ans):

        return base64.b32decode(ans)

if __name__=="__main__":

        final = 'UC7KOWVXWVNKNIC2XCXKHKK2W5NLBKNOUOSK3LNNVWW3E==='

        flag=decode1(decode2(decode3(final)))

        print("flag=",flag)


Normal_RSA

将flag.enc和pubkey.pem放入kali虚拟机中,在终端打开,输入“openssl rsa -pubin -text -modulus -in warmup -in pubkey.pem”

得到的Modulus即为n,Exponent即为e

将得到的Modulus=....转化为十进制(工具)为87924348264132406875276140514499937145050893665602592992418171647042491658461,即为n

大数分解,得到:
p=275127860351348928173285174381581152299
q=319576316814478949870590164193048041239

在终端中输入“python rsatool.py -o private.pem -e 65537 -p 275127860351348928173285174381581152299 -q 319576316814478949870590164193048041239”

将flag.enc放在rsatool文件夹中,在终端打开,输入“openssl rsautl -decrypt -in flag.enc -inkey private.pem”


转轮机加密

python代码

import re

sss='''1:< ZWAXJGDLUBVIQHKYPNTCRMOSFE < 2: < KPBELNACZDTRXMJQOYHGVSFUWI < 3:< BDMAIZVRNSJUWFHTEQGYXPLOCK < 4: < RPLNDVHGFCUKTEBSXQYIZMJWAO < 5:< IHFRLABEUOTSGJVDKCPMNZQWXY < 6: < AMKGHIWPNYCJBFZDRUSLOQXVET < 7:< GWTHSPYBXIZULVKMRAFDCEONJQ < 8: < NOZUTWDCVRJLXKISEFAPMYGHBQ < 9:< XPLTDSRFHENYVUBMCQWAOIKZGJ < 10: < UDNAJFBOWTGVRSCZQKELMXYIHP <11 < MNBVCXZQWERTPOIUYALSKDJFHG < 12 < LVNCMXZPQOWEIURYTASBKJDFHG <13 < JZQAWSXCDERFVBGTYHNUMKILOP <'''

m="NFQKSEVOQOFNP"

content=re.findall(r'<(.*?) <',sss,re.S)

iv=[2,3,7,5,13,12,9,1,8,10,4,11,6]

vvv=[]

ans=""

for i in range(13):  

        index=content[iv[i]-1].index(m[i])  

        vvv.append(index)

for i inrange(0,26):  

        flag=""  

        for j in range(13):      

                flag+=content[iv[j]-1][(vvv[j]+i)%26]  

        print(flag)


easy_ECC

ECC:椭圆加密算法,ECC椭圆曲线加密学习笔记椭圆曲线的奇妙比喻

python代码

import collections

def inverse_mod(k, p):
        """Returns the inverse of k modulo p. This function returns the only integer x such that (x * k) % p == 1. k must be non-zero and p must be a prime. """
        if k == 0:
                raise ZeroDivisionError('division by zero')
        if k < 0:
                # k ** -1 = p - (-k) ** -1 (mod p)
                return p - inverse_mod(-k, p)
        # Extended Euclidean algorithm.
        s, old_s = 0, 1
        t, old_t = 1, 0
        r, old_r = p, k
        while r != 0:
                quotient = old_r // r
                old_r, r = r, old_r - quotient * r
                old_s, s = s, old_s - quotient * s
                old_t, t = t, old_t - quotient * t
        gcd, x, y = old_r, old_s, old_t
        assert gcd == 1
        assert (k * x) % p == 1
        return x % p

# Functions that work on curve points #
def is_on_curve(point): 
 """Returns True if the given point lies on the elliptic curve."""
        if point is None:
                # None represents the point at infinity.
                return True
        x, y = point
        return (y * y - x * x * x - curve.a * x - curve.b) % curve.p == 0

def point_neg(point):
        """Returns -point."""
        assert is_on_curve(point)
        if point is None: 
               # -0 = 0
                return None
        x, y = point
        result = (x, -y % curve.p)
        assert is_on_curve(result)
        return result

def point_add(point1, point2):
        """Returns the result of point1 + point2 according to the group law."""
        assert is_on_curve(point1)
        assert is_on_curve(point2)
        if point1 is None:
                 # 0 + point2 = point2
                return point2
        if point2 is None:
                # point1 + 0 = point1
                return point1
        x1, y1 = point1
        x2, y2 = point2
        if x1 == x2 and y1 != y2:
                # point1 + (-point1) = 0
                return None
        if x1 == x2:
                # This is the case point1 == point2.
                m = (3 * x1 * x1 + curve.a) * inverse_mod(2 * y1, curve.p)
        else:
                # This is the case point1 != point2.
                m = (y1 - y2) * inverse_mod(x1 - x2, curve.p)
        x3 = m * m - x1 - x2
        y3 = y1 + m * (x3 - x1)
        result = (x3 % curve.p, -y3 % curve.p)
        assert is_on_curve(result)
        return result

def scalar_mult(k, point):
        """Returns k * point computed using the double and point_add algorithm."""
        assert is_on_curve(point)
        if k < 0:
            # k * point = -k * (-point)
            return scalar_mult(-k, point_neg(point))
        result = None
        addend = point
        while k:
                if k & 1:
                        # Add.
                        result = point_add(result, addend)
                # Double.
                addend = point_add(addend, addend)
                k >>= 1
        assert is_on_curve(result)
        return result

# Keypair generation and ECDHE #
def make_keypair():
        """Generates a random private-public key pair."""
        private_key = curve.n
        public_key = scalar_mult(private_key, curve.g)
        return private_key, public_key

EllipticCurve = collections.namedtuple('EllipticCurve', 'name p a b g n h')
curve = EllipticCurve(
        'secp256k1',
        # Field characteristic.
        p=15424654874903,
        # Curve coefficients.
        a=16546484,
        b=4548674875,
        # Base point.
        g=(6478678675,5636379357093),
        # Subgroup order.
        n=546768,
        # Subgroup cofactor.
        h=1,
)
private_key, public_key = make_keypair()
print("private key:", hex(private_key))
print("public key: (0x{:x}, 0x{:x})".format(*public_key))
print("x + y = " + str(public_key[0] + public_key[1]))



参考:XCTF-攻防世界-密码学crypto-新手练习区-writeup

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 218,451评论 6 506
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 93,172评论 3 394
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 164,782评论 0 354
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,709评论 1 294
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,733评论 6 392
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,578评论 1 305
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,320评论 3 418
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 39,241评论 0 276
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,686评论 1 314
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,878评论 3 336
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,992评论 1 348
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,715评论 5 346
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,336评论 3 330
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,912评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 33,040评论 1 270
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 48,173评论 3 370
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,947评论 2 355

推荐阅读更多精彩内容

  • 0x01 base64 元宵节灯谜是一种古老的传统民间观灯猜谜的习俗。 因为谜语能启迪智慧又饶有兴趣,灯谜增添节日...
    Hf1dw阅读 18,803评论 1 6
  • "use strict";function _classCallCheck(e,t){if(!(e instanc...
    久些阅读 2,031评论 0 2
  • 定义枚举 项目中用到的手势解锁,网上搜到了一位大神所封装的,用起来很方便 KMNineBoxView是继承UIVi...
    神一样的队友阅读 1,192评论 1 0
  • 译者语:我没有任何技术背景,翻译这篇文章挺费劲,Google,维基百科左右相伴。感谢我的同事,程序宋少和美术李大师...
    Ginny阅读 470评论 0 1
  • 一.题目 二.解题过程 1.打开附件,考察ECC加密 2.参考脚本: importcollections impo...
    Du1in9阅读 1,031评论 0 0