# Encrypting/decrypting With Fibionicci Algorithm

#### Joe

##### Thành viên VIP
Before we start to discuss about Encrypting/Decrypting technique We should always keep in mind that "what is made by man will be broken by man". Nothing is really safe. And the risk of breaking is higher if more people know about the using encrypting/decrypting technique.

I am sure that everyone knows about the "Fibionicci Number" (or FN for short.) Don't laugh when I say that the ancient Chinese had known FN before Fibionicci was born and used them to encrypt/decrypt their messages during the warring States period. or in their Fengshui and I-Ching to "foretell" some (future) events.

Also, FN can be used to encrypt/decrypt and if they were used wisely the encrypted text is almost foolproof and unbreakable. HOW? As said, the ancient Chinese warlords had used this HOW to encrypt/decrypt their messaging texts. Some word about FN.

An actual FN is calculated by adding 2 preceding FNs. Let's start by 2 first natural numbers: 0 and 1.
• The first FN is equal 0+1. We now have 3 FNs: 0, 1, 1.
• The next FN is (1+1 = 2). we get 4 FNs: 0, 1, 1, 2.
• etc.
The formula is: FNn = FNn-1 + FNn-2 where n the next number.

Knowing that you can build a foolproof and unbreakable Encrypting/Decrypting algorithm for your own use. To do that you start at an arbitrary FN (which is known only by you) and create an alpha-numeric table which start at A, B, C, D, ..., a, b, c, ..., 0, 1, 2,..., 9 .... Example:

My secret FN is 2.
PHP:
``````			+---+---+---+---+----+----+----+----+----+-------+
! 2 ! 3 ! 5 ! 8 ! 13 ! 21 ! 34 ! 55 ! 89 ! ..... !
+---+---+---+---+----+----+----+----+----+-------+
! A ! B ! C ! D ! E  ! F  ! G  ! H  ! I  ! ......!
+---+---+---+---+----+----+----+----+----+-------+``````
With this "secret" table I am in a fair position to encrypt my name:

J o e N a r t c a
144 433494437 3524578 27777890035288 987 514229 1836311903 4807526976 1346269 514229 44945570212853

It's easy for me to keep ONE (secret) FN in my mind than to memorize the entire table. The best of all is that the table does not need to be written down. And if I change my secret FN daily or weekly my encrypted name will be changed accordingly and that's hard for someone to break my encrypted text.

To alleviate to hardness of memorizing of a (big) Fibionicci Number I just use the "Fibionicci Algorithm" as the ancient Chinese did to do my "encrypting/decrypting work" so that my "ellbowroom" is now more flexible:
• Any number can be used as a starting number, for example N.
• The preceding number is calculated as P = N/2 + N%2.
• The Fibionicci Algorithm stays unchanged: Next = N + P
Because the generated number could be exorbitantly big I show you my "Fibionicci Algorithm" written in JAVA using BigInteger (long is "too" small and double or BigDecimal are floating -meaning imprecise).

Note: If you have to use UTF-8 character set you have to modify the codes accordingly.

Here is the simplest Fibionicci Encrypt-Decrypt-Generator which can be used as an API as following:
1. Fibionicci(String FibNumber): Constructor
2. encrypt(String fname): encrypt a file.
3. decrypt(String fname): decrypt a file.
PHP:
``````import java.io.*;
import java.util.*;
import java.nio.file.Files;
import java.math.BigInteger;
// Joe Nartca (C)
public class Fibionicci {
public static void main(String... args) throws Exception {
if (args.length != 3) {
System.out.println("Usage: java Fibionicci parm1 parm2 parm3\n"+
"parm1 is 0 to encrypt, any number is to decrypt\n"+
"parm2 is your secret starting number (integer)\n"+
"parm3 is the filename of your text (for encrypting or decrypting)");
System.exit(0);
}
Fibionicci fb = new Fibionicci(args);
if (args.charAt(0) == '0') fb.encrypt(args);
else fb.decrypt(args);
System.out.println("Done!");
}
/**
encrypt a Fibionicci file
@param fname String, file name containing the text for encrypting
@Exception exception throws by JAVA
*/
public void encrypt(String fname) throws Exception {
FileOutputStream fo = new FileOutputStream("Encrypted_"+fname, false);
StringBuilder sb = new StringBuilder("Your encrypted text:"+sep);
for (int i = 0; i < txt.length; ++i) {
BigInteger BI =  C_FN.get((int)txt[i]);
if (BI != null) sb.append(BI.toString()+" ");
}
fo.write(sb.toString().getBytes());
fo.flush();
fo.close();
}
/**
Decrypt a Fibionicci file
@param fname String, file name containing the encrypted text
@Exception exception throws by JAVA
*/
public void decrypt(String fname) throws Exception {
FileOutputStream fo = new FileOutputStream("Decrypted_"+fname, false);
String[] fn = (new String(Files.readAllBytes((new File(fname)).toPath()))).split(" ");
StringBuilder sb = new StringBuilder("Your decrypted text:"+sep);
for (int i = 0; i < fn.length; ++i) {
if (fn[i].length() != 0 && fn[i].charAt(0) >= '0')
sb.append(""+(char)FN_C.get(new BigInteger(fn[i].replace("\r","").replace("\n",""))).intValue());
}
fo.write(sb.toString().getBytes());
fo.flush();
fo.close();
}
/**
Constructor
@param fibNumber String, starting Fibionicci Number
@Exception exception throws by JAVA
*/
public Fibionicci(String fibNumber) throws Exception {
BigInteger fn = new BigInteger(fibNumber); // starting value
// look for the predecessor and check for correct Fibionicci number
long l = Long.parseLong(fibNumber);
BigInteger fb = new BigInteger(""+(l/2)+(l%2));
FN_C = new HashMap<BigInteger, Integer>();
C_FN = new HashMap<Integer, BigInteger>();
// create FN-Apha-numeric table
for (int i = 65; i < 127; ++i) {
FN_C.put(fn, i);
C_FN.put(i, fn);
String b = fn.toString();
fb = new BigInteger(b);
}
// special characters & numbers
for (int i = 32; i < 65; ++i) {
FN_C.put(fn, i);
C_FN.put(i, fn);
String b = fn.toString();
fb = new BigInteger(b);
}
// include \r
String b = fn.toString();
fb = new BigInteger(b);
FN_C.put(fn, (int)'\r');
C_FN.put((int)'\r', fn);
// include \n
b = fn.toString();
fb = new BigInteger(b);
FN_C.put(fn, (int)'\n');
C_FN.put((int)'\n', fn);
}
//
private String sep = System.lineSeparator();
private HashMap<BigInteger, Integer> FN_C;
private HashMap<Integer, BigInteger> C_FN;
}``````
If you've successfully compiled this little app you run it as following:
- 1st parameter: 0 for encrpting and a non-zero is for decrypting
- 3rd parameter: the file that contains the text to be encrypted or decrypted.

PHP:
``````C:\Fibionicci>javac -g:none -d ./classes Fibionicci.java

C:\Fibionicci>java Fibionicci 0 3 joe.txt
Done!

C:\Fibionicci>java Fibionicci 1 3 joeE.txt
Done!

C:\Fibionicci>``````
If "Done!" comes up your encrypted or decrypted text is stored in the file with the name "Encrypted_" or "Decrypted_"+fileName of the 3rd parameter

Example: I want to encrypt my name "Joe Nartca". The 3rd parameter is the file joe.txt then after the execution I get this content in the file "Encrypted_joe.txt":
PHP:
``````Your encrypted text:
539 29113076152 236707323 64252545876217 3694 34535133 123324969614 322868962111 90414152 34535133 819351227872986822723 1325738135422452890894``````
Remove the text "Your encrypted text" and store the content int joeE.txt. The reverse is:
PHP:
``````Your decrypted text:
Joe Nartca``````
Your decrypted text is in the file "Decrypted_JoeE.txt":

Have fun with Fibionacci Algorithm...or with your I-Ching/Fengshui.

Sửa lần cuối:
• tranhuyvc