Crypto-1

VDN3{zixmtkoj} 凱薩密碼 shift = 5

i —> n ----------012345 abcdefghijklmnopqrstu

Crypto-2

GOY3{3tIxevzu} 凱薩密碼 shift = 6

(可以發現其實是 -6 )

int main()
{
    cin.tie(0)->sync_with_stdio(false);
    
    
    string s;
    s = "GOY3{3tIxevzu}";
    int d = 20;
    for(auto &c : s)
    {
        if('A' <= c && c <= 'Z')c = (c-'A'+d)%26 + 'A';
        if('a' <= c && c <= 'z')c = (c-'a'+d)%26 + 'a';
    }
    cout << s;
}

Crypto-3

已知開頭是AIS3 算一下 s 的開頭離 a 的距離(向前的距離)

int main()
{
    cin.tie(0)->sync_with_stdio(false);
    
    
    string s;
    s = "JRB3{tw0fw_y1jrwcngc}";
    int d = 'A' - s[0];
    while(d < 0)d += 26;
    for(auto &c : s)
    {
        if('A' <= c && c <= 'Z')c = (c-'A'+d)%26 + 'A';
        if('a' <= c && c <= 'z')c = (c-'a'+d)%26 + 'a';
    }
    cout << s;
}

Crypto-4

與 Crypto-3 相同

int main()
{
    cin.tie(0)->sync_with_stdio(false);
    
    
    string s;
    s = "LTD3{mcfe3_q0cnp}";
    int d = 'A' - s[0];
    while(d < 0)d += 26; // 其實可以寫成 if 因為 d 不會 <= -26
    for(auto &c : s)
    {
        if('A' <= c && c <= 'Z')c = (c-'A'+d)%26 + 'A';
        if('a' <= c && c <= 'z')c = (c-'a'+d)%26 + 'a';
    }
    cout << s;
}

Crypto-5

image

image

image

image

image

image

image

image

image

image

image

image

image

另解: 可以注意到他的flag或對照表在前端 image

Crypto-6

ull pow(ull e, ull u, ull MOD)
{
    ull ans = 1;
    while(u)
    {
        if(u & 1)ans *= e;
        e *= e;
        ans %= MOD;
        e %= MOD;
        u >>= 1;
    }
    return ans;
}

int main()
{
    cin.tie(0)->sync_with_stdio(false);
    
    
    ull n = 5767, p = 73, e = 7, c = 4780;
    ull q = n / p, phi = (p-1)*(q-1);
    ull d = pow(e, phi-2, phi);

    ull m = pow(c, d, n);

    cout << m;
}

(忘記phi不一定是質數了 還是乖乖用py寫好了)

n = 5767
p = 73
e = int(7)
c = 4780
q = n/p
phi = int((p-1)*(q-1))
d = pow(e, -1, phi)
m = c**d % n
print(m)

(用 gemini 生了個 C++ 的解法)

#include <iostream>

using namespace std;

using ull = unsigned long long;
using ll = long long;

// 快速冪:計算 (base^exp) % mod
ull pow_mod(ull base, ull exp, ull mod) {
    ull ans = 1;
    base %= mod;
    while (exp > 0) {
        if (exp & 1) ans = (__int128)ans * base % mod; // 防止相乘溢位
        base = (__int128)base * base % mod;
        exp >>= 1;
    }
    return ans;
}

// 擴展歐幾里得演算法:求解 a*x + b*y = gcd(a, b)
ll extgcd(ll a, ll b, ll &x, ll &y) {
    if (b == 0) {
        x = 1;
        y = 0;
        return a;
    }
    ll x1, y1;
    ll d = extgcd(b, a % b, x1, y1);
    x = y1;
    y = x1 - y1 * (a / b);
    return d;
}

// 計算 a 在模 m 下的逆元 (即 d * e ≡ 1 mod phi)
ull modInverse(ull a, ull m) {
    ll x, y;
    extgcd(a, m, x, y);
    // 將結果調整為正數
    return (x % (ll)m + m) % m;
}

int main() {
    cin.tie(0)->sync_with_stdio(false);

    ull n = 5767, p = 73, e = 7, c = 4780;
    ull q = n / p;
    ull phi = (p - 1) * (q - 1);

    // 使用擴展歐幾里得演算法求 d
    ull d = modInverse(e, phi);

    // 解密訊息 m = (c^d) % n
    ull m = pow_mod(c, d, n);

    cout << "d = " << d << "\n";
    cout << "m = " << m << "\n";

    return 0;
}

Crypto-7

同理

import math

n = 10807
e = int(7)
c = 1220
p = 1
q = n/p
while math.gcd(int((p-1)*(q-1)), e) != 1:
    p += 1
    while n % p != 0:
        p += 1
    q = n/p

phi = int((p-1)*(q-1))
d = pow(e, -1, phi)
m = c**d % n
print("m=", m)

Crypto-8

對他使用二分搜吧(因為他取餘的數值非常大)

from Crypto.Util.number import bytes_to_long, long_to_bytes

c = int(104113716851630291574772668658086347914139279774012288613)

l = int(1)
r = c+1
while l+1 < r:
    mid = (l+r)//2
    if mid**3 <= c:
        l = mid
    else:
        r = mid

print(long_to_bytes(l))

Crypto-HW-01

image 查查看 pigpen-key image AIS3{P1GPEN_L00KUP}

Crypto-HW-02

QkZXWEVNIEJUUEJTRlNUU0ZIUSBKRktNQiBFTVNTTU9CIFBUUyBFTVJWQiBFUlFEVFJETSBCU1JTRkJTRkdCLiBLTUdIS00gU0pNIFBSQk02NCBFUklNTyBVRk9CUyBTSk1RIEJIRUFNIFNKTSBHRlhKTU8uIFNKTSBVRVJEIEZCIFJGQjN7UDY0X1NKTVFfVVJ9Lg== 看到== 是base64 base64解碼後得 BFWXEM BTPBSFSTSFHQ JFKMB EMSSMOB PTS EMRVB ERQDTRDM BSRSFBSFGB. KMGHKM SJM PRBM64 ERIMO UFOBS SJMQ BHEAM SJM GFXJMO. SJM UERD FB RFB3{P64_SJMQ_UR}. 和Crypto-5一樣 可以使用quipqiup 或直接丟進Crypto-5的來玩玩看 image image

Crypto-HW-03

Crypto-3做多次一點

#include <bits/stdc++.h>

using namespace std;

using ull = unsigned long long;
using ll = long long;

int main()
{
    cin.tie(0)->sync_with_stdio(false);
    
    
    string s;
    ofstream out("solve_ctf.txt");
    while(getline(cin, s, '\n'))
    {
        int idx = s.find('{')-2;
        int d = 'S' - s[idx];
        while(d < 0)d += 26;
        for(auto &c : s)
        {
            if('A' <= c && c <= 'Z')c = (c-'A'+d)%26 + 'A';
            if('a' <= c && c <= 'z')c = (c-'a'+d)%26 + 'a';
        }
        out << s << '\n';
    }
    
}

solve_ctf.txt檔案有點大 the secret message is AIS3{c4es_ar0k} keep looking

Crypto-HW-04

同Crypto-7

import math
n = 21968021
e = 17
c = 5854946
e = int(17)
p = 1
q = n/p
while math.gcd(int((p-1)*(q-1)), e) != 1:
    p += 1
    while n % p != 0:
        p += 1
    q = n/p

phi = int((p-1)*(q-1))
d = pow(e, -1, phi)
m = c**d % n
print("m=", m)

(跑起來需要 1 分鐘)

Crypto-HW-05

同Crypto-8

from Crypto.Util.number import bytes_to_long, long_to_bytes

c = int(138390867184400671369429485315529066403913401512756077717260811128013885680231233545090894181)

l = int(1)
r = c+1
while l+1 < r:
    mid = (l+r)//2
    if mid**3 <= c:
        l = mid
    else:
        r = mid

print(long_to_bytes(l))