gpt4 book ai didi

Java:DES/ECB 加密始终产生相同的密文

转载 作者:行者123 更新时间:2023-12-01 18:16:38 28 4
gpt4 key购买 nike

我最近编写了一个简单的 java 应用程序,尝试使用 ECB CBC 加密模式,并且我注意到我的代码工作方式有些奇怪。由于某种原因,我生成的密文将始终产生相同的字符串,并且我注意到 ECB 和 CBC 模式之间生成的密文几乎没有差异。我不确定这是否是正常行为,因此如果有人能对此有所了解,我将不胜感激。

import java.security.*;
import java.util.Scanner;

import javax.crypto.*;
import javax.crypto.spec.IvParameterSpec;

public class MainApp
{
static Scanner sc = new Scanner(System.in);
public KeyGenerator keygen;
public SecretKey secKey;
Cipher c;

static SecureRandom rnd = new SecureRandom();
static IvParameterSpec iv = new IvParameterSpec(rnd.generateSeed(8));

public static void main(String[] args) throws Exception
{
MainApp theApp = new MainApp();
theApp.start();
}

public void start() throws Exception
{
keygen = KeyGenerator.getInstance("DES");
secKey = keygen.generateKey();

System.out.println(secKey);

boolean success = false;
boolean success2 = false;
boolean exit = false;
int type = 0;

do
{
do
{
System.out.println("Weclome to the DES Encryption/Decription zone!");
System.out.println("What form of mode do you wish to use? [E]CB or [C]BC? Type [Q]uit to exit");
String input = sc.nextLine();

if(input.equalsIgnoreCase("e")){

type = 1;

do{
System.out.println("Do you wish to use padding? [Y]es or [N]o?");
input = sc.nextLine();

if(input.equalsIgnoreCase("y")){
c = Cipher.getInstance("DES/ECB/PKCS5Padding");
success = true;
success2 = true;
}
else if(input.equalsIgnoreCase("n")){
c = Cipher.getInstance("DES/ECB/NoPadding");
success = true;
success2 = true;
}
else{
System.out.println("Error - please enter a valid input");
success = false;
success2 = false;
}
}while(!success2);

}
else if(input.equalsIgnoreCase("c")){

type = 2;

do{
System.out.println("Do you wish to use padding? [Y]es or [N]o?");
input = sc.nextLine();

if(input.equalsIgnoreCase("y")){
c = Cipher.getInstance("DES/CBC/PKCS5Padding");
success = true;
success2 = true;
}
else if(input.equalsIgnoreCase("n")){
c = Cipher.getInstance("DES/CBC/NoPadding");
success = true;
success2 = true;
}
else{
System.out.println("Error - please enter a valid input");
success = false;
success2 = false;
}
}while(!success2);
}

else if(input.equalsIgnoreCase("q")){
System.out.println("Thanks for using me!");
System.exit(0);
success = true;
exit = true;
}
else{
System.out.println("Error - please enter a valid input");
success = false;
}
}while(!success);


System.out.println("Input what you wish to encrypt");
String input = sc.nextLine();

byte[] text = input.getBytes();

System.out.println(type);

System.out.println("--------------------------------------------");

System.out.println("Text : " + new String(text));

byte[] textEncrypted = encrypt(text, c, type);

System.out.println("Text Encrypted : " + textEncrypted);

byte[] textDecrypted = decrypt(textEncrypted, c, type);

System.out.println("Text Decrypted : " + new String(textDecrypted));

System.out.println("--------------------------------------------");

}while(!exit);
}

public byte[] encrypt(byte[] b, Cipher c, int type) throws Exception
{
if(type == 1)
{
c.init(Cipher.ENCRYPT_MODE, secKey);
}
else if(type == 2)
{
c.init(Cipher.ENCRYPT_MODE, secKey, iv);
}
byte[] encryptedText = null;
try {
encryptedText = c.doFinal(b);
} catch (IllegalBlockSizeException e) {
System.out.println("ERROR - If you have selected to not automatically pad your plaintext it must be a mutiple of eight bytes to be accepted. Exiting program");
System.exit(0);
}

return encryptedText;
}

public byte[] decrypt(byte[] b, Cipher c, int type) throws Exception
{
if(type == 1)
{
c.init(Cipher.DECRYPT_MODE, secKey);
}
else if(type == 2)
{
c.init(Cipher.DECRYPT_MODE, secKey, iv);
}

byte[] decryptedText = c.doFinal(b);

return decryptedText;

}


}

编辑:这是我的应用程序输出的示例。经过仔细检查,它似乎偶尔会循环显示一些不同结果的集合,但我可以看出它们由于某种原因而被重复。我之前见过 [B@3b2bad06 以及 [B@306c8343] 作为密文出现。

com.sun.crypto.provider.DESKey@184ba
Weclome to the DES Encryption/Decription zone!
What form of mode do you wish to use? [E]CB or [C]BC? Type [Q]uit to exit
e
Do you wish to use padding? [Y]es or [N]o?
y
Input what you wish to encrypt
testtext
1
--------------------------------------------
Text : testtext
Text Encrypted : [B@2aa75818
Text Encrypted : [B@2aa75818
Text Decrypted : testtext
--------------------------------------------
Weclome to the DES Encryption/Decription zone!
What form of mode do you wish to use? [E]CB or [C]BC? Type [Q]uit to exit
e
Do you wish to use padding? [Y]es or [N]o?
y
Input what you wish to encrypt
testtext
1
--------------------------------------------
Text : testtext
Text Encrypted : [B@5088a588
Text Encrypted : [B@5088a588
Text Decrypted : testtext
--------------------------------------------
Weclome to the DES Encryption/Decription zone!
What form of mode do you wish to use? [E]CB or [C]BC? Type [Q]uit to exit
e
Do you wish to use padding? [Y]es or [N]o?
y
Input what you wish to encrypt
sometext
1
--------------------------------------------
Text : sometext
Text Encrypted : [B@3b2bad06
Text Encrypted : [B@3b2bad06
Text Decrypted : sometext
--------------------------------------------
Weclome to the DES Encryption/Decription zone!
What form of mode do you wish to use? [E]CB or [C]BC? Type [Q]uit to exit
e
Do you wish to use padding? [Y]es or [N]o?
y
Input what you wish to encrypt
sometext
1
--------------------------------------------
Text : sometext
Text Encrypted : [B@306c8343
Text Encrypted : [B@306c8343
Text Decrypted : sometext
--------------------------------------------

最佳答案

转换为十六进制后打印加密的字符串: How to convert a byte array to a hex string in Java?

您正在打印加密字节数组的哈希表示,而不是其内容。

关于Java:DES/ECB 加密始终产生相同的密文,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29129256/

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