Skip to main content
added lines and words, removed characters
Source Link
jsvnm
  • 441
  • 4
  • 6

Ruby (200200 185)

sample runs + wc:

$ ruby onetimepad.rb 
ENCRYPTENCODE
AAZZHELLOWORLDANOTHERTESTINPUTZZZ
CJGTEFEDXEZUERZYCLGHDWLDASFUTHWKC
CJFSLJPOLANLPUBPMIBXOXTPTQIVBMDPX
$ ruby onetimepad.rb 
DECRYPTDECODE
CJGTEFEDXEZUERZYCLGHDWLDASFUTHWKC
CJFSLJPOLANLPUBPMIBXOXTPTQIVBMDPX
AAZZHELLOWORLDANOTHERTESTINPUTZZZ
$ wc onetimepad.rb
       24       37     200185 onetimepad.rb
f=->k,p,o{p.scan(r=/./).zip(k.scan r).map{|a|a.map{|b|b.ord-65}.inject(o)}.map{|c|(c%26+65).chr}*''}
puts(gets[0]==?D?(k=gets;f[k,gets,:-]):[k=(2..(p=gets).size).map{(65+rand(26)).chr}*'',f[k,p,:+]])

and with newlines and indents (still not very readable)

f=->k,p,o{
  p.scan(r=/./).zip(k.scan r).map{|a|
    a.map{|b|b.ord-65}.inject(o)
  }.map{|c|
    (c%26+65).chr
  }*''
}
puts(gets[0]==?D ?
     (k=gets;f[k,gets,:-])
    : [k=(2..(p=gets).size).map{
         (65+rand(26)).chr
       }*'',
       f[k,p,:+]])
def f;gets.scan(/./).map{|b|b.ord-65};end
s=->a{a.map{|b|(b+65).chr}*''}
r=->b,a,o{s[a.zip(b).map{|a,b|(a.send o,b)%26}]}
puts(gets=~/^D/?r[f,f,:+]:[s[k=(p=f).map{rand 26}],r[k,p,:-]])

Ruby (200)

sample runs + wc:

$ ruby onetimepad.rb 
ENCRYPT
AAZZHELLOWORLD
CJGTEFEDXEZUER
CJFSLJPOLANLPU
$ ruby onetimepad.rb 
DECRYPT
CJGTEFEDXEZUER
CJFSLJPOLANLPU
AAZZHELLOWORLD
$ wc onetimepad.rb
       2       3     200 onetimepad.rb
f=->k,p,o{p.scan(r=/./).zip(k.scan r).map{|a|a.map{|b|b.ord-65}.inject(o)}.map{|c|(c%26+65).chr}*''}
puts(gets[0]==?D?(k=gets;f[k,gets,:-]):[k=(2..(p=gets).size).map{(65+rand(26)).chr}*'',f[k,p,:+]])

and with newlines and indents (still not very readable)

f=->k,p,o{
  p.scan(r=/./).zip(k.scan r).map{|a|
    a.map{|b|b.ord-65}.inject(o)
  }.map{|c|
    (c%26+65).chr
  }*''
}
puts(gets[0]==?D ?
     (k=gets;f[k,gets,:-])
    : [k=(2..(p=gets).size).map{
         (65+rand(26)).chr
       }*'',
       f[k,p,:+]])

Ruby (200 185)

sample runs + wc:

$ ruby onetimepad.rb
ENCODE
ANOTHERTESTINPUTZZZ
ZYCLGHDWLDASFUTHWKC
BPMIBXOXTPTQIVBMDPX
$ ruby onetimepad.rb
DECODE
ZYCLGHDWLDASFUTHWKC
BPMIBXOXTPTQIVBMDPX
ANOTHERTESTINPUTZZZ
$ wc onetimepad.rb
       4       7     185 onetimepad.rb
def f;gets.scan(/./).map{|b|b.ord-65};end
s=->a{a.map{|b|(b+65).chr}*''}
r=->b,a,o{s[a.zip(b).map{|a,b|(a.send o,b)%26}]}
puts(gets=~/^D/?r[f,f,:+]:[s[k=(p=f).map{rand 26}],r[k,p,:-]])
Source Link
jsvnm
  • 441
  • 4
  • 6

Ruby (200)

sample runs + wc:

$ ruby onetimepad.rb 
ENCRYPT
AAZZHELLOWORLD
CJGTEFEDXEZUER
CJFSLJPOLANLPU
$ ruby onetimepad.rb 
DECRYPT
CJGTEFEDXEZUER
CJFSLJPOLANLPU
AAZZHELLOWORLD
$ wc onetimepad.rb
       2       3     200 onetimepad.rb
f=->k,p,o{p.scan(r=/./).zip(k.scan r).map{|a|a.map{|b|b.ord-65}.inject(o)}.map{|c|(c%26+65).chr}*''}
puts(gets[0]==?D?(k=gets;f[k,gets,:-]):[k=(2..(p=gets).size).map{(65+rand(26)).chr}*'',f[k,p,:+]])

and with newlines and indents (still not very readable)

f=->k,p,o{
  p.scan(r=/./).zip(k.scan r).map{|a|
    a.map{|b|b.ord-65}.inject(o)
  }.map{|c|
    (c%26+65).chr
  }*''
}
puts(gets[0]==?D ?
     (k=gets;f[k,gets,:-])
    : [k=(2..(p=gets).size).map{
         (65+rand(26)).chr
       }*'',
       f[k,p,:+]])