java中如何通过网络发送blowfish密钥
我有一个Java项目通过无线网络对图像进行身份验证。我正在使用河豚对图像进行加密。我面临的问题是如何将河豚对称密钥发送给接收者,以便他能够解密图像。我对密码学比较陌生。请附上代码片段来说明同样的问题。java中如何通过网络发送blowfish密钥,java,network-programming,encryption-symmetric,Java,Network Programming,Encryption Symmetric,我有一个Java项目通过无线网络对图像进行身份验证。我正在使用河豚对图像进行加密。我面临的问题是如何将河豚对称密钥发送给接收者,以便他能够解密图像。我对密码学比较陌生。请附上代码片段来说明同样的问题。 package ClientModule; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream;
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");//,"");
}
}
包客户端模块;
导入java.io.File;
导入java.io.FileInputStream;
导入java.io.FileNotFoundException;
导入java.io.FileOutputStream;
导入java.io.IOException;
导入java.io.InputStream;
导入java.io.OutputStream;
导入java.security.InvalidKeyException;
导入java.security.NoSuchAlgorithmException;
导入javax.crypto.BadPaddingException;
导入javax.crypto.Cipher;
导入javax.crypto.IllegalBlockSizeException;
导入javax.crypto.KeyGenerator;
导入javax.crypto.NoSuchPaddingException;
导入javax.crypto.SecretKey;
导入javax.swing.*;
公共类ImageEncryption\u客户端
{
KeyGenerator-KeyGenerator=null;
public static SecretKey SecretKey=null;
公共静态密码=null;
ImageEncryption_客户端(){
试一试{
/**
*创建一个河豚键
*/
keyGenerator=keyGenerator.getInstance(“河豚”);
secretKey=keyGenerator.generateKey();
System.out.println(secretKey.toString());
/**
*创建提及算法名称的cipher实例
*-河豚
*/
cipher=cipher.getInstance(“河豚”);
System.out.println(cipher.toString());
}捕获(NoSuchPaddingException ex){
系统输出打印项次(ex);
}捕获(nosuchalgorithmex异常){
系统输出打印项次(ex);
}
}
私有void加密(字符串srcPath、字符串destPath)
{
File rawFile=新文件(srcPath);
文件加密文件=新文件(destPath);
InputStream inStream=null;
OutputStream outStream=null;
试一试{
/**
*初始化密码以进行加密
*/
cipher.init(cipher.ENCRYPT_模式,secretKey);
/**
*初始化输入和输出流
*/
inStream=新文件输入流(rawFile);
outStream=新文件OutputStream(encryptedFile);
字节[]缓冲区=新字节[1024];
内伦;
而((len=inStream.read(buffer))>0){
outStream.write(密码更新(缓冲区,0,len));
冲水;
}
outStream.write(cipher.doFinal());
流内关闭();
exptream.close();
}捕获(非法块大小例外){
showMessageDialog(null,“发生异常”,“异常”,JOptionPane.ERROR\u消息);
系统输出打印项次(ex);
}捕获(BadPaddingException ex){
showMessageDialog(null,“发生异常”,“异常”,JOptionPane.ERROR\u消息);
系统输出打印项次(ex);
}捕获(InvalidKeyException ex){
showMessageDialog(null,“发生异常”,“异常”,JOptionPane.ERROR\u消息);
系统输出打印项次(ex);
}捕获(FileNotFoundException ex){
showMessageDialog(null,“发生异常”,“异常”,JOptionPane.ERROR\u消息);
系统输出打印项次(ex);
}捕获(IOEX异常){
showMessageDialog(null,“发生异常”,“异常”,JOptionPane.ERROR\u消息);
系统输出打印项次(ex);
}
}
void enc(字符串文件名)/,字符串目录)
{
字符串fileToEncrypt=文件名;
字符串arr[]=filename.split(“\\”);
字符串encryptedFile=arr[0]+“_加密。”+arr[1];
String directoryPath=“C:\\Users\\Public\\Pictures\\Sample Pictures\\”;
加密(directoryPath+fileToEncrypt,
目录路径+加密文件);
}
公共静态无效主管道(字符串…kkk)
{
新的ImageEncryption_Client().enc(“Koala.jpg”);/,“”);
}
}
首先,如果不做更多的研究或咨询安全专家,我不会涉足这一领域
专门针对开发人员的一个非常好的加密资源是:
现在回答你的问题。我首先要问的是,您是否真的需要进行密钥交换,或者是否可以在双方都设置一个预共享的秘密/密钥对
密钥交换
密钥交换是您要问的一类问题。有许多实现:
Diffie Hellman是SSL和TLS中流行的经典方法:
Diffie Hellman的java实现示例如下:
预共享密钥
密钥交换非常复杂,需要您拥有通信的双方。另一种方法是在通信的两端放置一个共享的秘密。最简单的方法是共享对称密钥
更安全的方法是使用非对称密钥(公钥加密),其中每一方生成自己的公钥-私钥对,然后与另一方预先共享公钥。因此,在Alice和Bob希望安全通信的经典示例中,Alice将向Bob提供公钥,Bob将使用公钥加密发送给Alice的所有数据。然后她可以用她的私钥解密。这种方法的优点是,如果任何一方受到攻击,攻击者就无法读取为另一方加密的消息。更多详情如下:
第三种方法是在前一种方法的基础上,为每个图像生成一个对称密钥,并使用该密钥对图像进行加密。然后使用您所属方的公钥加密对称密钥