def key_regen(self): username = self.u_name try: user = User.select().where(User.username == username).get() pwd_md5 = user.pwd pwd2num = passcoder.Utils.str2num(pwd_md5) # 生成rabin公私钥 rabin = passcoder.PKSRabin() rabin_p, rabin_q, rabin_n = rabin.keygen(512) # 使用用户的账号的密码进行保护 rabin_xor_p = rabin_p ^ pwd2num rabin_xor_q = rabin_q ^ pwd2num # 生成rsa公私钥 rsa_sign = passcoder.RsaSign() sign_p, sign_q, sign_e, sign_d = rsa_sign.k_gen(32) sign_xor_p = sign_p ^ pwd2num sign_xor_q = sign_q ^ pwd2num sign_xor_e = sign_e ^ pwd2num sign_xor_d = sign_d ^ pwd2num # save user.rabin_p = str(rabin_xor_p) user.rabin_q = str(rabin_xor_q) user.rsa_p = str(sign_xor_p) user.rsa_q = str(sign_xor_q) user.rsa_e = str(sign_xor_e) user.rsa_d = str(sign_xor_d) user.save() self.showmsg("key regen success") except Exception as e: self.showmsg("{}".format(e))
def verify(self): if not os.path.exists(self.pub_rabin) or not os.path.exists( self.pub_rsa): self.showmsg("file not exists") return # load ras public key file rsa_start, rsa_e, rsa_n, rsa_end = KeyUtil.load_rsa_pub(self.pub_rsa) if "RSA-PUBLIC-KEY-START" not in rsa_start or "RSA-PUBLIC-KEY-END" not in rsa_end: self.showmsg("rsa pubkey file format not match") return # load rabin public key file rabin_start, rabin_n, rabin_sign, rabin_end = KeyUtil.load_rabin_pub( self.pub_rabin) if "RABIN-PUBLIC-KEY-START" not in rabin_start or "RABIN-PUBLIC-KEY-END" not in rabin_end: self.showmsg("rabin pubkey file format not match") return rabin_n_un = base64.b32decode(rabin_n.encode("utf-8")) rsa_e_un = int(base64.b32decode(rsa_e.encode("utf-8")).decode('utf-8')) rsa_n_un = int(base64.b32decode(rsa_n.encode("utf-8")).decode('utf-8')) crc32_digest = passcoder.RsaSign().unsign(rsa_e_un, rsa_n_un, rabin_sign) crc32_rabin = binascii.crc32(rabin_n_un) if crc32_rabin == crc32_digest: self.showmsg("verify success ,signature match") else: self.showmsg("verify failed ,signature not match")
def sign(self): username = self.le_username.text() password = self.le_pwd.text() password2 = self.le_pwd_again.text() if len(username) < 8 or len(password) < 8 or len(password2) < 8: self.showmsg("length not enough") return # username = username.replace(" ", "") # password = password.replace(" ", "") # password2 = password2.replace(" ", "") if password != password2: self.showmsg("password do not match") return # 判断库里是不是有重复的 if User.select().where(User.username == username).count() != 0: self.showmsg("username already registered") return pwd_md5 = hashlib.md5(password.encode("utf-8")).hexdigest() pwd2num = passcoder.Utils.str2num(pwd_md5) # convert string to num # 生成rabin公私钥 rabin = passcoder.PKSRabin() rabin_p, rabin_q, rabin_n = rabin.keygen(512) # 使用用户的账号的密码进行保护 rabin_xor_p = rabin_p ^ pwd2num rabin_xor_q = rabin_q ^ pwd2num # 生成rsa公私钥 rsa_sign = passcoder.RsaSign() sign_p, sign_q, sign_e, sign_d = rsa_sign.k_gen(32) sign_xor_p = sign_p ^ pwd2num sign_xor_q = sign_q ^ pwd2num sign_xor_e = sign_e ^ pwd2num sign_xor_d = sign_d ^ pwd2num try: # create user User.create(username=username, pwd=pwd_md5, rabin_p=str(rabin_xor_p), rabin_q=str(rabin_xor_q), rsa_p=str(sign_xor_p), rsa_q=str(sign_xor_q), rsa_e=str(sign_xor_e), rsa_d=str(sign_xor_d)) self.le_username.setText("") self.le_pwd.setText("") self.le_pwd_again.setText("") self.showmsg("register success,close and return to login") except Exception as e: self.showmsg("register failed:{}".format(e)) return
def encrypt(self): text = self.textEditor.toPlainText() if len(text) == 0: self.showmsg("please input some text") return if not self.rabin_n: self.showmsg("please load public key file first") return filename = "{}-{}-encrypted-msg.txt".format(datetime.datetime.now(), self.u_name) encrypted_msg = passcoder.RsaSign().encrypt(self.rabin_n, text) with open(filename, "w+") as f: f.write(encrypted_msg) self.showmsg("save success,file:{}".format(filename)) self.close()
def dl_pubkey(self): ''' download rabin public key ''' try: # 设置publickey file_path = QFileDialog.getExistingDirectory( self, "choose path to save", r"./") filename = file_path + convert_path("/{}.pub_rabin".format( self.u_name)) print("文件{}".format(filename)) # 下载rabin key user = User.user = User.select().where( User.username == self.u_name).get() rabin_p, rabin_q, rsa_p, rsa_q, rsa_e, rsa_d = xor_allkey( user.username) rsa_sign = passcoder.RsaSign() rabin_n = str(rabin_q * rabin_p) signer = rsa_sign.sign(rsa_d, rsa_p, rsa_q, rabin_n) if KeyUtil.gen_pub_key(user.username, rabin_n, signer, filename): # 产生rsa的公钥 KeyUtil.gen_rsa_pubkey( user.username, str(rsa_e), str(rsa_p * rsa_q), file_path + convert_path("/{}.pub_rsa".format(self.u_name))) self.showmsg( "save success,file name:{}.pub_rabin,{}.pub_rsa".format( self.u_name, self.u_name)) else: self.showmsg("save success,file failed.format") except Exception as e: print(e) self.showmsg(e)