看雪2019晋级赛
题目一 签到题目 流浪者
这道题目直接测试,下断点。1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#-*-coding:utf-8 -*-
print "Start... ..."
cin = "KanXueCTF2019JustForhappy"
flag = ""
for i in cin:
if i>= 'o' and i <= 'z':
sub = ord('o')-ord('C')
c = chr(ord(i) - sub)
flag = flag +c
elif i >= 'O' and i <= 'Z':
c = i
flag = flag + c
elif i >='B' and i<= 'N':
sub = ord('a') - ord('B')
c = chr(ord(i) + sub)
flag = flag + c
elif i>='j' and i <='n':
sub = ord('n') - ord ('j')
c = chr(ord(i)+ sub)
flag = flag + c
elif i>='0' and i <='7':
sub = ord('s')-ord('0')
c = chr(ord(i)+sub)
flag = flag + c
elif i>='8' and i <='9':
sub = ord('A')-ord('8')
c = chr(ord(i)+ sub)
flag = flag + c
elif i>='a' and i<='i':
sub = ord('a')-ord('0')
c = chr(ord(i)-sub)
flag = flag +c
elif i=='A':
c='9'
flag=flag +c
print flag
print "End... ..."
"""
j012AB
Kabc89
CDEFGHIJKLMN OPQRSTUVWXYZ
opqrstuvwxyz OPQRSTUVWXYZ
abcdefghijklm nopqr stuvwxyzAB
BCDEFGHIJKLMN jklmn 0123456789
0123456789
abcdefghiA
"""
题目十 初入好望角
是.net加密的,C#题目。 AES加密,附上python 和 C#两套解密脚本1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88from Crypto.Cipher import AES
from binascii import b2a_hex, a2b_hex
import base64
# 如果text不足16位的倍数就用空格补足为16位
def add_to_16(text):
if len(text.encode('utf-8')) % 16:
add = 16 - (len(text.encode('utf-8')) % 16)
else:
add = 0
text = text + ('\0' * add)
return text.encode('utf-8')
# 加密函数
def encrypt(text):
key = '9999999999999999'.encode('utf-8')
mode = AES.MODE_CBC
iv = b'qqqqqqqqqqqqqqqq'
text = add_to_16(text)
cryptos = AES.new(key, mode, iv)
cipher_text = cryptos.encrypt(text)
# 因为AES加密后的字符串不一定是ascii字符集的,输出保存可能存在问题,所以这里转为16进制字符串
return b2a_hex(cipher_text)
# 解密后,去掉补足的空格用strip() 去掉
def decrypt(text,key):
iv = 'Kanxue2019CTF-Q1'.encode('utf-8')
#key = b'Kanxue2019\0\0\0\0\0\0'
#6DDEF7A43C004F7D6983044B1E36A93459F18BC837C46EAF321132734163A0B4
mode = AES.MODE_CBC
cryptos = AES.new(key, mode, iv)
plain_text = cryptos.decrypt((text))
return bytes.decode(plain_text).rstrip('\0')
if __name__ == '__main__':
#print (hex(ord(a[i])))
encoding="6DDEF7A43C004F7D6983044B1E36A93459F18BC837C46EAF321132734163A0B4"
key = ""
for i in range(0,len(encoding),2):
#print (encoding[i]+encoding[i+1])
b = ((int)((i)/2))
#if b < len(a):
text = "0x"+encoding[i]+encoding[i+1]
flag = int(text,16)
key = key + chr(flag)
#print (chr(flag))
key=encoding
key = bytes.fromhex(key)
print (key)
e = "4RTlF9Ca2+oqExJwx68FiA=="
e = base64.b64decode(e)
s = decrypt(e,key)
print (s)
"""
python 3.5以后
a = 'aabbccddeeff'
a_bytes = bytes.fromhex(a)
print(a_bytes)
b'\xaa\xbb\xcc\xdd\xee\xff'
aa = a_bytes.hex()
print(aa)
aabbccddeeff
>>>
python2.7
a = 'aabbccddeeff'
a_bytes = a.decode('hex')
print(a_bytes)
b'\xaa\xbb\xcc\xdd\xee\xff'
aa = a_bytes.encode('hex')
print(aa)
aabbccddeeff
>>>
python2.8-python3.5
a = 'aabbccddeeff'
a_bytes = bytes.fromhex(a)
print(a_bytes)
b'\xaa\xbb\xcc\xdd\xee\xff'
aa = ''.join(['%02x' % b for b in a_bytes])
print(aa)
aabbccddeeff
>>>
"""
以下是C#code通过在线运行得到答案1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98//Rextester.Program.Main 是代码入口函数,不要改变它.
//编译版本 4.0.30319.17929 for Microsoft (R) .NET Framework 4.5
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
using System;
using System.IO;
using System.Security.Cryptography;
using System.Text;
namespace Rextester
{
public class Program
{
private const string string_0 = "Kanxue2019CTF-Q1";
private const int int_0 = 256;
public static void Main(string[] args)
{
//你的代码...
Console.WriteLine("Hello, world!");
if (Program.smethod_0("hello", "Kanxue2019") == "4RTlF9Ca2+oqExJwx68FiA==")
{
Console.WriteLine("Congratulations! : )");
Console.ReadLine();
}
string flag = Program.decry("4RTlF9Ca2+oqExJwx68FiA==");
}
public static string smethod_0(string string_1, string string_2)
{
byte[] bytes = Encoding.UTF8.GetBytes("Kanxue2019CTF-Q1");
byte[] bytes2 = Encoding.UTF8.GetBytes(string_1);
byte[] bytes3 = new PasswordDeriveBytes(string_2, null).GetBytes(32);
byte[] a=new byte[10];
string text = "";
for (int i = 0; i < bytes3.Length; i++)
{
text += bytes3[i].ToString("X2") ;
}
Console.WriteLine(text);
string str = System.Text.Encoding.Default.GetString ( bytes3 );
Console.WriteLine(str);
ICryptoTransform transform = new RijndaelManaged
{
Mode = CipherMode.CBC
}.CreateEncryptor(bytes3, bytes);
MemoryStream memoryStream = new MemoryStream();
CryptoStream expr_4F = new CryptoStream(memoryStream, transform, CryptoStreamMode.Write);
expr_4F.Write(bytes2, 0, bytes2.Length);
expr_4F.FlushFinalBlock();
byte[] inArray = memoryStream.ToArray();
memoryStream.Close();
expr_4F.Close();
return Convert.ToBase64String(inArray);
}
public static string decry(string source){
//Console.WriteLine("no answer");
//创建一个MemoryStream实例,存放收到的加密数据字节流
byte[] encryptoByte = Convert.FromBase64String(source);
//byte[] encryptoByte = mStream.ToArray();
MemoryStream encryptoStream = new MemoryStream(encryptoByte);
//创建RijndaelManaged实例
RijndaelManaged RMCrypto = new RijndaelManaged();
byte [] IV = Encoding.UTF8.GetBytes("Kanxue2019CTF-Q1");
string string_2 = "Kanxue2019";
Console.WriteLine("no answer");
byte [] Key = new PasswordDeriveBytes(string_2, null).GetBytes(32);
//创建用于解密的CryptoStream实例
CryptoStream CryptStream = new CryptoStream(encryptoStream,
RMCrypto.CreateDecryptor(Key, IV),
CryptoStreamMode.Read);
//创建StreamReader实例,从CryptoStream中读出数据,
//StreamReader默认使用UTF8编码读出的数据
StreamReader SReader = new StreamReader(CryptStream);
//输出解密后的消息.
Console.WriteLine("The decrypted original message: {0}",SReader.ReadToEnd());
Console.WriteLine("no answer");
return SReader.ReadToEnd();
}
}
}