加密代码
path = chooser.getSelectedFile();
String x = path.toString();
try {
Scanner scanner = new Scanner(new File(x));
content = scanner.useDelimiter("\\Z").next();
} catch (FileNotFoundException e2) {
e2.printStackTrace();
}
try{
KeyGenerator keygenerator = KeyGenerator.getInstance("DES");
SecretKey myDesKey = keygenerator.generateKey();
Cipher desCipher;
desCipher = Cipher.getInstance("DES");
byte[] text = content.getBytes("UTF8");
desCipher.init(Cipher.ENCRYPT_MODE, myDesKey);
byte[] textEncrypted = desCipher.doFinal(text);
String s = new String(textEncrypted);
try {
File statText = new File(x);
FileOutputStream is = new FileOutputStream(statText);
OutputStreamWriter osw = new OutputStreamWriter(is);
Writer w = new BufferedWriter(osw);
w.write(s);
w.close();
}
catch (IOException e3) {
JOptionPane.showMessageDialog(null, "something is wrong with the txt file");
}
}catch(Exception e1)
{
System.out.println("En");
}
这里是解密部分:
path = chooser.getSelectedFile();
String x = path.toString();
try {
Scanner scanner = new Scanner(new File(x));
content = scanner.useDelimiter("\\Z").next();
} catch (FileNotFoundException e2) {
e2.printStackTrace();
}
try{
KeyGenerator keygenerator = KeyGenerator.getInstance("DES");
SecretKey myDesKey = keygenerator.generateKey();
Cipher desCipher;
desCipher = Cipher.getInstance("DES");
byte[] text = content.getBytes("UTF8");
byte[] textEncrypted = desCipher.doFinal(text);
desCipher.init(Cipher.DECRYPT_MODE, myDesKey);
byte[] textDecrypted = desCipher.doFinal(textEncrypted);
String s = new String(textDecrypted);
System.out.println(s);
}catch(Exception e1)
{
System.out.println("De"); /* here we go this is my error*/
}
第二个代码的错误" catch(例外e1)"
答案 0 :(得分:0)
我在你的代码中看到两个问题。在您的第一部分中,您只发送了加密消息,接收方不会解密,因为您没有将密钥发送到接收方。第二个问题是你使用String s = new String(textEncrypted);
,你应该发送textEncrypted
而不是字符串,因为将字节转换为字符串可能会因编码而丢失一些信息。
在第二部分中,您应该在加密前致电desCipher.init(Cipher.ENCRYPT_MODE, myDesKey);
。
答案 1 :(得分:0)
问题解决了
加密代码:
path = chooser.getSelectedFile();
String x = path.toString();
try {
scanner = new Scanner(new File(x));
content = scanner.useDelimiter("\\Z").next();
} catch (FileNotFoundException e2) {
e2.printStackTrace();
}
try{
String desKey = "0123456789abcdef";
byte[] keyBytes = DatatypeConverter.parseHexBinary(desKey);
SecretKeyFactory factory = SecretKeyFactory.getInstance("DES");
SecretKey key = factory.generateSecret(new DESKeySpec(keyBytes));
desCipher = Cipher.getInstance("DES");
byte[] text = content.getBytes();
desCipher.init(Cipher.ENCRYPT_MODE, key);
byte[] textEncrypted = desCipher.doFinal(text);
String s = new String(textEncrypted);
try {
File statText = new File(x);
FileOutputStream is = new FileOutputStream(statText);
OutputStreamWriter osw = new OutputStreamWriter(is);
Writer w = new BufferedWriter(osw);
w.write(s);
w.close();
}
catch (IOException e3) {
JOptionPane.showMessageDialog(null, "something is wrong with the txt file");
}
}catch(Exception e1)
{
System.out.println("relax and try again");
}
解密代码:
path = chooser.getSelectedFile();
String x = path.toString();
try {
Scanner scanner = new Scanner(new File(x));
content = scanner.useDelimiter("\\Z").next();
} catch (FileNotFoundException e2) {
e2.printStackTrace();
}
try{
String desKey = "0123456789abcdef";
byte[] keyBytes = DatatypeConverter.parseHexBinary(desKey);
SecretKeyFactory factory = SecretKeyFactory.getInstance("DES");
SecretKey key = factory.generateSecret(new DESKeySpec(keyBytes));
desCipher = Cipher.getInstance("DES");
byte[] text = content.getBytes();
desCipher.init(Cipher.DECRYPT_MODE, key);
byte[] textEncrypted = desCipher.doFinal(text);
String s = new String(textEncrypted);
try {
File statText = new File(x);
FileOutputStream is = new FileOutputStream(statText);
OutputStreamWriter osw = new OutputStreamWriter(is);
Writer w = new BufferedWriter(osw);
w.write(s);
w.close();
}
catch (IOException e3) {
JOptionPane.showMessageDialog(null, "something is wrong with the txt file");
}
}catch(Exception e24)
{
System.out.println("relax and try again");
}
谢谢大家。