|
mok-kong shen
|
Jan 21 2014, 05:19 PM
Post #1
|
- Posts:
- 1,874
- Group:
- Members
- Member
- #3,310
- Joined:
- December 12, 2009
|
- Code:
-
#[space]Conversion[space]between[space]integer[space]and[space]its[space]multiple-base[space]representation.
#[space]Version[space]1.1,[space]released[space]on[space]24.03.2014.
#[space]Update[space]notes:
#[space]Version[space]1.0:[space]Released[space]on[space]21.01,2014.
#[space]Version[space]1.1,[space]22.03,2014:[space]The[space]designations[space]in[space]the[space]formula[space]modified[space]and[space]the #[space]binary[space]bit[space]representation[space]discarded.
#[space]Version[space]1.1.1,[space]24.03.2014:[space]Correction[space]for[space]I=0.
#[space]Code[space]lines[space]of[space]the[space]same[space]date[space]are[space]always[space]identical.[space]There[space]may[space]however[space]be[space]interim #[space]modifications[space]of[space]comment[space]lines.[space]The[space]most[space]recent[space]document[space]of[space]this[space]software[space]can #[space]be[space]obtained[space]from:[space]http://s13.zetaboards.com/Crypto/topic/7149584/1/
#[space]Email[space]address[space]of[space]the[space]author:[space]mok-kong.shen@t-online.de
#[space]Prologue.
#[space]As[space]is[space]well-known,[space]the[space]common[space](unique)[space]representation[space]of[space]a[space]non-negative[space]integer #[space]I[space]as[space]a[space]sum[space]of[space]terms[space]involving[space]increasing[space]powers[space]of[space]a[space]base[space]value[space]b[space]can[space]be #[space]generalized[space]into[space]the[space]following[space]form[space]where[space]a[space]set[space]of[space](arbitrary)[space]different[space]base #[space]values[space]bi[space]are[space]used[space]instead[space](our[space]designations[space]may[space]differ[space]from[space]the[space]literature):
#[space]I[space]=[space]a0[space]+[space]a1*b0[space]+[space]a2*b0*b1[space]+[space]a3*b0*b1*b2[space]+[space].......[space]+[space]am*b1*b2*b3...*b_(m-1)
#[space]with[space]ai[space]<[space]bi[space]and[space]bi[space]>=[space]2.[space](Note[space]that[space]am[space]is[space]not[space]constrained[space]and[space]could[space]be[space]large, #[space]if[space]the[space]user-given[space]list[space]of[space]the[space]base[space]values[space]bi[space]is[space]not[space]sufficiently[space]long.)
#[space]baselist[space]is[space]a[space]list[space]to[space]be[space]specified[space]as[space][b0,[space]b1,[space]b2,[space]b3,[space].....[space]b_(m-1)]
def[space]initmultbase(): [space][space]global[space]baselist,lblist,prd [space][space]for[space]bi[space]in[space]baselist: [space][space][space][space]assert(bi>=2) [space][space]lblist=len(baselist) [space][space]s=1 [space][space]prd=[s] [space][space]for[space]i[space]in[space]range(0,lblist): [space][space][space][space]bi=baselist[i] [space][space][space][space]assert(bi>=2) [space][space][space][space]s*=bi [space][space][space][space]prd+=[s] [space][space]return
#[space]Convert[space]an[space]integer[space]to[space]its[space]multiple-base[space]representation[space]as[space]a[space]list #[space][a0,[space]a1,[space]a2,[space].....,[space]am].
def[space]convintegertomultbaserep(integer): [space][space]global[space]baselist,lblist,prd [space][space]assert(integer>=0) [space][space]g=integer [space][space]if[space]g==0: [space][space][space][space]return[0] [space][space]multbaserep=[] [space][space]for[space]i[space]in[space]range(lblist,0,-1): [space][space][space][space]d=prd[i] [space][space][space][space]ai,g=divmod(g,d) [space][space][space][space]multbaserep+=[ai] [space][space]multbaserep+=[g] [space][space]while[space]multbaserep[0]==0: [space][space][space][space]del[space]multbaserep[0] [space][space]multbaserep.reverse() [space][space]return(multbaserep)
#[space]The[space]reverse[space]of[space]convintegertomultbaserep()
def[space]convmultbasereptointeger(multbaserep): [space][space]global[space]baselist,lblist,prd [space][space]integer=0 [space][space]lmbrep=len(multbaserep) [space][space]for[space]i[space]in[space]range(lmbrep): [space][space][space][space]ai=multbaserep[i] [space][space][space][space]if[space]i<lmbrep-1: [space][space][space][space][space][space]assert(ai<baselist[i]) [space][space][space][space]integer+=ai*prd[i] [space][space]return(integer)
#[space]An[space]illustrative[space]example.
#[space]Specification[space]of[space]the[space]base[space]values. baselist=[5,3,2,3,4]
#[space]Initialization[space]of[space]the[space]system. initmultbase()
integer=352
multbaserep=convintegertomultbaserep(integer)
integer1=convmultbasereptointeger(multbaserep)
print("Multiple[space]base[space]representation[space]example:") print(integer,multbaserep,integer1) print()
##############################################################
#[space]Appendix.
#[space]For[space]the[space]common[space]base[space]representation[space]with[space]base[space]value[space]b:
#[space]I[space]=[space]a0[space]+[space]a1*b[space]+[space]a2*b^2[space]+[space]a3*b^3[space]+[space].......
#[space]we[space]have[space]the[space]following[space]corresponding[space]functions:
def[space]convintegertocommonbaserep(base,integer): [space][space]assert(base>=2[space]and[space]integer>=0) [space][space]g=integer [space][space]if[space]g==0: [space][space][space][space]return[0] [space][space]commonbaserep=[] [space][space]while[space]g>0: [space][space][space][space]g,a=divmod(g,base) [space][space][space][space]commonbaserep+=[a] [space][space]return(commonbaserep)
def[space]convcommonbasereptointeger(base,commonbaserep): [space][space]assert(base>=2) [space][space]integer=0 [space][space]prd=1 [space][space]for[space]i[space]in[space]range(len(commonbaserep)): [space][space][space][space]a=commonbaserep[i] [space][space][space][space]integer+=a*prd [space][space][space][space]prd*=base [space][space]return(integer)
#[space]An[space]illustrative[space]example.
integer=352
commonbaserep=convintegertocommonbaserep(9,integer)
integer1=convcommonbasereptointeger(9,commonbaserep)
print("Common[space]base[space]representation[space]example:") print(integer,commonbaserep,integer1)
|
|
WTShaw
|
Jan 21 2014, 08:57 PM
Post #2
|
- Posts:
- 80
- Group:
- Members
- Member
- #4,006
- Joined:
- January 1, 2014
|
As this is interesting, the evaluation of some values in binary can be rather self defeating since the particulars of some bases are farm more interesting than in binary form. Remember that the bit is not the basic value of everything, just multiples of bits. Left in other realms, more work might be accomplished with less effort. A comparison of base qualities in use is rather in order, and that is not so difficult, just easily overlooked.
Don't get me wrong, binaries are sometimes available options to be studied, but best only as embodied as several bit bases at various levels, 16, 64, etc.
|
|
mok-kong shen
|
Jan 21 2014, 09:35 PM
Post #3
|
- Posts:
- 1,874
- Group:
- Members
- Member
- #3,310
- Joined:
- December 12, 2009
|
@WTSHAW, The integers you obtained are also derived from binary bits obtained from the coding values of the characters of the plaintext according to the ordering in the alphabet. The scheme I implemented here is a generalization and not different in principle. Because the representation formula now does not consist of merely powers of a single base value b, one has to do a bit more complicated programming. Since there is now a set of (secret) b values instead of a single one, the task of the analyst would also be harder.
|