70 likes | 164 Views
Simple encryption. Introduction to Computers and Programming NCTU , Fall 2012 07/01/2013. Property of XOR. A ⊕ B = C C ⊕ B = A This property can be used in encryption: Think of A as original file B is password C is encrypted file
E N D
Simple encryption Introduction to Computers and Programming NCTU, Fall 2012 07/01/2013
Property of XOR • A ⊕ B = C • C ⊕ B = A • This property can be used in encryption: • Think of A as original file • B is password • C is encrypted file • So, to decrypt a file, you just need to encrypt it with same password! 1100 1001 ⊕ 0101 1100 1001 0101 ⊕ 0101 1100 1100 1001
XOR encryption (1/3) input.txt: Password:
XOR encryption (2/3) • While (it is not end of file) { • Read inByte from input • outByte = inByte ^ passwordByte • Write outByte to output • Rotate passwordByte • } passwordByte: q w e r t y
XOR encryption (3/3) ⊕ Output: =
Practice • Implement XOR encryption • Your program should ask user for: • Name of input file (max 64 characters) • Name of output file (max 64 characters) • Password (max 64 characters) • Try to catch runtime errors that can occur: • Invalid filename • File does not exist • Empty password • etc.
Example Encrypt Decrypt