2025-02-08 10:30:59 +08:00

131 lines
3.9 KiB
JSON

{
"crypto-rsa-basic": {
"prefix": "crypto-rsa",
"body": [
"from Crypto.Util.number import *",
"from Crypto.PublicKey import RSA",
"import gmpy2",
"",
"# 读取公钥文件(如果有)",
"#with open('${1:public.pem}', 'rb') as f:",
"# key = RSA.import_key(f.read())",
"# n = key.n",
"# e = key.e",
"",
"n = ${2:n} # 模数",
"e = ${3:e} # 公钥指数",
"c = ${4:c} # 密文",
"",
"# 常用RSA解密函数",
"def decrypt(p, q, e, c):",
" phi = (p-1) * (q-1)",
" d = gmpy2.invert(e, phi)",
" m = pow(c, d, p*q)",
" return long_to_bytes(m)",
"",
"$0",
"# 解密后的明文",
"#plain = decrypt(p, q, e, c)",
"#print(plain)"
]
},
"crypto-classical": {
"prefix": "crypto-classical",
"body": [
"from string import ascii_lowercase, ascii_uppercase",
"",
"def caesar_decrypt(ciphertext, shift):",
" result = ''",
" for char in ciphertext:",
" if char in ascii_lowercase:",
" result += chr((ord(char) - ord('a') - shift) % 26 + ord('a'))",
" elif char in ascii_uppercase:",
" result += chr((ord(char) - ord('A') - shift) % 26 + ord('A'))",
" else:",
" result += char",
" return result",
"",
"def vigenere_decrypt(ciphertext, key):",
" result = ''",
" key_length = len(key)",
" key = key.lower()",
" i = 0",
" for char in ciphertext:",
" if char.isalpha():",
" shift = ord(key[i % key_length]) - ord('a')",
" if char.isupper():",
" result += chr((ord(char) - ord('A') - shift) % 26 + ord('A'))",
" else:",
" result += chr((ord(char) - ord('a') - shift) % 26 + ord('a'))",
" i += 1",
" else:",
" result += char",
" return result",
"",
"ciphertext = '${1:ciphertext}'",
"$0"
]
},
"crypto-math": {
"prefix": "crypto-math",
"body": [
"from Crypto.Util.number import *",
"import gmpy2",
"",
"def extended_gcd(a, b):",
" if a == 0:",
" return b, 0, 1",
" else:",
" gcd, x, y = extended_gcd(b % a, a)",
" return gcd, y - (b // a) * x, x",
"",
"def mod_inverse(a, m):",
" gcd, x, _ = extended_gcd(a, m)",
" if gcd != 1:",
" raise Exception('模逆不存在')",
" else:",
" return x % m",
"",
"def chinese_remainder(remainders, moduli):",
" total = 0",
" product = 1",
" for modulus in moduli:",
" product *= modulus",
" for remainder, modulus in zip(remainders, moduli):",
" p = product // modulus",
" total += remainder * mod_inverse(p, modulus) * p",
" return total % product",
"",
"$0"
]
},
"crypto-hash": {
"prefix": "crypto-hash",
"body": [
"import hashlib",
"from itertools import product",
"import string",
"",
"def md5(text):",
" return hashlib.md5(text.encode()).hexdigest()",
"",
"def sha1(text):",
" return hashlib.sha1(text.encode()).hexdigest()",
"",
"def sha256(text):",
" return hashlib.sha256(text.encode()).hexdigest()",
"",
"def brute_force_hash(target_hash, length_range=(1,8), charset=string.ascii_lowercase):",
" for length in range(length_range[0], length_range[1] + 1):",
" for guess in product(charset, repeat=length):",
" guess_str = ''.join(guess)",
" if md5(guess_str) == target_hash:",
" return guess_str",
" return None",
"",
"target = '${1:hash_value}'",
"$0"
]
}
}