我对Java编程和密码学都比较陌生,并且只是想知道是否有人可以给我一个关于解密已经通过该算法运行的消息需要多长时间的估计。 (我绝对不会加密任何严重的事情)
static Scanner sc = new Scanner(System.in);
static char nchar; // New Character
static String osentence = ""; // The original User Input
static String esentence = ""; // The encrypted User Input
static int iteration = 0; // Iterations
static int inlength = 0;
static Random randomGenerator = new Random();
static int randomInt;
public static void main(String[] args) {
osentence = sc.nextLine(); // User Input
inlength = osentence.length();
while (iteration < inlength) {
randomInt = randomGenerator.nextInt(3);
if (randomInt == 0) {
nchar = encryptm0();
} else if (randomInt == 1) {
nchar = encryptm1();
} else if (randomInt == 2) {
nchar = encryptm2();
}
esentence = esentence + nchar; // Builds the sentence
iteration++;
}
System.out.print(esentence);
}
public static char encryptm0() {
nchar = osentence.charAt(iteration);
switch (nchar) {
case 'a':
nchar = '☻';
break;
case 'b':
nchar = '◘';
break;
case 'c':
nchar = '♪';
break;
case 'd':
nchar = 'e';
break;
case 'e':
nchar = '↨';
break;
case 'f':
nchar = '☺';
break;
case 'g':
nchar = 'm';
break;
case 'h':
nchar = '▓';
break;
case 'i':
nchar = 'Ë';
break;
case 'j':
nchar = '♥';
break;
case 'k':
nchar = '§';
break;
case 'l':
nchar = 'k';
break;
case 'm':
nchar = '¤';
break;
case 'n':
nchar = '╔';
break;
case 'o':
nchar = '╕';
break;
case 'p':
nchar = 'æ';
break;
case 'q':
nchar = 'q';
break;
case 'r':
nchar = '♫';
break;
case 's':
nchar = 'z';
break;
case 't':
nchar = '◄';
break;
case 'u':
nchar = '►';
break;
case 'v':
nchar = '♠';
break;
case 'w':
nchar = '‼';
break;
case 'x':
nchar = '☼';
break;
case 'y':
nchar = 'Θ';
break;
case 'z':
nchar = '‡';
break;
case ' ':
nchar = '₧';
case '.':
nchar = 'a';
case ',':
nchar = 'f';
case ':':
nchar = 't';
case ';':
nchar = 'w';
}
return nchar;
}
public static char encryptm1() {
nchar = osentence.charAt(iteration);
switch (nchar) {
case 'a':
nchar = 'n';
break;
case 'b':
nchar = '■';
break;
case 'c':
nchar = '♕';
break;
case 'd':
nchar = 'p';
break;
case 'e':
nchar = '→';
break;
case 'f':
nchar = 'h';
break;
case 'g':
nchar = '╓';
break;
case 'h':
nchar = '«';
break;
case 'i':
nchar = 'y';
break;
case 'j':
nchar = 'i';
break;
case 'k':
nchar = '‰';
break;
case 'l':
nchar = '*';
break;
case 'm':
nchar = 'r';
break;
case 'n':
nchar = '▬';
break;
case 'o':
nchar = '↔';
break;
case 'p':
nchar = 'v';
break;
case 'q':
nchar = '♂';
break;
case 'r':
nchar = '♀';
break;
case 's':
nchar = '○';
break;
case 't':
nchar = '◙';
break;
case 'u':
nchar = 'c';
break;
case 'v':
nchar = 'b';
break;
case 'w':
nchar = 'º';
break;
case 'x':
nchar = '↓';
break;
case 'y':
nchar = 'x';
break;
case 'z':
nchar = '?';
break;
case ' ':
nchar = '√';
case '.':
nchar = '÷';
case ',':
nchar = 'ß';
case ':':
nchar = '≡';
case ';':
nchar = 'í';
}
return nchar;
}
public static char encryptm2() {
nchar = osentence.charAt(iteration);
switch (nchar) {
case 'a':
nchar = '↑';
break;
case 'b':
nchar = '▲';
break;
case 'c':
nchar = 'g';
break;
case 'd':
nchar = 'Š';
break;
case 'e':
nchar = '⌡';
break;
case 'f':
nchar = 'l';
break;
case 'g':
nchar = '♦';
break;
case 'h':
nchar = '←';
break;
case 'i':
nchar = '╠';
break;
case 'j':
nchar = 'Δ';
break;
case 'k':
nchar = 'Ÿ';
break;
case 'l':
nchar = 'o';
break;
case 'm':
nchar = 's';
break;
case 'n':
nchar = 'd';
break;
case 'o':
nchar = '<';
break;
case 'p':
nchar = '∞';
break;
case 'q':
nchar = 'j';
break;
case 'r':
nchar = '¶';
break;
case 's':
nchar = '▼';
break;
case 't':
nchar = '↕';
break;
case 'u':
nchar = '®';
break;
case 'v':
nchar = 'u';
break;
case 'w':
nchar = '$';
break;
case 'x':
nchar = '¬';
break;
case 'y':
nchar = '♣';
break;
case 'z':
nchar = '∟';
break;
case ' ':
nchar = '%';
case '.':
nchar = '•';
case ',':
nchar = ':';
case ':':
nchar = '≈';
case ';':
nchar = 'ª';
}
return nchar;
}
}
答案 0 :(得分:0)
所以这个算法基本上是security by obscurity。关键是它只是“安全”(如果攻击者无法访问加密算法,我们会在一秒内回复)。如果攻击者确实具有访问权限,则解密是微不足道的,因为他将映射返回到原始字符。
您构造的密码是替换密码的变体,由于您的函数是单射或一对一的事实,因此存在许多攻击。您有3个字母的事实不会增加您的算法的安全性,因为每个字符明确映射到目标字母表中的其中一个字符。因此,不是将字符映射到另一个字符,而是将字符映射到一组不同的字符,并从该集合中随机选择一个元素,但这会减少为一对一的映射。
但是,如果您有a
之类的内容可以映射到b
,而c
可以映射到b
,这不是一对一的,并且没有明显的解密。
最后,你的角色映射是“模糊”字符(即大多数情况下不是a-z)的事实不会增加额外的安全性。
我赞赏你对加密的兴趣,并建议探索一次性垫和Diffie-Helman开始学习对称和非对称加密。一般来说,设计您的方案是一种很好的做法,即使每个人(攻击者)都能知道它的工作方式,并且保密的唯一部分是关键,它也是安全的。
此外,并非您使用java.util.Random
考虑到算法的安全性,但java.util.Random
不被视为加密安全。