gpt4 book ai didi

java - 通过比较进行字符串预测

转载 作者:塔克拉玛干 更新时间:2023-11-03 04:27:27 26 4
gpt4 key购买 nike

今天我醒来,想是否可以只分析每次比较之间的时间来预测字符串。

我创建了一个基本类(我知道它不是最好的算法,但它对我有用)来尝试证明这一点,答案是

import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;

public class Test {

public static final int iters = 1000000;
public static final String SECRET_WORD = "85742";
public static final char[] LETTERS = new char[] { '1', '2', '3', '4', '5',
'6', '7', '8', '9', '0' };

public static void main(String[] args) {
int length = calculateLength();
System.out.println("Secret word is " + SECRET_WORD
+ " with a real length of " + SECRET_WORD.length()
+ " and a calculate Length of " + length);
prediceText(length);

}

private static String prediceText(int length) {
StringBuilder sbMain = new StringBuilder(length);
for (int i = 0; i < length; i++) {
Map<Character, Double> map = map2();

while (map.entrySet().size() > 1) {
for (Entry<Character, Double> entry : map.entrySet()) {
String str = sbMain.toString() + entry.getKey();
while (str.length() < length) {
str += " ";
}
long[] diffs = new long[iters];
for (int j = 0; j < iters; j++) {
long timeInit = System.nanoTime();
if (SECRET_WORD.equals(str)) {
}
diffs[j] = System.nanoTime() - timeInit;
}

long total = 0;
for (long diff : diffs) {
total += diff;
}

entry.setValue((double) total / iters);
}

double min = Double.MAX_VALUE;
char myChar = 'a';
for (Entry<Character, Double> entry : map.entrySet()) {
if (entry.getValue() < min) {
myChar = entry.getKey();
min = entry.getValue();
}
}
System.out.print(".");
map.remove(myChar);
}

sbMain.append(map.keySet().iterator().next());
System.out.println("####### " + sbMain.toString() + " ######");

}

return sbMain.toString();
}

private static int calculateLength() {
Map<Integer, Double> map = map();
int iter = 0;
while (map.entrySet().size() > 1) {
for (Entry<Integer, Double> entry : map.entrySet()) {
StringBuilder sb = new StringBuilder();
while (sb.length() < entry.getKey()) {
sb.append("a");
}
String str = sb.toString();
long[] diffs = new long[iters];
for (int i = 0; i < iters; i++) {
long timeInit = System.nanoTime();
if (SECRET_WORD.equals(str)) {
}
diffs[i] = System.nanoTime() - timeInit;
}

long total = 0;
for (long diff : diffs) {
total += diff;
}

entry.setValue((double) total / iters);
}

double min = Double.MAX_VALUE;
int length = 0;
for (Entry<Integer, Double> entry : map.entrySet()) {
if (entry.getValue() < min) {
length = entry.getKey();
min = entry.getValue();
}
}
System.out.print(".");

iter++;
map.remove(length);
}

return map.keySet().iterator().next();
}

private static Map<Integer, Double> map() {
Map<Integer, Double> map = new HashMap<Integer, Double>();
for (int i = 1; i < 21; i++) {
map.put(i, (double) 0);
}
return map;
}

private static Map<Character, Double> map2() {
Map<Character, Double> map = new HashMap<Character, Double>();
for (char myChar : LETTERS) {
map.put(myChar, (double) 0);
}
return map;
}

}

这个控制台显示:

...................Secret word is 85742 with a real length of 5 and a calculate Length of 5
.........####### 8 ######
.........####### 85 ######
.........####### 857 ######
.........####### 8574 ######
.........####### 85742 ######

那个代码可以为我预测字符串,成功率为90%,那么我认为一个好的算法可能是一个问题。

这个问题会不会有安全隐患

最佳答案

是的,此类问题可能具有安全隐患。它叫做timing attack并在密码学中广为人知。通常使用不同的算法比较敏感数据,例如无论是否发现差异,所有符号都会比较到最后。但是应采取预防措施,因为智能 JIT 编译器可以优化您的代码,因此它仍然容易受到攻击。

关于java - 通过比较进行字符串预测,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30210264/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com