- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
在套接字之间用 java 编写 AES-192 算法时,我不断收到该异常,但不知道如何修复它。我认为这一定与我在其他帖子中读到的服务器解码方式有关,但我无法正确理解。我的java版本是13以防万一需要。感谢您的帮助。
*注意:有些已实现的库可能无法使用,来自之前的尝试使其正常工作
服务器代码
import java.net.*;
import java.io.*;
import java.util.*;
import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import java.util.Base64;
public class Bob {
private Socket socket = null;
private ServerSocket bob = null;
private DataInputStream in = null;
public Bob(int port) {
try {
bob = new ServerSocket(port);
socket = bob.accept();
in = new DataInputStream(new BufferedInputStream(socket.getInputStream()));
String ciphertext = "";
while(!ciphertext.equals("Stop")){
try{
ciphertext = in.readUTF();
System.out.println("Received ciphertext: " + ciphertext);
String plaintext = decrypt(ciphertext);
System.out.println("Decrypted plaintext: " + plaintext + "\n");
}catch(Exception e){
System.out.println(e);
}
}
socket.close();
in.close();
}
catch(IOException i)
{
System.out.println(i);
}
}
public static String decrypt(String encrypted) {
try {
int ivSize = 16;
int keySize = 24;
String key = "i5q5jZFd73kuK3wG2jTCvtWp";
SecretKeySpec skeySpec = new SecretKeySpec(key.getBytes("UTF-8"), "AES");
byte[] encryptedIvTextBytes = encrypted.getBytes();
byte[] iv = new byte[ivSize];
System.arraycopy(encryptedIvTextBytes, 0, iv, 0, iv.length);
IvParameterSpec ivParameterSpec = new IvParameterSpec(iv);
int ciphertextLength = encryptedIvTextBytes.length - ivSize;
byte[] ciphertextB = new byte[ciphertextLength];
System.arraycopy(encryptedIvTextBytes,ivSize,ciphertextB,0,ciphertextLength);
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5PADDING");
cipher.init(Cipher.DECRYPT_MODE, skeySpec, ivParameterSpec);
byte[] original = cipher.doFinal(Base64.getDecoder().decode(ciphertextB));
eturn new String(original);
} catch (Exception e) {
System.out.println(e);
}
return null;
}
public static void main(String args[]) {
Bob bob = new Bob(3125);
}
}
客户端代码
import java.net.*;
import java.io.*;
import javax.crypto.*;
import java.security.*;
import java.util.*;
import javax.crypto.Cipher;
import javax.crypto.Mac;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import java.security.MessageDigest;
import java.security.SecureRandom;
import java.util.Base64;
public class Alice {
private Socket socket = null;
private DataOutputStream out = null;
private BufferedReader in = null;
public Alice(String address, int port) {
try {
socket = new Socket(address, port);
in = new BufferedReader(new InputStreamReader(System.in));
out = new DataOutputStream(socket.getOutputStream());
} catch(UnknownHostException u) {
System.out.println(u);
} catch(IOException i) {
System.out.println(i);
}
String plaintext = "";
while(!plaintext.equals("Stop")){
try {
System.out.println("Introduce plaintext:");
plaintext = in.readLine();
String ciphertext = encrypt(plaintext);
System.out.println("Encrypted: " + ciphertext + "\n");
out.writeUTF(ciphertext);
} catch(IOException i) {
System.out.println(i);
}
}
try {
socket.close();
in.close();
out.close();
} catch(IOException i) {
System.out.println(i);
}
}
public static String encrypt(String value) {
try {
String key = "i5q5jZFd73kuK3wG2jTCvtWp";
SecretKeySpec skeySpec = new SecretKeySpec(key.getBytes("UTF-8"), "AES");
int ivSize = 16;
byte[] iv = new byte[ivSize];
SecureRandom random = new SecureRandom();
random.nextBytes(iv);
IvParameterSpec ivParameterSpec = new IvParameterSpec(iv);
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5PADDING");
cipher.init(Cipher.ENCRYPT_MODE, skeySpec, ivParameterSpec);
byte[] encrypted = cipher.doFinal(value.getBytes());
byte[] encryptedAndIV = new byte[ivSize + encrypted.length];
System.arraycopy(iv, 0, encryptedAndIV, 0, ivSize);
System.arraycopy(encrypted, 0, encryptedAndIV, ivSize, encrypted.length);
return new String(Base64.getEncoder().encode(encryptedAndIV));
} catch (Exception e) {
throw new RuntimeException("Error occured while encrypting data", e);
}
}
public static void main(String args[]) {
Alice alice = new Alice("localhost", 3125);
}
}
最佳答案
您的问题出在解密例程中。通常,在 CBC 中您必须遵循
加密;
size=IV.lenght+encrypted.lenght
将 IV 添加到更大的字节数组中 IV||encrypted
将加密字节附加到更大的字节中base64(IV||加密
)解密;
在您的代码中,您没有在解密之前对字节进行解码以提取 IV,而是在解密过程中进行了解码。
ByteBuffer buffer = ByteBuffer.wrap(new Base64().decode(encryptedText));
//get the IV from the bytes
byte[] iv = new byte[ivSize];
buffer.get(iv, 0, iv.length);
IvParameterSpec ivParameterSpec = new IvParameterSpec(iv);
//get the encrypted bytes
byte[] encryptedTextBytes = new byte[buffer.capacity() - iv.length];
buffer.get(encryptedTextBytes);
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5PADDING");
cipher.init(Cipher.DECRYPT_MODE, skeySpec, ivParameterSpec);
byte[] original = cipher.doFinal(encryptedTextBytes);
关于java - 不断收到 javax.crypto.IllegalBlockSizeException : Input length must be multiple of 16 when decrypting with padded cipher,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60405749/
如果这不是一个错误,那就是另一个错误。如果不是那样的话,那就是别的东西了。我觉得我的项目已经改变了很多,现在只是试图解决代码签名问题,结果一切都搞砸了。我严格按照说明进行操作,但出现错误,例如当前的“
我不确定是否有一些我不知道的内置变量或规则,或者 make 是否有问题,或者我只是疯了。 对于我的一个项目,我有一个如下的 makefile: CC=g++ CFLAGS=-O3 `libpng-co
我有大约 10 个 div,它们必须不断翻转,每个 div 延迟 3 秒 这个 codrops 链接的最后一个效果是我正在寻找的,但无需单击 div http://tympanus.net/Devel
我如何使用 jQuery 持续运行 PHP 脚本并每秒获取响应,以及将鼠标上的少量数据发送到同一脚本? 我真的必须添加一些随机扩展才能让这么简单的计时器工作吗? 最佳答案 To iterate is
JBoss 4.x EJB 3.0 我见过如下代码(大大简化): @Stateless @TransactionAttribute(TransactionAttributeType.NOT_SUPPO
使用 PHPStorm,我试图忽略每次尝试进行 git 提交时 pop 的 workspace.xml。 我的 .gitignore 看起来像: /.idea/ .idea/workspace.xml
我是一名优秀的程序员,十分优秀!