Programming Geek
Rated 4.7/5 based on 1446 reviews

CodeVita Season IV Round 1 : Fibonacci encryption and decryption

Problem : Fibonacci encryption and decryption

A password encryption algorithm on a pair of source message (plain text) and password, containing lowercase and uppercase letters only, is explained through example as below:

Source Message is: abcd
Password is: xyz 


Steps in the Encryption Algorithm 
  1. The source message is reversed i.e abcd is written as dcba.
  2. The password is appended i.e the cipher text becomes dcbaxyz.
  3. The Fibonacci series, up to the length of the password is generated. In our case, the password length is 3, so the first three Fibonacci terms viz. 1, 1, 2 are generated. Start the Fibonacci series from 1 instead of 0.
  4. Every element in odd position in the cipher text is forwarded by the current Fibonacci term, i.e. in the first iteration all odd indexed elements would be advanced by 1, in the second iteration by 1, in the third iteration by 2 and so on. Once 'z' is reached, we wrap around i.e. if we forward 'z' or 'Z' by 1 the output would be 'a' or 'A'.

    Similarly, every element in even position in the cipher text is reversed by the current Fibonacci term, i.e. in the first iteration all even elements would be reversed by 1, in the next iteration by 1, in the next by 2 and so on. Once 'a' is reached, wrap around i.e. if we reverse 'a' or 'A' by 1 the output would be 'z' or 'Z'.
  5. So, in our case the Encryption iteration for the cipher text 'dcbaxyz' would be,
    • ebczyxa when the current fibonacci term is 1
    • fadyzwb when the current fibonacci term is 1
    • hyfwbud when the current fibonacci term is 2
    So, our resultant encrypted string would be 'hyfwbud'.


Decryption operation is the reverse of encryption operation. For e.g. if encrypted text is 'hyfwbud', and password is xyz, then the decryption steps are as below:
  • Password length is 3, hence number of Fibonacci terms are 3 viz. 1, 1, 2
  • First iteration on ecrypted text when the current fibonacci term is 1 will be gzexavc
  • Second iteration on gzexavc when the current fibonacci term is 1 will be fadyzwb
  • Third iteration on fadyzwb when the current fibonacci term is 2 will be dcbaxyz
  • If L is the password length, 3 in this case, then extract last L characters from ciphertext i.e. dcbaxyz and match it with password provided in input.
    • If password matches, reverse the remaining text which will be the original clear text i.e. abcd
    • If password does not match, then clear text cannot be obtained. See output specifications for more details


Your task would be encrypt or decrypt a given list of strings based on two inputs, viz. file name and password. The file referred to by the file name consists of the clear text strings to be encrypted or ciphertext to be decrypted, respectively.

Input Format:

First line contains an integer N (1 for encryption or 2 for decryption)
Second line contains File name, where file contains text to encrypt or decrypt
Third line contains password
Output Format:

Print encrypted string, one per line of clear text in the file, if 1 is given as first input.
OR
Print decrypted string, one per line of cipher text in the file, if 2 is given as first input.
OR
Print Wrong Password if passwords don't match, if 2 is given as first input.
Constraints:
Password length is greater than 0
Sample Input and Output

SNo.File NameFile contentsInputOutput
1
file1.txt

Hi
Hello
HowAreYou
Assassination
Captain
Nation
rabbit
fox

1
file1.txt
password

kFryuqymtb
qjncJncquuqpf
wmActYymJncquuqpf
pmkrclkquyuqCncquuqpf
pgcrryEncquuqpf
pmkrcLryuqymtb
vgdzcpryuqymtb
zmhncquuqpf
2
file2.txt

kFryuqymtb
qjncJncquuqpf
wmActYymJncquuqpf
pmkrclkquyuqCncquuqpf
pgcrryEncquuqpf
pmkrcLryuqymtb
vgdzcpryuqymtb
zmhncquuqpf

2
file2.txt
password

Hi
Hello
HowAreYou
Assassination
Captain
Nation
rabbit
fox
3
file3.txt

kFryuqymtb
qjncJncquuqpf
wmActYymJncquuqpf
pmkrclkquyuqCncquuqpf
pgcrryEncquuqpf
pmkrcLryuqymtb
vgdzcpryuqymtb
zmhncquuqpf

2
file3.txt
passwordd

Wrong Password

Note:

Please do not use package and namespace in your code. For object oriented languages your code should be written in one class.
Note:

Participants submitting solutions in C language should not use functions from / as these files do not exist in gcc
Note:

For C and C++, return type of main() function should be int.

No comments :

Post a Comment