gpt4 book ai didi

cryptography - 我的密码解密类(class)有多安全?

转载 作者:行者123 更新时间:2023-12-03 18:20:19 24 4
gpt4 key购买 nike

我是任何类型解密的完整初学者。我写了一个我认为应该非常安全的类。你能否就我如何改进算法给我 build 性的批评。

package main;

import java.util.Random;

public class Main {

public static void main(String[] args) {
//we will be playing around with this string
new Main("1234567890abc");
}

private Random rnd;
private byte[] randoms;

/**
* Starts up RNG
* Prints out a test
*/
public Main(String password) {
//random long generated from the password
long randomLong = randomNumber(password);
//Random class using randomLong as seed
rnd = new Random(randomLong);
randoms = new byte[password.length()];
//Array of random bytes generated with rnd
rnd.nextBytes(randoms);

System.out.println(randomNumber(password));

String cryped = encrypt(password);
String decryped = decrypt(cryped);

System.out.println(cryped);
System.out.println(decryped);
}

/**
* Encrypts the password.
*/
private String encrypt(String password) {
char[] chars = password.toCharArray();
for (int i = 0; i < chars.length; i++) {
chars[i] = (char) (chars[i] + randoms[i]);
}
return String.valueOf(chars);
}

/**
* Decrypts an allready encryped password.
*/
private String decrypt(String crypted) {
char[] chars = crypted.toCharArray();
for (int i = 0; i < chars.length; i++) {
chars[i] = (char) (chars[i] - randoms[i]);
}
return String.valueOf(chars);
}

/**
* Finds a random number BASED ON PASSWORD
*/
private long randomNumber(String password)
{
char[] chars = password.toCharArray();
long number = 0;
for (char c : chars) {
number += c;
}
number *= chars.length;
return number;
}
}

该类是用 Java 编写的,但应该对任何人都可读。

最佳答案

  • 不要在现实生活中重新发明自己的密码学(这是一种练习吗?)即使是专家也会犯错误。使用经过公开审查的东西。
  • java 随机数生成器在加密方面并不安全。有了足够长的文本进行加密,就会出现允许各种信息泄漏的模式,包括泄露密码(但请参见第 3 点)和明文。
  • 您使用密码为随机数生成器提供种子。这是一个标准(而且很好)的想法,但您使用的是一种对排列不变的算法!即您的加密将“sinecure”和“不安全”或其他字谜密码视为等效(可能还有其他密码)。对于最多 16 个字母且没有超过 255 的代码点的强密码,最高可达种子为 255*16*16 = 65280;但可能性更少,因为有比这更低的种子无法到达。在我的键盘上,暴力破解仅显示 9734 种不同的密码种子,这些密码仅由键盘可写字符组成,不包括最多 16 个字符的换行符(我数为 95);每个字母不到 1 位熵。
  • CodeInChaos 在他的回答中有一些额外的观察:您正在使用流密码(更难做到正确!)。您还在加密密码,这表明您可能正在寻找散列而不是加密函数,(或者这只是一个例子?)。
  • 顺便说一下,如果您尝试存储密码;不要 - 甚至没有加密!看看索尼惨败的原因;您可能会被黑客入侵,您可能会丢失密码数据库,并且攻击者可能知道您的加密 key 。相反,使用标准的、最佳实践的密码散列(如果可能,最好使用标准的预先存在的组件)。这样的系统至少应该使用安全散列,例如 sha1 或更好的;密码应该单独加盐(salt 可以以明文形式存储),并且该过程应该在计算上进行昂贵的处理,从而使蛮力失去吸引力。见 http://chargen.matasano.com/chargen/2007/9/7/enough-with-the-rainbow-tables-what-you-need-to-know-about-s.html详情。
  • 关于cryptography - 我的密码解密类(class)有多安全?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6249149/

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