f88e2d9a8a44d1d63c7378236c9a532bffc592fa
[nit.git] / lib / crapto / examples / repeating_key_xor_solve.nit
1 # This file is part of NIT ( http://www.nitlanguage.org ).
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
6 #
7 # http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
14
15 import base64
16 import crapto
17
18 # Check usage
19 if args.length != 1 then
20 print "Usage: repeating_key_xor_solve <cipher_file>"
21 exit 1
22 end
23
24 # Read the cipher from the file
25 var cipher_bytes = args[0].to_path.read_all_bytes.decode_base64
26
27 # Create a RepeatingKeyXorCipher object to manipulate your ciphertext
28 var xorcipher = new RepeatingKeyXorCipher
29 xorcipher.ciphertext = cipher_bytes
30
31 # Try to find the best candidate key
32 xorcipher.find_key
33
34 # Decrypt the cipher according to the found key
35 xorcipher.decrypt
36
37 # Check the resulting plaintext out...
38 print xorcipher.plaintext