我有Vigenère密码java代码,它运行良好,但我想用斐波纳契公式修改这个代码,所以加密结果就像一次性填充方法.. 这是代码:
public class Coba {
public static void main(String[] args) {
String key = "abcd";
String ori = "satusatu";
String enc = enkripsibaru(ori, key);
System.out.println(enc);
System.out.println(key);
System.out.println(ori);
System.out.println(dekripsibaru(enc, key));
}
static String enkripsibaru (String plaintext, String key)
{
String s = "";
int lengthPlainText = plaintext.length();
int lengthKey = key.length();
int j = 0;
for (int i = 0; i < lengthPlainText; i++)
{
if (j >= lengthKey)
{
key = plaintext;
lengthKey = lengthPlainText;
j = 0;
}
s += (char) (((int) plaintext.charAt(i) + key.charAt(j))%26);
j++;
}
return s;
}
public static String dekripsibaru(String cipher, String kunci)
{
String s = "";
int lengthCipherText = cipher.length();
int lengthKey = kunci.length();
int j = 0;
int temp1;
int a = 0;
for (int i = 0; i<lengthCipherText; i++)
{
if (j >= lengthKey)
{
temp1 = (int) cipher.charAt(i) - (int) s.charAt(a);
if (temp1 < 0) {
temp1 += 26;
}
s += (char) (temp1 % 26);
a++;
}
else
{
temp1 = (int) cipher.charAt(i) - (int) kunci.charAt(j);
if (temp1 < 0)
{
temp1 += 26;
}
s += (char) (temp1 % 26);
}
j++;
}
return s;
}
}
我应该怎么做这个代码与斐波纳契结合? 感谢
答案 0 :(得分:0)
我猜你想要使用Fibonacci序列而不是密钥。
Fibonacci序列如下:
0,1,1,2,3,5,8,13,21,34,55等
所以各个字母的变化将是上面的模26:
0,1,1,2,3,5,8,13,21,8,3等
所以让我们制作一个计算Fibonacci序列的方法:
static long fibonacci (int n) {
if (n <= 1) return n;
else return fibonacci(n-1) + fibonacci(n-2);
}
现在我们替换:
s += (char) (((int) plaintext.charAt(i) + key.charAt(j))%26);
使用:
s += (char) ((int) ((int) plaintext.charAt(i) + fibonacci(i)) % 26);