我是任何解密的完全初学者。我写了一篇我认为应该非常安全的课程。你能否给我建设性的批评,我怎么能改进算法。
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编写的,但任何人都应该可读。
答案 0 :(得分:10)
答案 1 :(得分:9)
可怕地以多种方式打破。
encrypt(p1)
和encrypt(p2)
。然后,他可以计算与encrypt(p1)-encrypt(p2)
相同的p1-p2
。randoms
中的每个元素都是一个字节,即8位。一个字符是16位。因此,您不会添加模256.因此,您泄漏有关加密密码的信息。 为了改进它,完全抛弃你自己的算法并使用一个众所周知的,经过审查的算法。除非您是加密专家,否则创建自己的算法是个坏主意。甚至专家也会经常犯错误。
你真的需要密码解密(即它是密码存储),还是密码散列不够?
我的建议是将您的主密码放在密钥派生函数中(PKDF2是一种常见的选择)。然后使用此函数返回的密钥,使用AES加密数据文件的其余部分。