我有一个Java项目通过无线网络对图像进行身份验证。我正在使用河豚加密图像。我面临的问题是如何将接收器的对称密钥发送给接收器,以便他可以解密图像。我对密码学相对来说比较陌生。请包括代码片段来说明相同的情况。 如何通过java中的网络发送河豚键
package ClientModule;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
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;
import javax.swing.*;
public class ImageEncryption_Client
{
KeyGenerator keyGenerator = null;
public static SecretKey secretKey = null;
public static Cipher cipher = null;
ImageEncryption_Client(){
try {
/**
* Create a Blowfish key
*/
keyGenerator = KeyGenerator.getInstance("Blowfish");
secretKey = keyGenerator.generateKey();
System.out.println(secretKey.toString());
/**
* Create an instance of cipher mentioning the name of algorithm
* - Blowfish
*/
cipher = Cipher.getInstance("Blowfish");
System.out.println(cipher.toString());
} catch (NoSuchPaddingException ex) {
System.out.println(ex);
} catch (NoSuchAlgorithmException ex) {
System.out.println(ex);
}
}
private void encrypt(String srcPath, String destPath)
{
File rawFile = new File(srcPath);
File encryptedFile = new File(destPath);
InputStream inStream = null;
OutputStream outStream = null;
try {
/**
* Initialize the cipher for encryption
*/
cipher.init(Cipher.ENCRYPT_MODE, secretKey);
/**
* Initialize input and output streams
*/
inStream = new FileInputStream(rawFile);
outStream = new FileOutputStream(encryptedFile);
byte[] buffer = new byte[1024];
int len;
while ((len = inStream.read(buffer)) > 0) {
outStream.write(cipher.update(buffer, 0, len));
outStream.flush();
}
outStream.write(cipher.doFinal());
inStream.close();
outStream.close();
} catch (IllegalBlockSizeException ex) {
JOptionPane.showMessageDialog(null,"An Exception Occurred","Exception",JOptionPane.ERROR_MESSAGE);
System.out.println(ex);
} catch (BadPaddingException ex) {
JOptionPane.showMessageDialog(null,"An Exception Occurred","Exception",JOptionPane.ERROR_MESSAGE);
System.out.println(ex);
} catch (InvalidKeyException ex) {
JOptionPane.showMessageDialog(null,"An Exception Occurred","Exception",JOptionPane.ERROR_MESSAGE);
System.out.println(ex);
} catch (FileNotFoundException ex) {
JOptionPane.showMessageDialog(null,"An Exception Occurred","Exception",JOptionPane.ERROR_MESSAGE);
System.out.println(ex);
} catch (IOException ex) {
JOptionPane.showMessageDialog(null,"An Exception Occurred","Exception",JOptionPane.ERROR_MESSAGE);
System.out.println(ex);
}
}
void enc(String filename)//, String dir)
{
String fileToEncrypt = filename;
String arr[]=filename.split("\\.");
String encryptedFile = arr[0]+"_encrypted."+arr[1];
String directoryPath = "C:\\Users\\Public\\Pictures\\Sample Pictures\\";
encrypt(directoryPath + fileToEncrypt,
directoryPath + encryptedFile);
}
public static void main(String... kkk)
{
new ImageEncryption_Client().enc("Koala.jpg");//,"");
}
}