summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJérémy Zurcher <jeremy@asynk.ch>2011-08-05 23:38:26 +0200
committerJérémy Zurcher <jeremy@asynk.ch>2011-08-05 23:38:26 +0200
commitbfd398596afed66754011b6720e5891c4897c858 (patch)
treec82b2c7f107a0eb08b3a31da24f2179759aa105a
parent91d70d775dd11a9ec350ca7f3b6d403fa897aadc (diff)
downloadayk-bfd398596afed66754011b6720e5891c4897c858.zip
ayk-bfd398596afed66754011b6720e5891c4897c858.tar.gz
update jcryption and specs
-rw-r--r--lib/ayk/jcryption.rb75
-rw-r--r--spec/jcryption_spec.rb16
2 files changed, 4 insertions, 87 deletions
diff --git a/lib/ayk/jcryption.rb b/lib/ayk/jcryption.rb
index 6e4d1ed..3b4871f 100644
--- a/lib/ayk/jcryption.rb
+++ b/lib/ayk/jcryption.rb
@@ -1,14 +1,7 @@
#! /usr/bin/env ruby
# -*- coding: UTF-8 -*-
-
-#----------------------------------------------------------------------------
#
-# File : jcryption.rb
-# Author : Jérémy Zurcher <jeremy@asynk.ch>
-# Date : 11/09/09
-# License :
-#
-# Copyright (c) 2009 Jérémy Zurcher
+# Copyright (c) 2009-2011 Jérémy Zurcher
#
# Permission is hereby granted, free of charge, to any person obtaining
# a copy of this software and associated documentation files (the
@@ -31,75 +24,15 @@
#
#----------------------------------------------------------------------------
#
-def Math.power_modulo(b, p, m)
- if p == 1
- b % m
- elsif (p & 0x1) == 0
- t = power_modulo(b, p >> 1, m)
- (t * t) % m
- else
- (b * power_modulo(b, p-1, m)) % m
- end
-end
-#
-class Fixnum
- def to_bytes
- str = ''
- int = self
- while int!=0 do
- int,r = int.divmod 256
- str += r.chr
- end
- str
- end
-end
-#
-class Bignum
- #
- class << self
- def from_bytes bytes
- val = 0
- idx = 0
- bytes.each_byte{ |b|
- val += b << 8 * idx
- idx += 1
- }
- val
- end
- end
- #
- def to_bytes
- str = ''
- bint = self
- while bint!=0 do
- bint,r = bint.divmod 256
- str += r.chr
- end
- str
- end
- #
- def bits
- s = self.to_bytes
- while s[-1].ord==0
- s.slice!(-1)
- end
- bit_len = s.length * 8
- tmp = s[-1].ord
- while not tmp & 0x80
- bit_len-=1
- tmp <<=1
- end
- bit_len
- end
-end
+require 'ayk/maths'
#
module JCryption
#
def decrypt data, d, n
- s = data.split(' ').inject('') do |str,block| str += ( Math::power_modulo( block.to_i(16), d, n ) ).to_bytes end
+ s = data.split(' ').inject('') do |str,block| str += ( Math::prime_power_modulus( block.to_i(16), d, n ) ).to_bytes end
sum,crc = 0,s.slice(0,2).to_i(16)
s.slice(2..-1).each_char do |char| sum += char.ord end
- return ( crc == sum & 0xFF ) ? s : nil
+ return ( crc == sum & 0xFF ) ? s : nil
end
module_function :decrypt
#
diff --git a/spec/jcryption_spec.rb b/spec/jcryption_spec.rb
index a974ea1..ce1d174 100644
--- a/spec/jcryption_spec.rb
+++ b/spec/jcryption_spec.rb
@@ -5,22 +5,6 @@ require 'ayk/jcryption'
#
describe JCryption do
#
- describe 'Bignum' do
- it '#to_bytes #from_bytes should work' do
- 0x217962755220666f207463657073612074656577732061207369206d756e676942.should eql Bignum.from_bytes(0x217962755220666f207463657073612074656577732061207369206d756e676942.to_bytes)
- end
- it '#bits' do
- K_BITS = 1024
- k = JCryption.gen_keypair( K_BITS )
- k.n.to_i.bits.should eql k.d.to_i.bits
- k.d.to_i.bits.should eql k.d.to_i.bits
- k.d.to_i.bits.should eql K_BITS
- k.p.to_i.bits.should eql k.q.to_i.bits
- k.q.to_i.bits.should eql k.q.to_i.bits
- k.q.to_i.bits.should eql K_BITS/2
- end
- end
- #
describe 'JCryption' do
it 'should decrypt data' do
data = '50c412c556a9fab55b50bd0d989067b341f9cdbf12ccb65aeb69dbb360a8e0dbd2069d686873b659b1be7a4c78fe631d362dbf538a5298ecaff19f78cd859897a7317c71b51b90e0a8c03e10059b68826bfeb194a4339a0ecc4a571ba9066fcf3504ffe76e07468cbe03337d3cfebd4efe687f5da72bf694dada987cea884180 ac07cc8462ce0f3e23dafe5d19f47eb33024c91de48413a971bf3b88c159090a26933f694d868bad61517a817f57998ed57bcebcd46a4ed8661cec3e24a8cdf21595902c2fd566820382e2d4f2ee7f1f11fa87a760bcecdf46282147d2c3a1afed3797d22296abcc3b1289489e7c265b3b4223bf93cbf0bb5122c021501a4305'