sakide 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (2) hide show
  1. data/lib/sakide.rb +98 -0
  2. metadata +62 -0
@@ -0,0 +1,98 @@
1
+ # USAGE:
2
+ # saki = Sakide.new "/path/to/key/file.des"
3
+ # cleartext = "PID=IEB0001&CRYPTO=1&MSGT=10&TRID=1234123412341234&UID=IEB00000001&LANG=HU&TS=19700101000000&AUTH=0&AMO=10000&URL=http://localhost/"
4
+ # puts "Cleartext: #{cleartext}"
5
+ # crypto = saki.encode(cleartext);
6
+ # puts "Crypted: #{crypto}"
7
+ # cleartext2 = saki.decode(crypto)
8
+ # puts "Cleartext: #{cleartext2}"
9
+
10
+ require "cgi"
11
+ require "zlib"
12
+ require "mcrypt"
13
+ require "base64"
14
+
15
+ class Sakide
16
+
17
+ def initialize(keyfile)
18
+ f = File.open keyfile, "r"
19
+ keyinfo = f.read(38)
20
+ f.close
21
+ k1 = keyinfo[14,8]
22
+ k2 = keyinfo[22,8]
23
+ @iv = keyinfo[30,8]
24
+ @key = k1+k2+k1
25
+ end
26
+
27
+ def encode(plaintext)
28
+ arr = plaintext.split '&'
29
+ outs = ''
30
+ pid = ''
31
+ arr.count.times do |i|
32
+ outs += "&#{arr[i]}" if arr[i].upcase != 'CRYPTO=1'
33
+ pid = arr[i].upcase[4,7] if arr[i].upcase[0,4] == 'PID='
34
+ end
35
+ outs = outs[1..-1]
36
+ outs = CGI.escape outs
37
+ outs.gsub!('%3D', '=')
38
+ outs.gsub!('%26', '&')
39
+ crc = Zlib::crc32(outs).to_s(16).rjust(8, '0')
40
+ 4.times do |i|
41
+ outs += crc[i*2,2].to_i(16).chr
42
+ end
43
+ pad = 8 - (outs.length % 8)
44
+ pad.times do |i|
45
+ outs += pad.chr
46
+ end
47
+ td = Mcrypt.new(:tripledes, :cbc, @key, @iv, '')
48
+ outs = td.encrypt outs
49
+ pad = 3 - (outs.length % 3)
50
+ pad.times do |i|
51
+ outs += pad.chr
52
+ end
53
+ outs = Base64.strict_encode64 outs
54
+ outs = CGI.escape(outs) # no clue why we need strip, without that we get an extra new line
55
+ "PID=#{pid}&CRYPTO=1&DATA=#{outs}"
56
+ end
57
+
58
+ def decode(crypto)
59
+ arr = crypto.split '&'
60
+ outs = ''
61
+ pid = ''
62
+ arr.count.times do |i|
63
+ outs += arr[i][5..-1] if arr[i][0,5].upcase == 'DATA='
64
+ pid = arr[i].upcase[4,7] if arr[i].upcase[0,4] == 'PID='
65
+ end
66
+ outs = CGI.unescape outs
67
+ outs = Base64.strict_decode64 outs
68
+ lastc = outs[-1].ord
69
+ validpad = 1
70
+ lastc.times do |i|
71
+ validpad = 0 if outs[(outs.size-1-i),1].ord != lastc
72
+ end
73
+ outs = outs[0,(outs.size-lastc)] if validpad == 1
74
+ td = Mcrypt.new(:tripledes, :cbc, @key, @iv, '')
75
+ outs = td.decrypt outs
76
+ lastc = outs[-1].ord
77
+ validpad = 1
78
+ lastc.times do |i|
79
+ validpad = 0 if outs[(-1-i)].ord != lastc
80
+ end
81
+ outs = outs[0,(outs.size-lastc)] if validpad == 1
82
+ crc = outs[(outs.size-4)..-1]
83
+ crch = ''
84
+ 4.times do |i|
85
+ crch += crc[i].ord.to_s(16).rjust(2, '0')
86
+ end
87
+ outs = outs[0,(outs.size-4)]
88
+ crc = Zlib::crc32(outs).to_s(16).rjust(8, '0')
89
+ if crch != crc
90
+ ''
91
+ else
92
+ outs.gsub!('&', '%26')
93
+ outs.gsub!('=', '%3D')
94
+ outs = CGI.unescape(outs)
95
+ "CRYPTO=1&#{outs}"
96
+ end
97
+ end
98
+ end
metadata ADDED
@@ -0,0 +1,62 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: sakide
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Matyas Juhasz
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-12-13 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: ruby-mcrypt
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
30
+ description: SAKIDE encryption modul for CIB bank credit card payments
31
+ email: juhasz.matyas@pixelface.hu
32
+ executables: []
33
+ extensions: []
34
+ extra_rdoc_files: []
35
+ files:
36
+ - lib/sakide.rb
37
+ homepage: http://rubygems.org/gems/sakide
38
+ licenses:
39
+ - MIT
40
+ post_install_message:
41
+ rdoc_options: []
42
+ require_paths:
43
+ - lib
44
+ required_ruby_version: !ruby/object:Gem::Requirement
45
+ none: false
46
+ requirements:
47
+ - - ! '>='
48
+ - !ruby/object:Gem::Version
49
+ version: '0'
50
+ required_rubygems_version: !ruby/object:Gem::Requirement
51
+ none: false
52
+ requirements:
53
+ - - ! '>='
54
+ - !ruby/object:Gem::Version
55
+ version: '0'
56
+ requirements: []
57
+ rubyforge_project:
58
+ rubygems_version: 1.8.24
59
+ signing_key:
60
+ specification_version: 3
61
+ summary: SAKIDE
62
+ test_files: []