Skip to content

Commit

Permalink
* delete __FILE__ trick from lib files.
Browse files Browse the repository at this point in the history
  • Loading branch information
nahi committed Oct 30, 2004
1 parent c1b5e40 commit 8479381
Show file tree
Hide file tree
Showing 4 changed files with 0 additions and 256 deletions.
16 changes: 0 additions & 16 deletions lib/pgp/armor.rb
Expand Up @@ -142,19 +142,3 @@ def calc_checksum


end


if __FILE__ == $0
src = <<EOP
-----BEGIN PGP MESSAGE-----
Version: OpenPrivacy 0.99
yDgBO22WxBHv7O8X7O/jygAEzol56iUKiXmV+XmpCtmpqQUKiQrFqclFqUDBovzS
vBSFjNSiVHsuAA==
=njUN
-----END PGP MESSAGE-----
EOP
include PGP
d = Armor.new(src)
p d
end
8 changes: 0 additions & 8 deletions lib/pgp/mpi.rb
Expand Up @@ -79,11 +79,3 @@ def from_bytes(bytes)


end


if __FILE__ == $0
p PGP::MPI.encode(511)
p PGP::MPI.decode(PGP::MPI.encode(511))
p PGP::MPI.encode(65537)
p PGP::MPI.decode(PGP::MPI.encode(65537))
end
9 changes: 0 additions & 9 deletions lib/pgp/packet/publicsubkey.rb
Expand Up @@ -47,12 +47,3 @@ def self.scanner(io, port, length)

end
end


if __FILE__ == $0
require 'pgp/packet'
d = PGP::Packet::PublicSubkeyRSA.new(1) # RSA Encrypt or Sign
d.keycreated = Time.now
p d.dump
end

223 changes: 0 additions & 223 deletions lib/pgp/packet/symencryptedintegrityprotecteddata.rb
Expand Up @@ -64,226 +64,3 @@ def self.load_version(port)

end
end


if __FILE__ == $0
include PGP
require 'pgp/packet/tstbase'
include Packet::TstBase

require 'pgp/pkeyalgorithm'
require 'pgp/mpi'
require 'openssl'
require 'pgp/hexdump'

def cfb_encrypt(algo, key, data)
header = Util.random_bytes(8)
header << header[6, 2]
cipher = OpenSSL::Cipher::Cipher.new(algo)
cipher.key = key
cipher.padding = 0
bs = 8
# step 1
fr = "\000" * bs
# step 2
cipher.encrypt
p fr
fre = cipher.update(fr); raise unless cipher.final.empty?
# step 3
result = []
for i in 0..(bs - 1) do
result << (fre[i] ^ header[i])
end
# step 4
fr = result.pack("C*")
# step 5
cipher.encrypt
fre = cipher.update(fr); raise unless cipher.final.empty?
# step 6
result << (fre[0] ^ header[bs - 1])
result << (fre[1] ^ header[bs])
# step 7
fr = result.pack("C*")[2..-1]
# step 8
cipher.encrypt
fre = cipher.update(fr); raise unless cipher.final.empty?
# step 9
pos = 0
while pos < data.length
for i in 0..(bs - 1) do
break if data[pos].nil?
result << (fre[i] ^ data[pos])
pos += 1
end
# step 10
fr = result[(pos - bs) + bs + 2, bs].pack("C*")
# step 11
cipher.encrypt
fre = cipher.update(fr); raise unless cipher.final.empty?
end
result.pack("C*")
end

def cfb_decrypt(algo, key, data)
cipher = OpenSSL::Cipher::Cipher.new(algo)
cipher.key = key
cipher.padding = 0
header = data[0, 10]
body = data[10..-1]
bs = 8
# step 1
fr = "\000" * bs
# step 2
cipher.encrypt
fre = cipher.update(fr); raise unless cipher.final.empty?
# step 3
result = []
for i in 0..(bs - 1) do
result << (fre[i] ^ header[i])
end
# step 4
fr = header[0, bs]
# step 5
cipher.encrypt
fre = cipher.update(fr); raise unless cipher.final.empty?
# step 6
result << (fre[0] ^ header[bs - 1])
result << (fre[1] ^ header[bs])
# step 7
fr = header[2, bs]
# step 8
cipher.encrypt
fre = cipher.update(fr); raise unless cipher.final.empty?
# step 9
pos = 0
while pos < body.length
for i in 0..(bs - 1) do
break if body[pos].nil?
result << (fre[i] ^ body[pos])
pos += 1
end
# step 10
fr = body[(pos - bs), bs]
# step 11
cipher.encrypt
fre = cipher.update(fr); raise unless cipher.final.empty?
end
result.pack("C*")[10..-1]
end

def foo(key, lastiv, iv, unused, data)
cipher = OpenSSL::Cipher::Cipher.new("DES-EDE3")
cipher.key = key
cipher.padding = 0
nbytes = data.size
pos = 0
result = []

bs = 8
if nbytes <= unused
raise
end

if unused > 0
nbytes -= unused
for idx in 0..(unused-1)
temp = data[pos]; pos += 1
result << (iv[idx + bs - unused] ^ temp)
iv[idx + bs - unused] = temp
end
end

while nbytes >= bs
lastiv = iv.dup
cipher.encrypt
iv = cipher.update(iv); raise unless cipher.final.empty?
for idx in 0..(bs-1) do
temp = data[pos]; pos += 1
result << (iv[idx] ^ temp)
iv[idx] = temp
end
nbytes -= bs
end

if nbytes > 0
lastiv = iv.dup
cipher.encrypt
iv = cipher.update(iv); raise unless cipher.final.empty?
unused = bs - nbytes
for idx in 0..(nbytes-1) do
temp = data[pos]; pos += 1
result << (iv[idx] ^ temp)
iv[idx] = temp
end
end
return [result.pack("C*"), lastiv, iv, unused]
end

def cipher_sync(lastiv, iv, unused)
bs = 8
if unused > 0
(lastiv + iv)[unused, bs]
else
raise
end
end

text = "123456789"
key2 = Util.random_bytes(24)
cipher = cfb_encrypt("DES-EDE3", key2, text)
plain = cfb_decrypt("DES-EDE3", key2, cipher)
p [cipher, plain]
#exit

m = SEC_SUBKEY.decrypt(MSG_SESSKEY.sessionkey)
alg, key = PKeyAlgorithm.decode_sessionkey(MPI.to_bytes(m))
msg = MSG_DATA.cipher
puts HexDump.encode(key)

p "--------"
puts HexDump.encode(msg)
puts HexDump.encode(cfb_decrypt("DES-EDE3", key2, msg))
p "--"

#msg = cipher; key = key2
header = msg[0, 10]
data = msg[10..-1]

lastiv = iv = "\000" * 8
unused = 0
result1, lastiv, iv, unused = foo(key, lastiv, iv, unused, header)
lastiv = iv
# Unlike the Symmetrically Encrypted Data Packet, no special CFB
# resynchronization is done after encrypting this prefix data.
#iv = cipher_sync(lastiv, iv, unused)
#unused = 0
result2, lastiv, iv, unused = foo(key, lastiv, iv, unused, data)
puts HexDump.encode(result1 + result2)
p "-"
cipher = OpenSSL::Cipher::Cipher.new("DES-EDE3-CFB")
cipher.decrypt
cipher.key = key
cipher.iv = "\000" * 8
cipher.padding = 0
puts HexDump.encode(cipher.update(msg) + cipher.final)

target = result2[0, result2.size - 20]
mdc = result2[-20..-1]
if Digest::SHA1.digest(result1 + target) != mdc
raise "MDC check failed"
end
require 'zlib'
z = Zlib::Inflate.new(-15)
p "--"
puts HexDump.encode(z.inflate(target[2..-1]) + z.finish)

p "/////"

m = SEC_SUBKEY.decrypt(MSG_SESSKEY.sessionkey)
algo, key = PKeyAlgorithm.decode_sessionkey(MPI.to_bytes(m))
MSG_DATA.decrypt(algo, key)
com = PGP::Packet::Packet.load(MSG_DATA.plain)
lit = com[0].body
p PGP::Packet::Packet.load(lit)
puts PGP::Packet::Packet.load(lit)[0].body
end

0 comments on commit 8479381

Please sign in to comment.