crypto-toolbox 0.2.5 → 0.2.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 6b4aeddf63a07920d3d86fb7e0414657b9760105
4
- data.tar.gz: 14d41f8ef3a8180a7345b0ebfdc50aed896dde6a
3
+ metadata.gz: 1123ac6ed9d5331c0c2201ba341b68a9841cbe92
4
+ data.tar.gz: 10885b70c101833936c9b687150737738e9e0d33
5
5
  SHA512:
6
- metadata.gz: c76e3531f39e618e8d73c23fa889ea1e0669dd6a8c05103f90ea31f197ba11955cfbe71606f534cc18899b0fd692a5b9d3f8d14e4c85668da28fe21d0c41348c
7
- data.tar.gz: 36b518c0a99a911a205b2e5c29c12f7a9bbed101f0425e19b083acb9c6ada9d2623d4aa8a132acc5bf95959979886196f8e895f4bd526b221bdf1ddaf31ba880
6
+ metadata.gz: 49add99b4bbba46bb5c7f25830a4eeeb52f2b9ab784edf1b8e2e9f572cba52b7085476494a2071b086ea14f4bcb95d55ed5a3cdd3f74f26f4d38304cd8aeab20
7
+ data.tar.gz: ad20f731508e0a281dc088775d05ca0973f80dcc61b2d1a9ab96b4195b5a055f14c1e2c494cdfb573567511bfd935fd02e0ab29e76b1c678081296446103d869
@@ -1,6 +1,7 @@
1
1
  # coding: utf-8
2
2
  require 'crypto-toolbox/utils/reporting/console.rb'
3
3
  require 'crypto-toolbox/utils/hamming_distance_filter.rb'
4
+ require 'crypto-toolbox/utils/ecb_detector.rb'
4
5
 
5
6
  require 'crypto-toolbox/crypt_buffer_input_converter.rb'
6
7
  require 'crypto-toolbox/crypt_buffer.rb'
@@ -11,7 +12,6 @@ require 'crypto-toolbox/analyzers/utils/ascii_language_detector.rb'
11
12
  require 'crypto-toolbox/analyzers/utils/spell_checker.rb'
12
13
  require 'crypto-toolbox/analyzers/utils/human_language_detector.rb'
13
14
 
14
-
15
15
  require 'crypto-toolbox/analyzers/padding_oracle.rb'
16
16
  require 'crypto-toolbox/analyzers/cbc_mac.rb'
17
17
  require 'crypto-toolbox/analyzers/vigenere_xor.rb'
@@ -22,4 +22,5 @@ require 'crypto-toolbox/ciphers/rot13.rb'
22
22
 
23
23
  require 'crypto-toolbox/forgers/stream_ciphers/forge_generator.rb'
24
24
 
25
+
25
26
  require 'crypto-toolbox/crypto_challanges/solver.rb'
@@ -5,13 +5,60 @@ module Ciphers
5
5
  @cipher = OpenSSL::Cipher::AES.new(keysize,mode)
6
6
  end
7
7
 
8
- def decipher_ecb(input,key)
8
+ def decipher_ecb(key,input)
9
9
  @cipher.decrypt
10
10
  @cipher.key = key
11
11
  (@cipher.update(input) + @cipher.final)
12
12
  end
13
+
14
+ def encipher_ecb(key,input)
15
+ @cipher.encrypt
16
+ @cipher.key = key
17
+
18
+ encipher_ecb_blockwise(@cipher,CryptBuffer(input).chunks_of(16)) + @cipher.final
19
+ end
13
20
 
14
- def encipher_ecb(input,key)
21
+ def encipher_cbc(key,input,iv: nil)
22
+ blocks = CryptBuffer(input).chunks_of(16)
23
+ iv ||= blocks.shift.str
24
+ k = CryptBuffer(key).hex
25
+ xor_input=iv.to_crypt_buffer
26
+
27
+ data = blocks.map do |block|
28
+ xored = xor_input ^ block
29
+
30
+ _,out = AES.encrypt(xored.str, k, {:format => :plain,:padding => false,:cipher => "AES-128-ECB",:iv => xor_input.str })
31
+ ecb_block = CryptBuffer(out)
32
+ xor_input = ecb_block
33
+ ecb_block.str
34
+ end.join
35
+ (data).to_crypt_buffer
36
+ end
37
+
38
+ def decipher_cbc(key,input,iv: nil)
39
+ blocks = CryptBuffer(input).chunks_of(16)
40
+ iv ||= blocks.shift.str
41
+ k = CryptBuffer(key).hex
42
+ xor_input=iv.to_crypt_buffer
43
+
44
+ data = blocks.map do |block|
45
+ out = ::AES.decrypt([xor_input.str,block.str] , k, {:format => :plain,:padding => false,:cipher => "AES-128-ECB",:iv => xor_input.str })
46
+ ecb_block = CryptBuffer(out)
47
+ result = ecb_block ^ xor_input
48
+ xor_input = block
49
+ result.str
50
+ end.join
51
+ (data).to_crypt_buffer
52
+ end
53
+
54
+ private
55
+ def encipher_ecb_blockwise(crypter,blocks)
56
+ blocks.map{|block| encipher_ecb_block(block) }.join
57
+ end
58
+
59
+ def encipher_ecb_block(crypter,block)
60
+ crypter.update(block.str)
15
61
  end
16
62
  end
17
63
  end
64
+
@@ -40,22 +40,20 @@ module CryptoChallanges
40
40
 
41
41
  def solve7(input,key)
42
42
  data = CryptBuffer.from_base64(input).str
43
- Ciphers::Aes.new(128,:ECB).decipher_ecb(data,key)
43
+ Ciphers::Aes.new(128,:ECB).decipher_ecb(key,data)
44
44
  end
45
45
 
46
46
  def solve8(ciphers)
47
- ciphers.map.with_index do |c,i|
48
- if c.chunks_of(16).map(&:bytes).uniq.length < c.chunks_of(16).length
49
- [i,c]
50
- else
51
- nil
52
- end
53
- # only resturn the first none nil value => compact.first
54
- end.compact.first
47
+ Utils::EcbDetector.new.detect(ciphers).first
55
48
  end
56
49
 
57
50
  def solve9(input)
58
51
  CryptBuffer(input).pad(4).str
59
52
  end
53
+
54
+ def solve10(key,input,iv)
55
+ data = CryptBuffer.from_base64(input).str
56
+ Ciphers::Aes.new(128,:ECB).decipher_cbc(key,data,iv: iv).str
57
+ end
60
58
  end
61
59
  end
@@ -0,0 +1,29 @@
1
+ module Utils
2
+ class EcbDetector
3
+ def detect(ciphers)
4
+ result = ciphers.map.with_index{|c,i| detect_ecb_entry(c,i) }
5
+ sanitize_result(result)
6
+ end
7
+
8
+ private
9
+
10
+ def sanitize_result(result)
11
+ result.reject(&:empty?)
12
+ end
13
+
14
+ # search for any chunks whose byte-pattern occours more than once,
15
+ # in that case the number of entries is reduced by uniq
16
+ def duplicate_chunk?(chunks)
17
+ chunks.map(&:bytes).uniq.length < chunks.length
18
+ end
19
+
20
+ def detect_ecb_entry(ciphertext,index)
21
+ if duplicate_chunk?(ciphertext.chunks_of(16))
22
+ [index,ciphertext]
23
+ else
24
+ []
25
+ end
26
+ end
27
+
28
+ end
29
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: crypto-toolbox
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.5
4
+ version: 0.2.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - Dennis Sivia
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-05-17 00:00:00.000000000 Z
11
+ date: 2015-05-20 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: aes
@@ -82,6 +82,7 @@ files:
82
82
  - lib/crypto-toolbox/crypt_buffer_input_converter.rb
83
83
  - lib/crypto-toolbox/crypto_challanges/solver.rb
84
84
  - lib/crypto-toolbox/forgers/stream_ciphers/forge_generator.rb
85
+ - lib/crypto-toolbox/utils/ecb_detector.rb
85
86
  - lib/crypto-toolbox/utils/hamming_distance_filter.rb
86
87
  - lib/crypto-toolbox/utils/reporting/console.rb
87
88
  homepage: https://github.com/scepticulous/crypto-toolbox