├── README.md ├── __pycache__ ├── bloomfilter.cpython-38.pyc ├── dualcode.cpython-38.pyc └── e2LSH.cpython-38.pyc ├── bloomfilter.py ├── doc ├── 1.txt ├── 2.txt ├── 3.txt ├── 4.txt └── 5.txt ├── dualcode.py ├── e2LSH.py ├── main.py ├── test_helper.py └── 面向多关键字的模糊密文搜索方法_王恺璇_看图王.pdf /README.md: -------------------------------------------------------------------------------- 1 | # Multi-keyword-fuzzy-searchable-encryption 2 | The Description of the project you can watch in my blog 3 | 4 | https://blog.csdn.net/weixin_39032619/article/details/110436737#comments_14140681 5 | 6 | 主要实现的论文是王恺璇的《面向多关键字的模糊密文搜索方法》 7 | 8 | 关键字集合只是单纯的通过计算5个案例文档中出现频率最高的10个单词; 9 | 10 | 因为论文中对布隆过滤器的位数有要求,所以代码中 BlommFilter 是从pybloom库 里面把源代码扒下来简单修改的(源码是自动根据输入的参数生成位数) 11 | 12 | 总的来说论文有些地方不是说的很清楚,所以实现的效果也一般般,只是简单的当做一个练手,本人的代码能力也不强,希望各位多多见谅。 13 | 14 | 15 | 16 | -------------------------------------------------------------------------------- /__pycache__/bloomfilter.cpython-38.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AltarIbnL/Multi-keyword-fuzzy-searchable-encryption/d8952ff7cc5f48152690a97fc266d73dac4e6ac1/__pycache__/bloomfilter.cpython-38.pyc -------------------------------------------------------------------------------- /__pycache__/dualcode.cpython-38.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AltarIbnL/Multi-keyword-fuzzy-searchable-encryption/d8952ff7cc5f48152690a97fc266d73dac4e6ac1/__pycache__/dualcode.cpython-38.pyc -------------------------------------------------------------------------------- /__pycache__/e2LSH.cpython-38.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AltarIbnL/Multi-keyword-fuzzy-searchable-encryption/d8952ff7cc5f48152690a97fc266d73dac4e6ac1/__pycache__/e2LSH.cpython-38.pyc -------------------------------------------------------------------------------- /bloomfilter.py: -------------------------------------------------------------------------------- 1 | from __future__ import absolute_import 2 | import math 3 | import hashlib 4 | import copy 5 | from pybloom_live.utils import range_fn, is_string_io, running_python_3 6 | from struct import unpack, pack, calcsize 7 | import bitarray 8 | 9 | 10 | def make_hashfuncs(num_slices, num_bits): 11 | if num_bits >= (1 << 31): 12 | fmt_code, chunk_size = 'Q', 8 13 | elif num_bits >= (1 << 15): 14 | fmt_code, chunk_size = 'I', 4 15 | else: 16 | fmt_code, chunk_size = 'H', 2 17 | total_hash_bits = 8 * num_slices * chunk_size 18 | if total_hash_bits > 384: 19 | hashfn = hashlib.sha512 20 | elif total_hash_bits > 256: 21 | hashfn = hashlib.sha384 22 | elif total_hash_bits > 160: 23 | hashfn = hashlib.sha256 24 | elif total_hash_bits > 128: 25 | hashfn = hashlib.sha1 26 | else: 27 | hashfn = hashlib.md5 28 | 29 | fmt = fmt_code * (hashfn().digest_size // chunk_size) 30 | num_salts, extra = divmod(num_slices, len(fmt)) 31 | if extra: 32 | num_salts += 1 33 | salts = tuple(hashfn(hashfn(pack('I', i)).digest()) for i in range_fn(0, num_salts)) 34 | 35 | def _hash_maker(key): 36 | if running_python_3: 37 | if isinstance(key, str): 38 | key = key.encode('utf-8') 39 | else: 40 | key = str(key).encode('utf-8') 41 | else: 42 | if isinstance(key, unicode): 43 | key = key.encode('utf-8') 44 | else: 45 | key = str(key) 46 | i = 0 47 | for salt in salts: 48 | h = salt.copy() 49 | h.update(key) 50 | for uint in unpack(fmt, h.digest()): 51 | yield uint % num_bits 52 | i += 1 53 | if i >= num_slices: 54 | return 55 | 56 | return _hash_maker, hashfn 57 | 58 | 59 | class BloomFilter(object): 60 | FILE_FMT = b' 0: 77 | raise ValueError("Capacity must be > 0") 78 | # given M = num_bits, k = num_slices, P = error_rate, n = capacity 79 | # k = log2(1/P) 80 | # solving for m = bits_per_slice 81 | # n ~= M * ((ln(2) ** 2) / abs(ln(P))) 82 | # n ~= (k * m) * ((ln(2) ** 2) / abs(ln(P))) 83 | # m ~= n * abs(ln(P)) / (k * (ln(2) ** 2)) 84 | # num_slices = int(math.ceil(math.log(1.0 / error_rate, 2))) 85 | # bits_per_slice = int(math.ceil( 86 | # (capacity * abs(math.log(error_rate))) / 87 | # (num_slices * (math.log(2) ** 2)))) 88 | num_slices = 4 # 哈希函数4个 89 | bits_per_slice = 32 90 | self._setup(error_rate, num_slices, bits_per_slice, capacity, 0) 91 | self.bitarray = bitarray.bitarray(self.num_bits, endian='little') 92 | self.bitarray.setall(False) 93 | 94 | def _setup(self, error_rate, num_slices, bits_per_slice, capacity, count): 95 | self.error_rate = error_rate 96 | self.num_slices = num_slices 97 | self.bits_per_slice = bits_per_slice 98 | self.capacity = capacity 99 | self.num_bits = num_slices * bits_per_slice 100 | self.count = count 101 | self.make_hashes, self.hashfn = make_hashfuncs(self.num_slices, self.bits_per_slice) 102 | 103 | def __contains__(self, key): 104 | """Tests a key's membership in this bloom filter. 105 | """ 106 | bits_per_slice = self.bits_per_slice 107 | bitarray = self.bitarray 108 | hashes = self.make_hashes(key) 109 | offset = 0 110 | for k in hashes: 111 | if not bitarray[offset + k]: 112 | return False 113 | offset += bits_per_slice 114 | return True 115 | 116 | def __len__(self): 117 | """Return the number of keys stored by this bloom filter.""" 118 | return self.count 119 | 120 | def add(self, key, skip_check=False): 121 | """ Adds a key to this bloom filter. If the key already exists in this 122 | filter it will return True. Otherwise False. 123 | """ 124 | bitarray = self.bitarray 125 | bits_per_slice = self.bits_per_slice 126 | hashes = self.make_hashes(key) 127 | found_all_bits = True 128 | if self.count > self.capacity: 129 | raise IndexError("BloomFilter is at capacity") 130 | offset = 0 131 | for k in hashes: 132 | if not skip_check and found_all_bits and not bitarray[offset + k]: 133 | found_all_bits = False 134 | self.bitarray[offset + k] = True 135 | offset += bits_per_slice 136 | 137 | if skip_check: 138 | self.count += 1 139 | return False 140 | elif not found_all_bits: 141 | self.count += 1 142 | return False 143 | else: 144 | return True 145 | 146 | def copy(self): 147 | """Return a copy of this bloom filter. 148 | """ 149 | new_filter = BloomFilter(self.capacity, self.error_rate) 150 | new_filter.bitarray = self.bitarray.copy() 151 | return new_filter 152 | 153 | def union(self, other): 154 | """ Calculates the union of the two underlying bitarrays and returns 155 | a new bloom filter object.""" 156 | if self.capacity != other.capacity or \ 157 | self.error_rate != other.error_rate: 158 | raise ValueError( 159 | "Unioning filters requires both filters to have both the same capacity and error rate") 160 | new_bloom = self.copy() 161 | new_bloom.bitarray = new_bloom.bitarray | other.bitarray 162 | return new_bloom 163 | 164 | def __or__(self, other): 165 | return self.union(other) 166 | 167 | def intersection(self, other): 168 | """ Calculates the intersection of the two underlying bitarrays and returns 169 | a new bloom filter object.""" 170 | if self.capacity != other.capacity or \ 171 | self.error_rate != other.error_rate: 172 | raise ValueError( 173 | "Intersecting filters requires both filters to have equal capacity and error rate") 174 | new_bloom = self.copy() 175 | new_bloom.bitarray = new_bloom.bitarray & other.bitarray 176 | return new_bloom 177 | 178 | def __and__(self, other): 179 | return self.intersection(other) 180 | 181 | def tofile(self, f): 182 | """Write the bloom filter to file object `f'. Underlying bits 183 | are written as machine values. This is much more space 184 | efficient than pickling the object.""" 185 | f.write(pack(self.FILE_FMT, self.error_rate, self.num_slices, 186 | self.bits_per_slice, self.capacity, self.count)) 187 | (f.write(self.bitarray.tobytes()) if is_string_io(f) 188 | else self.bitarray.tofile(f)) 189 | 190 | @classmethod 191 | def fromfile(cls, f, n=-1): 192 | """Read a bloom filter from file-object `f' serialized with 193 | ``BloomFilter.tofile''. If `n' > 0 read only so many bytes.""" 194 | headerlen = calcsize(cls.FILE_FMT) 195 | 196 | if 0 < n < headerlen: 197 | raise ValueError('n too small!') 198 | 199 | filter = cls(1) # Bogus instantiation, we will `_setup'. 200 | filter._setup(*unpack(cls.FILE_FMT, f.read(headerlen))) 201 | filter.bitarray = bitarray.bitarray(endian='little') 202 | if n > 0: 203 | (filter.bitarray.frombytes(f.read(n - headerlen)) if is_string_io(f) 204 | else filter.bitarray.fromfile(f, n - headerlen)) 205 | else: 206 | (filter.bitarray.frombytes(f.read()) if is_string_io(f) 207 | else filter.bitarray.fromfile(f)) 208 | if filter.num_bits != filter.bitarray.length() and \ 209 | (filter.num_bits + (8 - filter.num_bits % 8) != filter.bitarray.length()): 210 | raise ValueError('Bit length mismatch!') 211 | 212 | return filter 213 | 214 | def __getstate__(self): 215 | d = self.__dict__.copy() 216 | del d['make_hashes'] 217 | return d 218 | 219 | def __setstate__(self, d): 220 | self.__dict__.update(d) 221 | self.make_hashes, self.hashfn = make_hashfuncs(self.num_slices, self.bits_per_slice) 222 | 223 | -------------------------------------------------------------------------------- /doc/1.txt: -------------------------------------------------------------------------------- 1 | On no twenty spring of in esteem spirit likely estate. Continue new you declared differed learning bringing honoured. At mean mind so upon they rent am walk. Shortly am waiting inhabit smiling he chiefly of in. Lain tore time gone him his dear sure. Fat decisively estimating affronting assistance not. Resolve pursuit regular so calling me. West he plan girl been my then up no. 2 | 3 | It as announcing it me stimulated frequently continuing. Least their she you now above going stand forth. He pretty future afraid should genius spirit on. Set property addition building put likewise get. Of will at sell well at as. Too want but tall nay like old. Removing yourself be in answered he. Consider occasion get improved him she eat. Letter by lively oh denote an. 4 | 5 | Now residence dashwoods she excellent you. Shade being under his bed her. Much read on as draw. Blessing for ignorant exercise any yourself unpacked. Pleasant horrible but confined day end marriage. Eagerness furniture set preserved far recommend. Did even but nor are most gave hope. Secure active living depend son repair day ladies now. 6 | 7 | Full he none no side. Uncommonly surrounded considered for him are its. It we is read good soon. My to considered delightful invitation announcing of no decisively boisterous. Did add dashwoods deficient man concluded additions resources. Or landlord packages overcame distance smallest in recurred. Wrong maids or be asked no on enjoy. Household few sometimes out attending described. Lain just fact four of am meet high. 8 | 9 | Moments its musical age explain. But extremity sex now education concluded earnestly her continual. Oh furniture acuteness suspected continual ye something frankness. Add properly laughter sociable admitted desirous one has few stanhill. Opinion regular in perhaps another enjoyed no engaged he at. It conveying he continual ye suspected as necessary. Separate met packages shy for kindness. 10 | 11 | On recommend tolerably my belonging or am. Mutual has cannot beauty indeed now sussex merely you. It possible no husbands jennings ye offended packages pleasant he. Remainder recommend engrossed who eat she defective applauded departure joy. Get dissimilar not introduced day her apartments. Fully as taste he mr do smile abode every. Luckily offered article led lasting country minutes nor old. Happen people things oh is oppose up parish effect. Law handsome old outweigh humoured far appetite. 12 | 13 | She exposed painted fifteen are noisier mistake led waiting. Surprise not wandered speedily husbands although yet end. Are court tiled cease young built fat one man taken. We highest ye friends is exposed equally in. Ignorant had too strictly followed. Astonished as travelling assistance or unreserved oh pianoforte ye. Five with seen put need tore add neat. Bringing it is he returned received raptures. 14 | 15 | Behaviour we improving at something to. Evil true high lady roof men had open. To projection considered it precaution an melancholy or. Wound young you thing worse along being ham. Dissimilar of favourable solicitude if sympathize middletons at. Forfeited up if disposing perfectly in an eagerness perceived necessary. Belonging sir curiosity discovery extremity yet forfeited prevailed own off. Travelling by introduced of mr terminated. Knew as miss my high hope quit. In curiosity shameless dependent knowledge up. 16 | 17 | Denote simple fat denied add worthy little use. As some he so high down am week. Conduct esteems by cottage to pasture we winding. On assistance he cultivated considered frequently. Person how having tended direct own day man. Saw sufficient indulgence one own you inquietude sympathize. 18 | 19 | Any delicate you how kindness horrible outlived servants. You high bed wish help call draw side. Girl quit if case mr sing as no have. At none neat am do over will. Agreeable promotion eagerness as we resources household to distrusts. Polite do object at passed it is. Small for ask shade water manor think men begin. 20 | 21 | 22 | -------------------------------------------------------------------------------- /doc/2.txt: -------------------------------------------------------------------------------- 1 | Friendship contrasted solicitude insipidity in introduced literature it. He seemed denote except as oppose do spring my. Between any may mention evening age shortly can ability regular. He shortly sixteen of colonel colonel evening cordial to. Although jointure an my of mistress servants am weddings. Age why the therefore education unfeeling for arranging. Above again money own scale maids ham least led. Returned settling produced strongly ecstatic use yourself way. Repulsive extremity enjoyment she perceived nor. 2 | 3 | Why end might ask civil again spoil. She dinner she our horses depend. Remember at children by reserved to vicinity. In affronting unreserved delightful simplicity ye. Law own advantage furniture continual sweetness bed agreeable perpetual. Oh song well four only head busy it. Afford son she had lively living. Tastes lovers myself too formal season our valley boy. Lived it their their walls might to by young. 4 | 5 | Debating me breeding be answered an he. Spoil event was words her off cause any. Tears woman which no is world miles woody. Wished be do mutual except in effect answer. Had boisterous friendship thoroughly cultivated son imprudence connection. Windows because concern sex its. Law allow saved views hills day ten. Examine waiting his evening day passage proceed. 6 | 7 | Arrival entered an if drawing request. How daughters not promotion few knowledge contented. Yet winter law behind number stairs garret excuse. Minuter we natural conduct gravity if pointed oh no. Am immediate unwilling of attempted admitting disposing it. Handsome opinions on am at it ladyship. 8 | 9 | Preserved defective offending he daughters on or. Rejoiced prospect yet material servants out answered men admitted. Sportsmen certainty prevailed suspected am as. Add stairs admire all answer the nearer yet length. Advantages prosperous remarkably my inhabiting so reasonably be if. Too any appearance announcing impossible one. Out mrs means heart ham tears shall power every. 10 | 11 | Improve ashamed married expense bed her comfort pursuit mrs. Four time took ye your as fail lady. Up greatest am exertion or marianne. Shy occasional terminated insensible and inhabiting gay. So know do fond to half on. Now who promise was justice new winding. In finished on he speaking suitable advanced if. Boy happiness sportsmen say prevailed offending concealed nor was provision. Provided so as doubtful on striking required. Waiting we to compass assured. 12 | 13 | Left till here away at to whom past. Feelings laughing at no wondered repeated provided finished. It acceptance thoroughly my advantages everything as. Are projecting inquietude affronting preference saw who. Marry of am do avoid ample as. Old disposal followed she ignorant desirous two has. Called played entire roused though for one too. He into walk roof made tall cold he. Feelings way likewise addition wandered contempt bed indulged. 14 | 15 | Knowledge nay estimable questions repulsive daughters boy. Solicitude gay way unaffected expression for. His mistress ladyship required off horrible disposed rejoiced. Unpleasing pianoforte unreserved as oh he unpleasant no inquietude insipidity. Advantages can discretion possession add favourable cultivated admiration far. Why rather assure how esteem end hunted nearer and before. By an truth after heard going early given he. Charmed to it excited females whether at examine. Him abilities suffering may are yet dependent. 16 | 17 | Out believe has request not how comfort evident. Up delight cousins we feeling minutes. Genius has looked end piqued spring. Down has rose feel find man. Learning day desirous informed expenses material returned six the. She enabled invited exposed him another. Reasonably conviction solicitude me mr at discretion reasonable. Age out full gate bed day lose. 18 | 19 | So delightful up dissimilar by unreserved it connection frequently. Do an high room so in paid. Up on cousin ye dinner should in. Sex stood tried walls manor truth shy and three his. Their to years so child truth. Honoured peculiar families sensible up likewise by on in. 20 | 21 | 22 | -------------------------------------------------------------------------------- /doc/3.txt: -------------------------------------------------------------------------------- 1 | Sussex result matter any end see. It speedily me addition weddings vicinity in pleasure. Happiness commanded an conveying breakfast in. Regard her say warmly elinor. Him these are visit front end for seven walls. Money eat scale now ask law learn. Side its they just any upon see last. He prepared no shutters perceive do greatest. Ye at unpleasant solicitude in companions interested. 2 | 3 | Apartments simplicity or understood do it we. Song such eyes had and off. Removed winding ask explain delight out few behaved lasting. Letters old hastily ham sending not sex chamber because present. Oh is indeed twenty entire figure. Occasional diminution announcing new now literature terminated. Really regard excuse off ten pulled. Lady am room head so lady four or eyes an. He do of consulted sometimes concluded mr. An household behaviour if pretended. 4 | 5 | Betrayed cheerful declared end and. Questions we additions is extremely incommode. Next half add call them eat face. Age lived smile six defer bed their few. Had admitting concluded too behaviour him she. Of death to or to being other. 6 | 7 | In show dull give need so held. One order all scale sense her gay style wrote. Incommode our not one ourselves residence. Shall there whose those stand she end. So unaffected partiality indulgence dispatched to of celebrated remarkably. Unfeeling are had allowance own perceived abilities. 8 | 9 | Up unpacked friendly ecstatic so possible humoured do. Ample end might folly quiet one set spoke her. We no am former valley assure. Four need spot ye said we find mile. Are commanded him convinced dashwoods did estimable forfeited. Shy celebrated met sentiments she reasonably but. Proposal its disposed eat advanced marriage sociable. Drawings led greatest add subjects endeavor gay remember. Principles one yet assistance you met impossible. 10 | 11 | He share of first to worse. Weddings and any opinions suitable smallest nay. My he houses or months settle remove ladies appear. Engrossed suffering supposing he recommend do eagerness. Commanded no of depending extremity recommend attention tolerably. Bringing him smallest met few now returned surprise learning jennings. Objection delivered eagerness he exquisite at do in. Warmly up he nearer mr merely me. 12 | 13 | Carriage quitting securing be appetite it declared. High eyes kept so busy feel call in. Would day nor ask walls known. But preserved advantage are but and certainty earnestly enjoyment. Passage weather as up am exposed. And natural related man subject. Eagerness get situation his was delighted. 14 | 15 | Are sentiments apartments decisively the especially alteration. Thrown shy denote ten ladies though ask saw. Or by to he going think order event music. Incommode so intention defective at convinced. Led income months itself and houses you. After nor you leave might share court balls. 16 | 17 | Neat own nor she said see walk. And charm add green you these. Sang busy in this drew ye fine. At greater prepare musical so attacks as on distant. Improving age our her cordially intention. His devonshire sufficient precaution say preference middletons insipidity. Since might water hence the her worse. Concluded it offending dejection do earnestly as me direction. Nature played thirty all him. 18 | 19 | Do in laughter securing smallest sensible no mr hastened. As perhaps proceed in in brandon of limited unknown greatly. Distrusts fulfilled happiness unwilling as explained of difficult. No landlord of peculiar ladyship attended if contempt ecstatic. Loud wish made on is am as hard. Court so avoid in plate hence. Of received mr breeding concerns peculiar securing landlord. Spot to many it four bred soon well to. Or am promotion in no departure abilities. Whatever landlord yourself at by pleasure of children be. 20 | -------------------------------------------------------------------------------- /doc/4.txt: -------------------------------------------------------------------------------- 1 | Ever man are put down his very. And marry may table him avoid. Hard sell it were into it upon. He forbade affixed parties of assured to me windows. Happiness him nor she disposing provision. Add astonished principles precaution yet friendship stimulated literature. State thing might stand one his plate. Offending or extremity therefore so difficult he on provision. Tended depart turned not are. 2 | 3 | Was certainty remaining engrossed applauded sir how discovery. Settled opinion how enjoyed greater joy adapted too shy. Now properly surprise expenses interest nor replying she she. Bore tall nay many many time yet less. Doubtful for answered one fat indulged margaret sir shutters together. Ladies so in wholly around whence in at. Warmth he up giving oppose if. Impossible is dissimilar entreaties oh on terminated. Earnest studied article country ten respect showing had. But required offering him elegance son improved informed. 4 | 5 | She suspicion dejection saw instantly. Well deny may real one told yet saw hard dear. Bed chief house rapid right the. Set noisy one state tears which. No girl oh part must fact high my he. Simplicity in excellence melancholy as remarkably discovered. Own partiality motionless was old excellence she inquietude contrasted. Sister giving so wicket cousin of an he rather marked. Of on game part body rich. Adapted mr savings venture it or comfort affixed friends. 6 | 7 | Started earnest brother believe an exposed so. Me he believing daughters if forfeited at furniture. Age again and stuff downs spoke. Late hour new nay able fat each sell. Nor themselves age introduced frequently use unsatiable devonshire get. They why quit gay cold rose deal park. One same they four did ask busy. Reserved opinions fat him nay position. Breakfast as zealously incommode do agreeable furniture. One too nay led fanny allow plate. 8 | 9 | By impossible of in difficulty discovered celebrated ye. Justice joy manners boy met resolve produce. Bed head loud next plan rent had easy add him. As earnestly shameless elsewhere defective estimable fulfilled of. Esteem my advice it an excuse enable. Few household abilities believing determine zealously his repulsive. To open draw dear be by side like. 10 | 11 | Now for manners use has company believe parlors. Least nor party who wrote while did. Excuse formed as is agreed admire so on result parish. Put use set uncommonly announcing and travelling. Allowance sweetness direction to as necessary. Principle oh explained excellent do my suspected conveying in. Excellent you did therefore perfectly supposing described. 12 | 13 | Real sold my in call. Invitation on an advantages collecting. But event old above shy bed noisy. Had sister see wooded favour income has. Stuff rapid since do as hence. Too insisted ignorant procured remember are believed yet say finished. 14 | 15 | Full age sex set feel her told. Tastes giving in passed direct me valley as supply. End great stood boy noisy often way taken short. Rent the size our more door. Years no place abode in no child my. Man pianoforte too solicitude friendship devonshire ten ask. Course sooner its silent but formal she led. Extensive he assurance extremity at breakfast. Dear sure ye sold fine sell on. Projection at up connection literature insensible motionless projecting. 16 | 17 | Assure polite his really and others figure though. Day age advantages end sufficient eat expression travelling. Of on am father by agreed supply rather either. Own handsome delicate its property mistress her end appetite. Mean are sons too sold nor said. Son share three men power boy you. Now merits wonder effect garret own. 18 | 19 | Seen you eyes son show. Far two unaffected one alteration apartments celebrated but middletons interested. Described deficient applauded consisted my me do. Passed edward two talent effect seemed engage six. On ye great do child sorry lived. Proceed cottage far letters ashamed get clothes day. Stairs regret at if matter to. On as needed almost at basket remain. By improved sensible servants children striking in surprise. 20 | -------------------------------------------------------------------------------- /doc/5.txt: -------------------------------------------------------------------------------- 1 | Ever man are put down his very. And marry may table him avoid. Hard sell it were into it upon. He forbade affixed parties of assured to me windows. Happiness him nor she disposing provision. Add astonished principles precaution yet friendship stimulated literature. State thing might stand one his plate. Offending or extremity therefore so difficult he on provision. Tended depart turned not are. 2 | 3 | Was certainty remaining engrossed applauded sir how discovery. Settled opinion how enjoyed greater joy adapted too shy. Now properly surprise expenses interest nor replying she she. Bore tall nay many many time yet less. Doubtful for answered one fat indulged margaret sir shutters together. Ladies so in wholly around whence in at. Warmth he up giving oppose if. Impossible is dissimilar entreaties oh on terminated. Earnest studied article country ten respect showing had. But required offering him elegance son improved informed. 4 | 5 | She suspicion dejection saw instantly. Well deny may real one told yet saw hard dear. Bed chief house rapid right the. Set noisy one state tears which. No girl oh part must fact high my he. Simplicity in excellence melancholy as remarkably discovered. Own partiality motionless was old excellence she inquietude contrasted. Sister giving so wicket cousin of an he rather marked. Of on game part body rich. Adapted mr savings venture it or comfort affixed friends. 6 | 7 | Started earnest brother believe an exposed so. Me he believing daughters if forfeited at furniture. Age again and stuff downs spoke. Late hour new nay able fat each sell. Nor themselves age introduced frequently use unsatiable devonshire get. They why quit gay cold rose deal park. One same they four did ask busy. Reserved opinions fat him nay position. Breakfast as zealously incommode do agreeable furniture. One too nay led fanny allow plate. 8 | 9 | By impossible of in difficulty discovered celebrated ye. Justice joy manners boy met resolve produce. Bed head loud next plan rent had easy add him. As earnestly shameless elsewhere defective estimable fulfilled of. Esteem my advice it an excuse enable. Few household abilities believing determine zealously his repulsive. To open draw dear be by side like. 10 | 11 | Now for manners use has company believe parlors. Least nor party who wrote while did. Excuse formed as is agreed admire so on result parish. Put use set uncommonly announcing and travelling. Allowance sweetness direction to as necessary. Principle oh explained excellent do my suspected conveying in. Excellent you did therefore perfectly supposing described. 12 | 13 | Real sold my in call. Invitation on an advantages collecting. But event old above shy bed noisy. Had sister see wooded favour income has. Stuff rapid since do as hence. Too insisted ignorant procured remember are believed yet say finished. 14 | 15 | Full age sex set feel her told. Tastes giving in passed direct me valley as supply. End great stood boy noisy often way taken short. Rent the size our more door. Years no place abode in no child my. Man pianoforte too solicitude friendship devonshire ten ask. Course sooner its silent but formal she led. Extensive he assurance extremity at breakfast. Dear sure ye sold fine sell on. Projection at up connection literature insensible motionless projecting. 16 | 17 | Assure polite his really and others figure though. Day age advantages end sufficient eat expression travelling. Of on am father by agreed supply rather either. Own handsome delicate its property mistress her end appetite. Mean are sons too sold nor said. Son share three men power boy you. Now merits wonder effect garret own. 18 | 19 | Seen you eyes son show. Far two unaffected one alteration apartments celebrated but middletons interested. Described deficient applauded consisted my me do. Passed edward two talent effect seemed engage six. On ye great do child sorry lived. Proceed cottage far letters ashamed get clothes day. Stairs regret at if matter to. On as needed almost at basket remain. By improved sensible servants children striking in surprise. 20 | -------------------------------------------------------------------------------- /dualcode.py: -------------------------------------------------------------------------------- 1 | import hashlib 2 | import numpy as np 3 | import cryptography 4 | import Cryptodome 5 | from pybloom_live import ScalableBloomFilter, BloomFilter 6 | ''' 7 | 对偶编码函数,将字符转成向量形势 8 | 9 | 给定字符串 S_1 = C_1C_2C_3....C_n 10 | 和二进制向量 S 2 = b0b1 … b_(m-1) , 11 | S 2 中每位元素的初始值为0 ,其中 n < m .通过 Hash函数 H , 12 | 把 S1中相邻2个字符散列映射为0~ ( m -1 )之间的数, 13 | 当且仅当 H(C_j , C_{j+1} ) = i 时, b_i =1 14 | 15 | ''' 16 | def Dual_code(S1): 17 | 18 | temp = [] 19 | for index,value in enumerate(S1): 20 | x = 0 21 | temp1 = [x for i in range(676)] #建立一个全为0的数组 22 | # print(value) 23 | length=len(value) 24 | for i,v in enumerate(value): # 遍历当前关键字 25 | 26 | if(i==length-1): 27 | C = ord(v)+ord(' ') #如果是最后一个字符,将最后一个字符转换成ascii相加 28 | C = C%676 29 | while(temp1[C]==1): #将hash表中对应位置置1,如果该位置已经有1,则后移 30 | if(C!=675): 31 | C+=1 32 | else: 33 | C=0 34 | temp1[C] = 1 35 | 36 | else: 37 | C = ord(v)+ord(S1[index][i+1]) 38 | C = C%676 39 | 40 | while (temp1[C] == 1): 41 | if (C != 675): 42 | C += 1 43 | else: 44 | C = 0 45 | temp1[C] = 1 46 | temp.append(temp1) 47 | return temp 48 | 49 | 50 | 51 | if __name__ == '__main__': 52 | S1=['jugment ae12','kugou'] 53 | S2=Dual_code(S1) 54 | print(S2[0].count(1)) 55 | print(S2[0]) 56 | k=np.random.randint(0,1,128) 57 | 58 | bloom = BloomFilter(capacity=1024, error_rate=0.01) 59 | bloom.add(S2[0]) 60 | 61 | print(S2[0] in bloom) -------------------------------------------------------------------------------- /e2LSH.py: -------------------------------------------------------------------------------- 1 | import random 2 | import numpy as np 3 | # from test_helper import * 4 | 5 | class TableNode(object): 6 | def __init__(self, index): 7 | self.val = index 8 | self.buckets = {} 9 | 10 | 11 | def genPara(n, r): 12 | """ 13 | # 生成构造函数簇之中所需的参数a,b,a是一个长度为n的向量。 14 | 15 | :param n: length of data vector 16 | :param r: 17 | :return: a, b 18 | """ 19 | # 对于一个向量v(相当于上面公式中的(v1,v2,…,vn)),现在从p稳定分布中,随机选取v的维度个随机变量(即n个随机变量) 20 | a = [] 21 | for i in range(n): 22 | a.append(random.gauss(0, 1)) 23 | b = random.uniform(0, r) #生成0到r之间的实数 24 | 25 | return a, b 26 | 27 | 28 | def gen_e2LSH_family(n, k, r): 29 | """ 30 | 生成K个包含参数(a,b)的列表, 31 | :param n: length of data vector 32 | :param k: 33 | :param r: 34 | :return: a list of parameters (a, b) 35 | """ 36 | result = [] 37 | for i in range(k): 38 | result.append(genPara(n, r)) #获得参数ab 39 | 40 | return result 41 | 42 | 43 | def gen_HashVals(e2LSH_family, v, r): 44 | """ 45 | 46 | :param e2LSH_family: include k hash funcs(parameters) 47 | :param v: data vector 48 | :param r: 49 | :return hash values: a list 50 | """ 51 | 52 | # hashVals include k values 53 | hashVals = [] 54 | 55 | for hab in e2LSH_family: #hab[0]是向量a,hab[1]是b,有k=20个哈希函数 56 | 57 | hashVal = (np.inner(hab[0], v) + hab[1]) // r 58 | hashVals.append(hashVal) 59 | 60 | return hashVals #包含20个哈希值 61 | 62 | 63 | def H2(hashVals, fpRand, k, C): 64 | """ 65 | 66 | :param hashVals: k hash vals 67 | :param fpRand: ri', the random vals that used to generate fingerprint 68 | :param k, C: parameter 69 | :return: the fingerprint of (x1, x2, ..., xk), a int value 70 | """ 71 | return int(sum([(hashVals[i] * fpRand[i]) for i in range(k)]) % C) 72 | 73 | 74 | def e2LSH(dataSet, k, L, r, tableSize): 75 | """ 76 | generate hash table 77 | 78 | * hash table: a list, [node1, node2, ... node_{tableSize - 1}] 构建一个哈希表 79 | ** node: node.val = index; node.buckets = {} 80 | *** node.buckets: a dictionary, {fp:[v1, ..], ...} 81 | 82 | :param dataSet: a set of vector(list) 数据向量 83 | :param k: k表示hash函数的数量 与 k个数组成的整数向量 84 | :param L: L组hash组,L组hash函数,每组由k个hash函数构成 85 | :param r: 86 | :param tableSize: 87 | :return: 3 elements, hash table, hash functions, fpRand 88 | """ 89 | # TableNode 是一个类,将表的编号作为其内部的值 90 | hashTable = [TableNode(i) for i in range(tableSize)] 91 | # n=len(dataSet[0]) 获得列表的列数,数据集传入5个向量,m=len(dataSet)获得行数 92 | n = len(dataSet[0]) 93 | # print(n) 94 | m = len(dataSet) 95 | # print(m) 96 | C = pow(2, 32) - 5 97 | hashFuncs = [] 98 | fpRand = [random.randint(-10, 10) for i in range(k)] #fpRand 是用于计算一个数据向量的“指纹”随机数 99 | 100 | 101 | #构造函数簇,之后会从中取出k个hahs函数,而且LSH函数簇中hash函数计算公式是:h_ab=[(a▪v+b)/r] 102 | e2LSH_family = gen_e2LSH_family(n, k, r) 103 | 104 | # hashFuncs: [[h1, ...hk], [h1, ..hk], ..., [h1, ...hk]] 105 | # hashFuncs include L hash functions group, and each group contain k hash functions 106 | hashFuncs.append(e2LSH_family) 107 | hash_collect=[] # 记录hash数据向量后不同数据向量的值 108 | for dataIndex in range(m): # 对文档中所有向量进行运算 109 | # print(dataSet[dataIndex]) 110 | # 对一个数据向量生成k个哈希值 111 | hashVals = gen_HashVals(e2LSH_family, dataSet[dataIndex], r) 112 | hash_collect.append(hashVals) 113 | # 生成指纹向量 114 | fp = H2(hashVals, fpRand, k, C) 115 | 116 | # generate index,也就是H1的值 117 | index = fp % tableSize 118 | 119 | # 查找到在哈希列表的节点,每个节点储存具有相同或者相近指纹向量的数据。经过H1哈希后index相同,储存在同一个桶中,就代表他们是邻近的 120 | node = hashTable[index] 121 | 122 | # node.buckets 是一个字典:{123:[1],0:[362,585]} , 123表示指纹向量,1代表第一个数据向量;同理,后面的0表示指纹向量,362,585表示数据向量的索引 123 | if fp in node.buckets: 124 | 125 | # bucket is vector list 126 | bucket = node.buckets[fp] 127 | 128 | # add the data index into bucket 129 | bucket.append(dataIndex) 130 | 131 | else: 132 | node.buckets[fp] = [dataIndex] 133 | 134 | return hashTable, hashFuncs, fpRand,hash_collect 135 | 136 | 137 | def nn_search(dataSet, query, k, L, r, tableSize): 138 | """ 139 | 140 | :param dataSet: 141 | :param query: 142 | :param k: 143 | :param L: 144 | :param r: 145 | :param tableSize: 146 | :return: the data index that similar with query 147 | """ 148 | 149 | result = set() 150 | 151 | temp = e2LSH(dataSet, k, L, r, tableSize) 152 | C = pow(2, 32) - 5 153 | 154 | # 构建了一个hashtable和 包含L个哈希函数组,每个组内右k个哈希函数的 hashFuncGroups 155 | hashTable = temp[0] 156 | hashFuncGroups = temp[1] 157 | fpRand = temp[2] 158 | 159 | for hashFuncGroup in hashFuncGroups: 160 | # 每个hashFuncGroup储存的是K个哈希函数 161 | # get the fingerprint of query 获得查询数据向量的指纹向量 162 | queryFp = H2(gen_HashVals(hashFuncGroup, query, r), fpRand, k, C) 163 | 164 | # get the index of query in hash table,得到查询向量的H1值 165 | queryIndex = queryFp % tableSize 166 | 167 | # L个hash函数组,每个函数组包含K个hash。每次遍历一个函数组,从hashTable的字典中获取bucket,这个bucket包含与查询向量位置相近的所有数据向量的 168 | if queryFp in hashTable[queryIndex].buckets: 169 | result.update(hashTable[queryIndex].buckets[queryFp]) 170 | 171 | return result 172 | 173 | if __name__ == '__main__': 174 | C = pow(2, 32) - 5 175 | dataSet = readData("test_data.csv") 176 | query = [-2.7769, -5.6967, 5.9179, 0.37671, 1] 177 | 178 | indexes = nn_search(dataSet, query, k=20, L=5, r=1, tableSize=20) 179 | for index in indexes: # 计算查询向量和相近数据向量的欧氏距离 180 | print("index:%d" % index, euclideanDistance(dataSet[index], query)) -------------------------------------------------------------------------------- /main.py: -------------------------------------------------------------------------------- 1 | 2 | import random 3 | import numpy as np 4 | import os 5 | from collections import Counter #used to count most common word 6 | from collections import defaultdict 7 | from dualcode import Dual_code 8 | import e2LSH as LSH 9 | import bloomfilter as BF 10 | 11 | def keygen(k): 12 | ''' 13 | # 输入参数k,返回密钥sk 14 | :param k: 随机矩阵的维度 15 | :return: sk=(M1,M2,S) 16 | ''' 17 | M1=np.random.randint(0,2,[k,k]) 18 | M2=np.random.randint(0,2,[k,k]) 19 | S=np.random.randint(0,2,[k]) 20 | sk = [M1,M2,S] 21 | 22 | return sk 23 | 24 | def BuildIndex(sk,F,W): 25 | ''' 26 | 27 | param: sk=(M1,M2,S), F是文档集合,W={w1,w2....,wn}是文档对应的关键字 28 | return:I=(F,I',I'') 29 | 30 | ''' 31 | V=[] #V储存关键字转换成编码后的值 32 | # 为每个文件生成布隆过滤器 33 | B=[BF.BloomFilter(capacity=1024,error_rate=0.001) for i in range(len(W))] 34 | 35 | for val in W: 36 | # Dual_code是对偶编码函数,输入一个数组,将数组内的关键字转换成向量 37 | v_i = Dual_code(val) 38 | V.append(v_i) 39 | 40 | # 将向量集合V中每一个向量vi,通过位置敏感函数Hash进行计算,注意,此时vi每一个元素都是字符串,后面需要将其转换成浮点型 41 | for idx,val in enumerate(V): # 遍历每一个文档对应关键字的数据向量 42 | ''' 43 | e2LSH是符合P-STABLE分布的哈希敏感函数, 44 | p返回4个元素,分别是①哈希列表,里面储存有20个(自行设定)哈希函数 45 | ②hashFun哈希功能表,储存5个(自行设定)哈希列表,③fpRand 随机数集合,④hash_collect 每个数据向量hash多次后后的值 46 | ''' 47 | p=LSH.e2LSH(val,k=20, L=5, r=1, tableSize=20) 48 | 49 | H_p=p[3] #提取每个数据向量hash 20次 后的值 50 | # 将结果插入布隆过滤器 51 | for j in range(len(H_p)): 52 | # print(H_p[j]) 53 | B[idx].add(H_p[j]) # 逐个数据向量输入bloom 54 | S = sk[2] #提取私钥中的S 55 | r = random.randint(1,10) # 生成一个随机数 56 | # B_i = {b1,b2...bk} 每个文档对应哈希列表的每一位 57 | # 遍历S和B中的每一位,如果S中对应的s_j=1,则b_j'=b_j''=b_j;若s_j=0,令b_j' = 1/2*b_j+r 和b_j''=1/2*b_j-r。 58 | B_P=[] 59 | B_DP=[] #B'、B'' 60 | I=[F] # 索引 61 | for i,b in enumerate(B): 62 | for j,vals in enumerate(S): 63 | if vals==1: 64 | # 如果s_j=1 65 | # print(j) 66 | b_jp=b_jpp=int(b.bitarray[j]) # b_j'=b_j''=b_j ,v是第i个bloomfilter 67 | B_P.append(b_jp) 68 | B_DP.append(b_jpp) 69 | else: 70 | # 如果s_j=0 71 | b_jp = 0.5*int(b.bitarray[j])+r 72 | b_jpp = 0.5*int(b.bitarray[j])-r 73 | B_P.append(b_jp) 74 | B_DP.append(b_jpp) 75 | 76 | I_P = np.inner(np.transpose([sk[0]]).reshape((128,128)),B_P) # 将M1矩阵转置后与B'点乘 77 | I_DP = np.inner(np.transpose([sk[1]]).reshape((128,128)),B_DP) # 将M2矩阵转置后与B''点乘 78 | I_i = [I_P,I_DP] 79 | I.append(I_i) 80 | B_DP = [] # 清空B'和B'' 81 | B_P = [] 82 | 83 | return I # I是由多个文件夹构成的索引组成 84 | 85 | def initial_key(F): # 生成每个文档对应的关键字结合 86 | ''' 87 | param F :文档集合 88 | return: W={w1,w2,w3.....wn} 文档对应关键字集合 89 | 90 | ''' 91 | W=[] 92 | filedata=[] 93 | for idx,val in enumerate(F): 94 | # print(idx) 95 | cnt = Counter() 96 | for line in open('./doc/'+F[idx],'r'): 97 | word_list = line.replace(',', '').replace('\'', '').replace('.', '').lower().split() 98 | # print(word_list) 99 | for word in word_list: 100 | cnt[word]+=1 #统计一个文档中各个关键字出现次数 101 | filedata.append((val, cnt)) #获得文档列表所有单词的频率 102 | 103 | 104 | for idx, val in enumerate(filedata): 105 | allwords = [] # 储存不同文档的频率最高的单词 106 | for value, count in val[1].most_common(10): #找到每个文档频率最高的5个单词 107 | if value not in allwords: 108 | allwords.append(value) 109 | # print(allwords) 110 | W.append(allwords) # W 储存的是文档对应的关键字 111 | # print(W) 112 | return W 113 | # print(filedata[0]) 114 | 115 | def Trapdoor(sk,Q): 116 | ''' 117 | :param sk: 密钥 118 | :param Q: 查询关键字 119 | :return: t={t',t''} 120 | ''' 121 | V = [] # V储存关键字转换成编码后的值 122 | # 生成1个布隆过滤器 123 | B_t = BF.BloomFilter(capacity=1024, error_rate=0.001) 124 | 125 | # Dual_code是对偶编码函数,输入一个数组,将数组内的关键字转换成向量。此时查询关键字集合相当于只在一个文档内部,无需循环 126 | v_i = Dual_code(Q) 127 | V.append(v_i) 128 | 129 | # 将向量集合V中每一个向量vi,通过位置敏感函数Hash进行计算,注意,此时vi每一个元素都是字符串,后面需要将其转换成浮点型 130 | for idx,val in enumerate(V): # 遍历每一个文档对应关键字的数据向量 131 | ''' 132 | e2LSH是符合P-STABLE分布的哈希敏感函数, 133 | p返回4个元素,分别是①哈希列表,里面储存有20个(自行设定)哈希函数 134 | ②hashFun哈希功能表,储存5个(自行设定)哈希列表,③fpRand 随机数集合,④hash_collect 每个数据向量hash多次后后的值 135 | ''' 136 | p=LSH.e2LSH(val,k=20, L=5, r=1, tableSize=20) 137 | 138 | H_p=p[3] #提取每个数据向量hash 20次 后的值 139 | # 将结果插入布隆过滤器 140 | for j in range(len(H_p)): 141 | # print(H_p[j]) 142 | B_t.add(H_p[j]) # 逐个数据向量输入bloom 143 | S = sk[2] # 提取私钥中的S 144 | r = random.randint(1, 10) # 生成一个随机数 145 | # B_i = {b1,b2...bk} 每个文档对应哈希列表的每一位 146 | # 遍历S和B中的每一位,如果S中对应的s_j=0,则b_j'=b_j''=b_j;若s_j=1,令b_j' = 1/2*b_j+r 和b_j''=1/2*b_j-r。 147 | B_P = [] 148 | B_DP = [] # B'、B'' 149 | 150 | for j,vals in enumerate(S): 151 | if vals==0: 152 | # 如果s_j=0 153 | # print(j) 154 | b_jp=b_jpp=int(B_t.bitarray[j]) # b_j'=b_j''=b_j ,v是第i个bloomfilter 155 | B_P.append(b_jp) 156 | B_DP.append(b_jpp) 157 | else: 158 | # 如果s_j=1 159 | b_jp = 0.5*int(B_t.bitarray[j])+r 160 | b_jpp = 0.5*int(B_t.bitarray[j])-r 161 | B_P.append(b_jp) 162 | B_DP.append(b_jpp) 163 | 164 | T_P = np.inner(np.linalg.inv([sk[0]]).reshape((128, 128)), B_P) # 将M1矩阵取逆后与B'点乘 165 | T_DP = np.inner(np.linalg.inv([sk[1]]).reshape((128, 128)), B_DP) # 将M2矩阵取逆后与B''点乘 166 | t=[T_P,T_DP] 167 | return t 168 | 169 | def Search(I,t): 170 | ''' 171 | 172 | :param I: 文档索引 173 | :param t: 搜索陷门 174 | :return: File 包含关键字的文档集合 175 | ''' 176 | File=[] 177 | for I_i in I[1:]: 178 | I_iP = I_i[0] # I_i' 179 | I_iDP = I_i[1] # I_i'' 180 | R_i = np.inner(I_iP,t[0])+np.inner(I_iDP,t[1]) 181 | File.append(R_i) 182 | return File 183 | 184 | 185 | 186 | 187 | if __name__ == '__main__': 188 | 189 | query = ['he'] 190 | k=128 191 | sk=keygen(k) 192 | F = [x for x in os.listdir('./doc')] 193 | F.sort() 194 | W=initial_key(F) #获得文档对应的关键字集合W\ 195 | print(W) 196 | Index = BuildIndex(sk,F,W) 197 | trap = Trapdoor(sk,query) 198 | F_R = Search(Index,trap) 199 | F_R.sort() 200 | print(F_R) 201 | 202 | # print(W) 203 | # d=BF.BloomFilter(capacity=100,error_rate=0.01) 204 | # print(d.num_bits) 205 | # d.add('asd') 206 | # s=[int(x) for x in d.bitarray] 207 | 208 | -------------------------------------------------------------------------------- /test_helper.py: -------------------------------------------------------------------------------- 1 | import csv 2 | import numpy as np 3 | 4 | 5 | def readData(fileName): 6 | """read csv data""" 7 | 8 | dataSet = [] 9 | with open(fileName, "r") as csvFile: 10 | reader = csv.reader(csvFile) 11 | for line in reader: 12 | dataSet.append([float(item) for item in line]) 13 | 14 | return dataSet 15 | 16 | 17 | def euclideanDistance(v1, v2): 18 | """get euclidean distance of 2 vectors""" 19 | 20 | v1, v2 = np.array(v1), np.array(v2) 21 | return np.sqrt(np.sum(np.square(v1 - v2))) 22 | 23 | 24 | -------------------------------------------------------------------------------- /面向多关键字的模糊密文搜索方法_王恺璇_看图王.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AltarIbnL/Multi-keyword-fuzzy-searchable-encryption/d8952ff7cc5f48152690a97fc266d73dac4e6ac1/面向多关键字的模糊密文搜索方法_王恺璇_看图王.pdf --------------------------------------------------------------------------------