diff options
author | Jérémy Zurcher <jeremy@asynk.ch> | 2009-09-21 12:52:44 +0200 |
---|---|---|
committer | Jérémy Zurcher <jeremy@asynk.ch> | 2011-06-30 08:08:33 +0200 |
commit | 0cd373cb04619724a29c0bb241774c8bd61698c1 (patch) | |
tree | 63a6977394ef591d9fc70c26d1aa84213c9beecd /lib | |
parent | 238f5654b288bc947d4bf56c2e8c166173253ce1 (diff) | |
download | ayk-0cd373cb04619724a29c0bb241774c8bd61698c1.zip ayk-0cd373cb04619724a29c0bb241774c8bd61698c1.tar.gz |
jcryption
Diffstat (limited to 'lib')
-rw-r--r-- | lib/ayk/jcryption.rb | 106 |
1 files changed, 106 insertions, 0 deletions
diff --git a/lib/ayk/jcryption.rb b/lib/ayk/jcryption.rb new file mode 100644 index 0000000..ab60d34 --- /dev/null +++ b/lib/ayk/jcryption.rb @@ -0,0 +1,106 @@ +#! /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 +# +# Permission is hereby granted, free of charge, to any person obtaining +# a copy of this software and associated documentation files (the +# "Software"), to deal in the Software without restriction, including +# without limitation the rights to use, copy, modify, merge, publish, +# distribute, sublicense, and/or sell copies of the Software, and to +# permit persons to whom the Software is furnished to do so, subject to +# the following conditions: +# +# The above copyright notice and this permission notice shall be +# included in all copies or substantial portions of the Software. +# +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND +# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE +# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION +# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION +# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. +# +#---------------------------------------------------------------------------- +# +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 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 +# +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 + 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 + end + module_function :decrypt + # + def rsa_keys key, len + '{"e":"'+key.e.to_s(16)+'","n":"'+key.n.to_s(16)+'","maxdigits":"'+(len*2/16+3).to_s+'"}' + end + module_function :rsa_keys + # + def gen_keypair bits + # therese no point to try without using openssl + require 'openssl' + OpenSSL::PKey::RSA.generate( bits ) + end + module_function :gen_keypair +end + |