我有很多非常小的数据(19字节)需要加密并通过tcp以加密格式发送到远程服务器.我正在使用下面的代码来执行此操作.
package aesclient;
import java.io.OutputStream;
import java.net.Socket;
import java.security.SecureRandom;
import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
public class AESClient {
static byte[] plaintext = new byte[] {0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f, 0x50, 0x51, 0x52, 0x53};
public static void main(String[] args) {
try {
Socket socket = new Socket("127.0.0.1", 1337); // connecting to server on localhost
OutputStream outputStream = socket.getOutputStream();
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5PADDING");
String s_key = "Random09" + "Random09"; // 16 …
Run Code Online (Sandbox Code Playgroud) 我有 Java AES GCM 加密问题。我已按照此页面上的说明生成以下代码:
package aes;
import java.nio.ByteBuffer;
import java.security.SecureRandom;
import javax.crypto.Cipher;
import javax.crypto.spec.GCMParameterSpec;
import javax.crypto.spec.SecretKeySpec;
public class AES {
private Cipher encryptCipher;
private Cipher decryptCipher;
private byte[] key;
private int keyLength;
private SecretKeySpec keySpec;
private SecureRandom random;
public AES(String key) {
try {
this.key = key.getBytes();
this.keyLength = this.key.length*8; // key length in bits
this.keySpec = new SecretKeySpec(this.key, "AES");
this.encryptCipher = Cipher.getInstance("AES/GCM/NoPadding");
this.decryptCipher = Cipher.getInstance("AES/GCM/NoPadding");
this.random = SecureRandom.getInstance("SHA1PRNG");
}
catch (Exception e) {
System.err.println(e.getMessage());
}
} …
Run Code Online (Sandbox Code Playgroud)