Java Encryption/Decryption
Java Encryption/Decryption
帮帮我,我无法使解密工作?
这个程序已经加密但没有解密我该如何解决这个问题?
import javax.crypto.*;
import java.util.Scanner;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.KeyGenerator;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.SecretKey;
public class CIBAw
{
public static void main(String[] argv)
{
Scanner input = new Scanner(System.in);
Scanner File = new Scanner(System.in);
try
{
KeyGenerator keygenerator = KeyGenerator.getInstance("DES");
SecretKey myDesKey = keygenerator.generateKey();
Cipher desCipher;
desCipher = Cipher.getInstance("DES");
desCipher.init(Cipher.ENCRYPT_MODE, myDesKey);
System.out.println("Encrypt a File");
System.out.print("Enter a sentence:");
//String file = File.nextLine();
byte[] text = "".getBytes();
System.out.println("" + new String(text));
// Encrypt the text
byte[] textEncrypted = desCipher.doFinal(text);
System.out.println("File Encryted : " + textEncrypted);
// Initialize the same cipher for decryption
desCipher.init(Cipher.DECRYPT_MODE, myDesKey);
// Decrypt the text
byte[] textDecrypted = desCipher.doFinal(textEncrypted);
System.out.println("File Decryted : " + new String(textDecrypted));
}catch(NoSuchAlgorithmException e)
{
e.printStackTrace();
}catch(NoSuchPaddingException e)
{
e.printStackTrace();
}catch(InvalidKeyException e)
{
e.printStackTrace();
}catch(IllegalBlockSizeException e)
{
e.printStackTrace();
}catch(BadPaddingException e)
{
e.printStackTrace();
}
}
}
好的,看完之后,我更改了代码以使其更具可读性,并为您提供解决此问题的更好技术:
- 读取输入。
- 创建密钥实例 (public/private)。
- 将输入转换为一个字节。
- 加密输入并打印出来。
解密输入并打印出来。
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
try
{
//Prompt for String
System.out.print("Enter a sentence:");
String in = input.next();
//Generate Key for encryption/decryption
KeyGenerator keygenerator = KeyGenerator.getInstance("DES");
SecretKey myDesKey = keygenerator.generateKey();
Cipher desCipher;
desCipher = Cipher.getInstance("DES");
desCipher.init(Cipher.ENCRYPT_MODE, myDesKey);
//Cast the input into bytes
byte[] text = in.getBytes();
System.out.println("" + new String(text));
// Encrypt the text
byte[] textEncrypted = desCipher.doFinal(text);
System.out.println("File Encryted : " + textEncrypted);
// Initialize the same cipher for decryption
desCipher.init(Cipher.DECRYPT_MODE, myDesKey);
// Decrypt the text
byte[] textDecrypted = desCipher.doFinal(textEncrypted);
System.out.println("File Decryted : " + new String(textDecrypted));
}catch(NoSuchAlgorithmException e)
{
e.printStackTrace();
}catch(NoSuchPaddingException e)
{
e.printStackTrace();
}catch(InvalidKeyException e)
{
e.printStackTrace();
}catch(IllegalBlockSizeException e)
{
e.printStackTrace();
}catch(BadPaddingException e)
{
e.printStackTrace();
}
}
帮帮我,我无法使解密工作? 这个程序已经加密但没有解密我该如何解决这个问题?
import javax.crypto.*;
import java.util.Scanner;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.KeyGenerator;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.SecretKey;
public class CIBAw
{
public static void main(String[] argv)
{
Scanner input = new Scanner(System.in);
Scanner File = new Scanner(System.in);
try
{
KeyGenerator keygenerator = KeyGenerator.getInstance("DES");
SecretKey myDesKey = keygenerator.generateKey();
Cipher desCipher;
desCipher = Cipher.getInstance("DES");
desCipher.init(Cipher.ENCRYPT_MODE, myDesKey);
System.out.println("Encrypt a File");
System.out.print("Enter a sentence:");
//String file = File.nextLine();
byte[] text = "".getBytes();
System.out.println("" + new String(text));
// Encrypt the text
byte[] textEncrypted = desCipher.doFinal(text);
System.out.println("File Encryted : " + textEncrypted);
// Initialize the same cipher for decryption
desCipher.init(Cipher.DECRYPT_MODE, myDesKey);
// Decrypt the text
byte[] textDecrypted = desCipher.doFinal(textEncrypted);
System.out.println("File Decryted : " + new String(textDecrypted));
}catch(NoSuchAlgorithmException e)
{
e.printStackTrace();
}catch(NoSuchPaddingException e)
{
e.printStackTrace();
}catch(InvalidKeyException e)
{
e.printStackTrace();
}catch(IllegalBlockSizeException e)
{
e.printStackTrace();
}catch(BadPaddingException e)
{
e.printStackTrace();
}
}
}
好的,看完之后,我更改了代码以使其更具可读性,并为您提供解决此问题的更好技术:
- 读取输入。
- 创建密钥实例 (public/private)。
- 将输入转换为一个字节。
- 加密输入并打印出来。
解密输入并打印出来。
public static void main(String[] args) { Scanner input = new Scanner(System.in); try { //Prompt for String System.out.print("Enter a sentence:"); String in = input.next(); //Generate Key for encryption/decryption KeyGenerator keygenerator = KeyGenerator.getInstance("DES"); SecretKey myDesKey = keygenerator.generateKey(); Cipher desCipher; desCipher = Cipher.getInstance("DES"); desCipher.init(Cipher.ENCRYPT_MODE, myDesKey); //Cast the input into bytes byte[] text = in.getBytes(); System.out.println("" + new String(text)); // Encrypt the text byte[] textEncrypted = desCipher.doFinal(text); System.out.println("File Encryted : " + textEncrypted); // Initialize the same cipher for decryption desCipher.init(Cipher.DECRYPT_MODE, myDesKey); // Decrypt the text byte[] textDecrypted = desCipher.doFinal(textEncrypted); System.out.println("File Decryted : " + new String(textDecrypted)); }catch(NoSuchAlgorithmException e) { e.printStackTrace(); }catch(NoSuchPaddingException e) { e.printStackTrace(); }catch(InvalidKeyException e) { e.printStackTrace(); }catch(IllegalBlockSizeException e) { e.printStackTrace(); }catch(BadPaddingException e) { e.printStackTrace(); } }