" + decrypted + "(" + k-6ren">
gpt4 book ai didi

java - 我的逆向加密代码有什么问题吗?

转载 作者:行者123 更新时间:2023-11-30 08:18:22 24 4
gpt4 key购买 nike

我必须编写一个程序,使用字母位移来加密用户输入的字符串。加密代码如下所示:

for (int j = 0; j<key; j++) {
for (i = j; i <length; i += key)
{
password += phrase.charAt(i);
}
}

这正如我所期望的那样。

例如,当我使用 key 长度为 3 时,它会发生变化

Testing1234567890ABCDEF

Ttg369BEei1470CFsn258AD

我的问题是当我尝试撤消它时。

for (int j = 0; j<=Math.ceil(phrase.length()/key); j++){
for (int i = j; i < phrase.length(); i += Math.ceil((phrase.length()/key))+1)
{
password += phrase.charAt(i);
}
}

这适用于该键上的该短语,但如果我使用不同的短语或不同的键,则不起作用。

我错过了什么?

最佳答案

看一下结果 - 请注意,只有当字符串长度和 key 值互质时,解密才是正确的。

public String encrypt(String s, int key) {
String encrypted = "";
for (int j = 0; j < key; j++) {
for (int i = j; i < s.length(); i += key) {
encrypted += s.charAt(i);
}
}
return encrypted;
}

public String decrypt(String encrypted, int key) {
String decrypted = "";
for (int j = 0; j <= Math.ceil(encrypted.length() / key); j++) {
for (int i = j; i < encrypted.length(); i += Math.ceil((encrypted.length() / key)) + 1) {
decrypted += encrypted.charAt(i);
}
}
return decrypted;
}

private static int gcd(int a, int b) {
return BigInteger.valueOf(a).gcd(BigInteger.valueOf(b)).intValue();
}

private void test(String s) {
for (int key = 1; key < 10; key++) {
String encrypted = encrypt(s, key);
String decrypted = decrypt(encrypted, key);
boolean good = decrypted.equals(s);
System.out.println(s + " -> " + encrypted + " -> " + decrypted + "(" + key + ")" + " gcd(" + s.length() + "," + key + ") = " + gcd(s.length(), key) + " " + (good ? "*" : ""));

}
}

public void test() {
test("Testing1234567890ABCDEF");
test("Testing1234567890ABCDEFG");

}

打印

Testing1234567890ABCDEF -> Testing1234567890ABCDEF -> Testing1234567890ABCDEF(1) gcd(23,1) = 1 *
Testing1234567890ABCDEF -> Tsig24680BDFetn13579ACE -> Testing1234567890ABCDEF(2) gcd(23,2) = 1 *
Testing1234567890ABCDEF -> Ttg369BEei1470CFsn258AD -> Testing1234567890ABCDEF(3) gcd(23,3) = 1 *
Testing1234567890ABCDEF -> Ti260Den37AEsg48BFt159C -> Testing1234567890ABCDEF(4) gcd(23,4) = 1 *
Testing1234567890ABCDEF -> Tn49Deg50Es16AFt27Bi38C -> Test3ng1284567C90ABDEFi(5) gcd(23,5) = 1
Testing1234567890ABCDEF -> Tg6Be17Cs28Dt39Ei40Fn5A -> Testing1234567890ABCDEF(6) gcd(23,6) = 1 *
Testing1234567890ABCDEF -> T18Ee29Fs30t4Ai5Bn6Cg7D -> Tes4Bg123An7890i6DEFt5C(7) gcd(23,7) = 1
Testing1234567890ABCDEF -> T20e3As4Bt5Ci6Dn7Eg8F19 -> Testing1234567890ABCDEF(8) gcd(23,8) = 1 *
Testing1234567890ABCDEF -> T3Be4Cs5Dt6Ei7Fn8g9102A -> Testin923456781ABCDEFg0(9) gcd(23,9) = 1
Testing1234567890ABCDEFG -> Testing1234567890ABCDEFG -> Testing1234567890ABCDEFG(1) gcd(24,1) = 1 *
Testing1234567890ABCDEFG -> Tsig24680BDFetn13579ACEG -> Ttsni1g32547698A0CBEDGFe(2) gcd(24,2) = 2
Testing1234567890ABCDEFG -> Ttg369BEei1470CFsn258ADG -> Ti2t15g4837A60D9CGBFEsen(3) gcd(24,3) = 3
Testing1234567890ABCDEFG -> Ti260Den37AEsg48BFt159CG -> Tn49i38C27BG6AF0EtDs1eg5(4) gcd(24,4) = 4
Testing1234567890ABCDEFG -> Tn49Deg50Es16AFt27BGi38C -> Testing1234567890ABCDEFG(5) gcd(24,5) = 1 *
Testing1234567890ABCDEFG -> Tg6Be17Cs28Dt39Ei40Fn5AG -> T18Eng7Di56Ct4ABs30Ge29F(6) gcd(24,6) = 6
Testing1234567890ABCDEFG -> T18Ee29Fs30Gt4Ai5Bn6Cg7D -> Test5C1234Bg890An7EFGi6D(7) gcd(24,7) = 1
Testing1234567890ABCDEFG -> T20e3As4Bt5Ci6Dn7Eg8F19G -> T3Bi7F2At6E10s5Dg9e4Cn8G(8) gcd(24,8) = 8
Testing1234567890ABCDEFG -> T3Be4Cs5Dt6Ei7Fn8Gg9102A -> Testing034567892BCDEFG1A(9) gcd(24,9) = 3

但是,仅仅因为它们相对素数并不意味着加密/解密就会起作用。

还在看。

关于java - 我的逆向加密代码有什么问题吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29341865/

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