User:Ewart Shaw/Crypt

From J Wiki
Jump to: navigation, search

Homegrown Encrypt/decrypt

I have many different passwords for online accounts, and can never remember which is which, particularly as so few websites allow individual 'hints'. Therefore I created a script to encrypt passwords, using a master password. The encryption pseudorandomly alters each character, pseudorandomly permutes the text, and the encryption varies with timestamp (so encryptions 1 millisecond apart look very different).

Others might find the functions useful. Please note that pseudorandom number generators, even the Mersenne twister, are generally not recommended for encryption. Hence the following all-encompassing disclaimer.

Disclaimer: I am not responsible.

NB. Encrypting functions
NB. Ewart Shaw 26-Oct-2004  (last modified 30-July-2011)
NB. =========================================================

encrypt=: 3 : 0
PASSWD encrypt y
:
rng=. saverng''
9!:43 ] 2
'a b'=. seeds x
y=. cvauto cvfasc y
9!:1 b
y=. perm y
9!:1 a
a=. wrap a64fcv submod y
restorerng rng
a
)

decrypt=: 3 : 0
PASSWD decrypt y
:
rng=. saverng''
9!:43 ] 2
'a b'=. seeds x
9!:1 a
y=. addmod cvfa64 unwrap y
9!:1 b
y=. unperm y
restorerng rng
ascfcv cvunauto y
)

NB. seeds=: (10x^9)"_ | 64x"_ #. a."_ i. ] ,: |.
seeds=: (10x^9) | [: |: (64x +i. 6) #."0 0 _ a."_ i. ] ,: |.

passwd=: 3 : 'PASSWD=: y'
hint=: 3 : '''insert your hint here'',LF'
passwd''   NB. initialise to empty password

NB. =========================================================
NB.* saverng  v  save RNG state

saverng=: (9!:44 ; 9!:0 ; 9!:42)
restorerng=: 3 : 0
 'state seed type'=. Y=: y
 9!:43 type
 9!:1 seed
 9!:45 state
'RNG restored'
)

NB. =========================================================
NB.* ascfcv  v  ASCII text from crypt vector
NB.* cvfasc  v  crypt vector from ASCII text
NB.* cvfts   v  crypt vector from time stamp

cvfts=: (6#64) #: 0 60 60 24 31 12 #. [: |. 1 1 1 1 1 36 <.@* 6!:0

cvfasc=: 3 : 0
n0=. n1 -~ 3 * n3=. >. 3 %~ n1=. >: n=. #y
pre=. (n0 + 3 * ? 84), ? n0#255
, (4#64) #: 255 #. (n3,3) $ pre, a. i. y
)

ascfcv=: 3 : 0
n4=. <. 4 %~ n=. # y
a=. , (3#255) #: 64 #. (n4,4) $ y
a. {~ (>: 3 | {.a) }. a
)

NB. =========================================================
NB.* a64fcv  v  ASCII (subset of 64 chars) from crypt vector
NB.* addmod  v  add random increments & take mod (default 64)
NB.* cvfa64  v  reverse a64fcv
NB.* perm    v  apply random permutation
NB.* submod  v  reverse addmod
NB.* unperm  v  reverse perm
NB.* unwrap  v  reverse wrap for ASCII text with no ' ' or LF
NB.* wrap    v  wrap y to matrix with x columns
NB.

perm=: (# ? #) { ]
unperm=: ([: /: # ? #) { ]

addmod=: 64&addmod : ([ | ] + [: ? #@] # [)
submod=: 64&submod : ([ | ] - [: ? #@] # [)

wrap=: 3 : 0
64 wrap y
:
n=. # y=. ,y
r=. >. n%x
(r,x) $ (r*x) {. y
)
unwrap=: , -. (' ',LF)"_

a64fcv=: {&(a. {~ 46 58, (, 65 97 +/ i.26), 48 + i. 10)
cvfa64=: (a. {~ 46 58, (, 65 97 +/ i.26), 48 + i. 10)&i.

NB. =========================================================
NB.* cvauto    v  auto-obscure crypt vector
NB.* cvunauto  v  reverse cvauto
NB.

cvauto=: 3 : 0
9!:43 ] 2
9!:1 seed=. cvfts''
y=. addmod y
9!:1 seed
seed, perm y
)

cvunauto=: 3 : 0
9!:43 ] 2
9!:1 seed=. 6 {. y
y=. unperm 6 }. y
9!:1 seed
submod y
)

NB. =========================================================
NB. Example, encoded with password   ,'s Bolero
NB. try:
NB.    passwd ',''s Bolero'
NB.    ] ORIG=. decrypt CODED
NB.    encrypt ORIG
NB.    encrypt ORIG
NB. Note that all encrypts are different (depends on time stamp)

hint=: 'Sarajevo'"_

CODED=: }: 0 : 0
qni4edbS3:.wncosoLGZSaWbw3J9tXXr9mc96SoERDZNn8WqTHUviBhIZz8Wf4wA
BQ6R4grMBI5.pDNUIcW5o:XPUwX5ZdYCVil5WW1MVD9ryReZUKb2NeuYQhICi3Ju
rhEREndRb2S:7m7GiXUXcZ1jWdZ5A9CawZzU:KB112YeN56ccua:c2MO.eMxAyML
GCDdSypDMoF3Nn4bfmDQtzCDxs1lp.lm:mYxSGfpNZbpKOkmYA99L90JLm3sMujr
Nj.rRVDcyI:xWutNSK0L5Lf:VG5U9CmQ2XYWEpDc4a7n3nI7FcK:nfMVWrR9RoC5
ngjHtzuxnzHVuHWmJakrmrYulZtt8BXWYZpD018HcaBmezDeXW:L7Rqf5QJgkDb5
wzuiewMNJWTrgdEPdhoqwdLqE5HUmdpLOvZ8yu1EX2KY.x6Vo9pDEZzH0PGm5pBS
POkl7A2M5pADYoTY5mweuYsDbErOYvn68JL.9vC5.jZ0N63ukq55iFIkOw9BRL4v
R9UhsKeE6BkyI0gAbrqjjQULBqlnmmUVRGOoMdk5GQNy9JmhkFA.qIvfYepIUxfQ
3I2hzU1bo7.StOmEWogplvfurR.qCtttbMBhvGdfJQGu7afsxbFg1380CilK2sSO
fGj:1MecPnmrNgIAn7GqAtNQKo09ZB7zWINhZwgAvPqMyFT142CYwfcun0MqmPsL
ajvDxtdrExvOuwsDP:o9S74urWbPC3y2Qlx440:I:9r7NR83VKjL6PeO6feXUg8E
:XDUiA4.vCSUbAsINGShbScuo06EIqEUoosY.u3zZsoEwFINmwFIJtqZ.n4dfLSw
lu9asFw.8MY26bawiHg3Qln2rq7YeL6KL00FheYWYQ2c.j3NQ4YD8zq0IX721VW1
i2IW:P0zt.JOmm3kYuThLpcdhBuyP8IqG705RE43rhbOl86JhczuTqL6ud0JG.Ew
GfJbYV3In:fNxyoLBTIlCIPSZh06u2hvWVFMXfpYnS844eaGdAiItRXIB4RAUwN:
3P:Sh5qx4aM3vk0FpXKM:qXdpw
)

NB. =========================================================
NB. describe on loading

DESCRIBE=: 0 : 0
NB. to encrypt/decrypt text
passwd 'whatever'   NB. set password to whatever
hint''              NB. something to remind you of your password
encrypt TEXT        NB. produce encrypted text (e.g. to be cut+pasted)
decrypt CODED       NB. produce original text from coded version
)

1!:2&2 ] DESCRIBE