# Fibionicci as "Mật Khẩu" ? Java 84 Joe viết ngày 29/06/2018

The well-written blog "Băm và lưu password đúng cách" written by Quang prompted me to write this blog for Kipalog.

However one should 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 encryp/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 encryted text is almost foolproof and unbreakable. HOW? As said, the ancient Chinese warlords had used this HOW to encrypt/decrypt their messaging texts. Some words 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 anpha-nummeric table which start at A, B, C, D, ..., a, b, c, ..., 0, 1, 2,..., 9 .... Example:

My secret FN is 2.

            +---+---+---+---+----+----+----+----+----+-------+
! 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 the 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 is 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:

import java.io.*;
import java.util.*;
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[0].charAt(0) == '0') fb.encrypt();
else fb.decrypt( );
System.out.println("Done!");
}
public void encrypt( ) throws Exception {
FileInputStream fi = new FileInputStream(fname);
byte[] txt = new byte[fi.available()];
fi.close();
FileOutputStream fo = new FileOutputStream(fname, true); // append
StringBuilder sb = new StringBuilder(n);
for (int i = 0; i < n; ++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();
}
public void decrypt( ) throws Exception {
FileInputStream fi = new FileInputStream(fname);
byte[] txt = new byte[fi.available()];
fi.close();
FileOutputStream fo = new FileOutputStream(fname, true); // append
String[] fn = (new String(txt)).split(" ");
StringBuilder sb = new StringBuilder(n);
for (int i = 0; i < fn.length; ++i) {
if (fn[i].length() == 0 || fn[i].charAt(0) < '0') continue;
sb.append(""+(char)FN_C.get(new BigInteger(fn[i].replace("\r","").replace("\n",""))).intValue());
}
fo.write(sb.toString().getBytes());
fo.flush();
fo.close();
}
private String fname;
private HashMap<BigInteger, Integer> FN_C;
private HashMap<Integer, BigInteger> C_FN;
// ignore all special characters except SPACE and \n
public Fibionicci(String[] args) throws Exception {
fname = args[2];
BigInteger fn = new BigInteger(args[1]); // starting value
// look for the predecessor and check for correct Fibionicci number
long l = Long.parseLong(args[1]);
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)'\n');
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);
}
}



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.
C:\Fibionicci>javac -g:none -d ./classes Fibionicci.java

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

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

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

C:\Fibionicci>


If "Done!" comes up your encrypted or decrypted text has been appended in the given file of 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:

Joe Nartca

539 29113076152 236707323 64252545876217 3694 34535133 123324969614 322868962111 90414152 34535133 819351227872986822723 1325738135422452890894


The reverse is:

539 29113076152 236707323 64252545876217 3694 34535133 123324969614 322868962111 90414152 34535133 819351227872986822723 1325738135422452890894

Joe Nartca



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

Bình luận

Bỏ hay Hay
{{comment.like_count}}
{{ comment_error }}

Hiển thị thử

Chỉnh sửa

### Joe

31 bài viết.
280 người follow
Cùng một tác giả
36 14
Chao cac ban To the Admins: if you think that this posting breaks some rules of your site please just delete it. NO need to send me a feedback. Th...
Joe viết 4 tháng trước
36 14
28 15
Fuzzy Logic and Machine Learning Hi First of all: I apologize everyone for my writing in English. I come to this site because someone of Daynhauh...
Joe viết 1 năm trước
28 15
27 11
You're a fresh graduate and work for more than 12 months in an IT company with some boring coding tasks... The tasks are unchallenging. Day in, day...
Joe viết 4 tháng trước
27 11
Bài viết liên quan
0 0
Trong bài viết này, một số hình ảnh hoặc nọi dung có thể bị thiếu do quá trình chế bản. Vui lòng xem nội dung ở blog gốc sau: (Link) (Link), chúng...
programmerit viết 3 năm trước
0 0
9 5
Facade Design Patern Facade Patern thuộc vào họ mô hình cấu trúc (structural patern). Facade patern phát biểu rằng : "just provide a unified an...
DuongVanTien viết 2 năm trước
9 5
4 2
Một số hàm giúp mapping data đơn giản với Groovy Ví dụ với list data như sau: def student1 = name: "Huan", age: 22, gender: "male"] def student...
Tất Huân viết 4 tháng trước
4 2

kipalog

bình luận

31 bài viết.
280 người follow

Đầu mục bài viết

Vẫn còn nữa! x

Kipalog vẫn còn rất nhiều bài viết hay và chủ đề thú vị chờ bạn khám phá!