examples: annotate examples
[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 module repeating_key_xor_solve is example
16
17 import base64
18 import crapto
19
20 # Check usage
21 if args.length != 1 then
22 print "Usage: repeating_key_xor_solve <cipher_file>"
23 exit 1
24 end
25
26 # Read the cipher from the file
27 var cipher_bytes = args[0].to_path.read_all_bytes.decode_base64
28
29 # Create a RepeatingKeyXorCipher object to manipulate your ciphertext
30 var xorcipher = new RepeatingKeyXorCipher
31 xorcipher.ciphertext = cipher_bytes
32
33 # Try to find the best candidate key
34 xorcipher.find_key
35
36 # Decrypt the cipher according to the found key
37 xorcipher.decrypt
38
39 # Check the resulting plaintext out...
40 print xorcipher.plaintext