Java: DES/ECB 加密总是产生相同的密文
Java: DES/ECB encryption always results in same ciphertext
我最近编写了一个简单的 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 应用程序来试验 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?
您正在打印加密字节数组的哈希表示,而不是其内容。