加密数据,使用UDP发送到服务器,然后解密。

huangapple go评论108阅读模式
英文:

Encrypt data, send it with UDP to a server, then decrypt it

问题

我尝试的是使用javax.crypto将字符串加密为byte[],然后通过DatagramSocket发送它,然后在接收端进行解密。

public static final String UNICODE = "UTF-8";

private SecretKey key;
private Cipher cipher;

public StringHandler() {
    try {
        key = generateKey("AES");
        cipher = Cipher.getInstance("AES");
    } catch (Exception e) {
        e.printStackTrace();
    }
}

private SecretKey generateKey(String type) throws Exception {
    KeyGenerator gen = KeyGenerator.getInstance(type);
    SecretKey key = gen.generateKey();
    return key;
}

public byte[] encrypt(String msg) {
    try {
        byte[] data = msg.getBytes(UNICODE);
        cipher.init(Cipher.ENCRYPT_MODE, key);
        return cipher.doFinal(data);
    } catch (Exception e) {
        e.printStackTrace();
        return null;
    }
}

public String decrypt(byte[] data) {
    try {
        cipher.init(Cipher.DECRYPT_MODE, key);
        return new String(cipher.doFinal(data), UNICODE);
    } catch (Exception e) {
        e.printStackTrace();
        return null;
    }
}

我在一个网站上阅读了如何加密和解密数据的方法,然后编写了这个类。从我所知,安全密钥必须在两端相同才能正确解密。是否有办法将其转换为字符串或其他形式,然后在服务器端从字符串中获取它?目前我不知道如何在不同的程序中解密它。

英文:

What I am trying to do is encrypt a string into a byte[] with javax.crypto, send it through a DatagramSocket, then on the receiving side, decrypt it.

public static final String UNICODE = "UTF-8";

private SecretKey key;
private Cipher cipher;

public StringHandler() {
	try {
		key = generateKey("AES");
		cipher = Cipher.getInstance("AES");
	} catch (Exception e) {
		e.printStackTrace();
	}
}

private SecretKey generateKey(String type) throws Exception {
	KeyGenerator gen = KeyGenerator.getInstance(type);
	SecretKey key = gen.generateKey();
	return key;
}

public byte[] encrypt(String msg) {
	try {
		byte[] data = msg.getBytes(UNICODE);
		cipher.init(Cipher.ENCRYPT_MODE, key);
		return cipher.doFinal(data);
	} catch (Exception e) {
		e.printStackTrace();
		return null;
	}
}

public String decrypt(byte[] data) {
	try {
		cipher.init(Cipher.DECRYPT_MODE, key);
		return new String(cipher.doFinal(data), UNICODE);
	} catch (Exception e) {
		e.printStackTrace();
		return null;
	}
}

I read on a website how to encrypt and decrypt data, and I wrote this class. From what I can tell, the security key has to be the same on both sides for it to decrypt properly. Is there any way to convert it to a string or something, then get it from a string on the server side? Currently I have no idea how decrypt it on a different program.

答案1

得分: 1

你可以将表示为字节数组的密钥转换为可以发送为"text"的形式。 Base64编码是常用的一种方法。

但这并不能解决您的真正问题:

> 有没有办法将其转换为字符串或其他形式,然后在服务器端从字符串中获取它?

真正的问题是如何安全地将表示秘密密钥的字符串发送到服务器;即在传输过程中没有其他人能够窃取密钥。

答案是您不能...除非使用另一种加密机制:

  • 一种可能性是使用不同的已知于客户端和服务器的秘密密钥来加密秘密密钥。
  • 第二种可能性是使用公钥加密。
  • 第三种可能性是使用安全通信系统来传输秘密密钥。例如,基于量子密码学的网络。

这是一个太大的主题,无法在此处详细讨论。如果您想了解"密钥分发问题"及其解决方案,请查找一本好的教材。或者从以下维基百科文章开始:

英文:

You can turn a key represented as an array of bytes into a form that can be sent as "text". Base64 encoding is a common way to do that.

But that doesn't solve your real problem:

> Is there any way to convert it to a string or something, then get it from a string on the server side?

The real problem is how to send the string that represents to secret key to the server securely; i.e. without someone else being able to steal the key while it is in transit.

And the answer is that you can't ... without using another encryption mechanism:

  • One possibility to encrypt the secret key with a different secret key that both the client and server already know.
  • A second possibility is to use public key encryption.
  • A third possibility is to use a secure communication system to transmit the secret key. For example a network based on quantum cryptography.

This is far too large a topic to cover here. If you want to understand the "key distribution problem" and its solutions, find a good textbook. Or start with these Wikipedia articles:

huangapple
  • 本文由 发表于 2020年8月14日 11:05:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/63405935.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定