fromCrypto.PublicKeyimportRSAimportbase64importgmpy2importosdefextract_key_params(key_content):"""Extract n and e from the public key"""try:key=RSA.import_key(key_content)returnkey.n,key.eexceptExceptionase:print(f"Error parsing key: {e}")returnNone,Nonedefread_encrypted_file(file_path):"""Read and decode the encrypted file"""try:withopen(file_path,'r')asf:content=f.read().strip()returnbase64.b64decode(content)exceptExceptionase:print(f"Error reading encrypted file: {e}")returnNonedefsmall_exponent_attack(ciphertext,n,e):"""Attempt to decrypt using small exponent attack"""# Convert ciphertext to integerc_int=int.from_bytes(ciphertext,byteorder='big')print(f"Exponent (e): {e}")# Try to find the eth rootroot,is_perfect=gmpy2.iroot(c_int,e)root_int=int(root)# Verify if it's a correct solutionifpow(root_int,e,n)==c_int:print(f"Found exact {e}th root!")returnroot_intprint(f"Not a perfect {e}th power, trying nearby values...")# Check values around the approximate rootforiinrange(-1000,1000):candidate=root_int+iifpow(candidate,e,n)==c_int:print(f"Found solution with offset {i}")returncandidatereturnNonedefmain():# Public key contentpub_key_content="""-----BEGIN PUBLIC KEY-----
MIIEIDANBgkqhkiG9w0BAQEFAAOCBA0AMIIECAKCBAEA65aKnloZ2T7V0qKWny2v
AShiFQ4NSQz06AfC/aCCoESmI4bIBcjcv72C6turYdai8kc5my6nD//nbQIWKKxN
oU4Jvx9d0sssItSouqgHM78wylWhLk/OYcYoAU4/AFCScxXX4xdw46QVhZWVfLpw
pl5yO2kKYPTCEA9Xf4+KuYxWPdGG1dk923gdhRm8PL0Ixw+DN/Mzlulk7SVGpZgU
5w/LWDBxvu129KNgtS+t2etYPtosadg6SlHdoVB8aWgPaDM9vc6MU5h2VF+KBvmR
EfIOiM5Nv6ALnZ9fPKMERlYjpPzECUc4PQkemwrtxIRvQf0iP1b2p2BenOL/RgTi
lNvqU5TA7R8PzjIC3OCQ1Un/Tn1GCOo/Zn+87y34+bSRVWKCj5XShFYA/9K73NUC
Cz4F2wuKJL9n2Npl+2472uu8ibQWUNFxFXszLz5S7IouqJPWMY/y87NN3ig5je+P
h24UfuMRalgGF0C+SO2mzpR5auz9Sh0VPt24u6w79HsaNIuQ01/bt9s3QhYeu7d1
Uu/AgoYHKdUrQLEQwx/4PNXDNjPrkMKnyxfQdQyZBICxyVo/SjP1qSYER0QNK+MH
47rheX/Rlv/6yzO/x1Z+D9z+tNw4DVVrPqIc8GfDoidvClhQQ/tquU8xRUC3YH4F
MuaqgTLkUVPYwJhtCkzxJ3VyAb2e1pElNf16m2S5i7CR0Ryla6eIY9OyGhB3gM3c
z8y+6zTeWlfzcNTqyD9nHOutLmKWHowNZlXgKVfO46/jyQtPaMcevFXt2y+UaRKU
IDNLZPa7GqLIBWcgDPEYdtlvkdYONfQkfn1QklckFHBP6sMzERxorCajtBuKC4ag
YaU54vO5wFh5oIdiQ2VNno3e/xvII7k2i2Hp5wZ4jqZ+n7RyZ9Yl6XeAepDcwUGY
ZqtWFrQOlaqDIknkEk084GwjGWqsjXNBtw+JzkDw6NuSJtm529LgYhU6wYTt/5D7
X6242mrPjqZ0AtQzzkkyY88jN2bJxXTXuxjKJ3Dg0Sp2iky9ggkjHAJ9u0vR8Y62
gwGBadCiKnsMfbS+/f0ZiKf1WfiyjAzZ2c72m0RgDbjIk9gEDU9c9alZFRqJpQbP
gbs0ztsILIeINFYgkp0N/vaNdJlwVfhtQsQKouZY+3v0DUpLOyr7U8Hqh/U0Vhnx
ZfZXBe0pIgFdIWsgtR4SvPg2XLj6A1JmFOA7kb9rB7ePfxf1yLXQFDjHDl587JM4
7NOVauRuL8+atfny5utwISueNgFZqIdOIji7asWRKFT5EJ9AmP8ZIXKRcdzYtL4V
ySpRdr4MWUdOFrZQkrCLqe1zFaLb0srEQSmaFuaFQeGRkizd6QJMxUoP14sGcCOf
HQIBFw==
-----END PUBLIC KEY-----"""# Path to encrypted fileencrypted_file_path="c:\\Users\\hitma\\Downloads\\女婕思_2025\\level7.enc"# Extract RSA parametersn,e=extract_key_params(pub_key_content)ifnotnornote:print("Failed to extract key parameters.")returnprint(f"Modulus (n): {n}")print(f"Public exponent (e): {e}")# Read and decode the encrypted dataencrypted_data=read_encrypted_file(encrypted_file_path)ifnotencrypted_data:print("Failed to read encrypted data.")return# Apply small exponent attackplaintext_int=small_exponent_attack(encrypted_data,n,e)ifplaintext_int:# Convert integer to bytesplaintext_bytes=plaintext_int.to_bytes((plaintext_int.bit_length()+7)//8,byteorder='big')# Save decrypted content to filewithopen("decrypted_level7.txt","wb")asf:f.write(plaintext_bytes)# Try to display as texttry:plaintext_str=plaintext_bytes.decode('utf-8',errors='replace')print("\nDecrypted message:")print(plaintext_str)except:print("\nDecryption successful but couldn't display as text")print("Saved binary output to 'decrypted_level7.txt'")else:print("Decryption failed. The message might be too large or e isn't small enough.")if__name__=="__main__":main()
module reg_control (
input clk,
input rst,
output reg signed [7:0] data,
output reg led
);
always @(posedge clk) begin
data <= data - 1;
end
always @(posedge clk) begin
data <= data + 5;
end
always @(posedge clk) begin
if (data < 0)
// flag {.....}
led <= 1;
else
led <= 0;
end
endmodule