我想使用 AES256 对称加密我的 iPhone 应用程序和我的 Java 服务器套接字。我目前正在使用 Rob Napier 的 RNCryptor/JNCryptor 库。iPhone 上的加密似乎运行良好,因为我能够再次解密加密的字符串。但是,一旦我尝试解密 Java 服务器套接字上的字符串,就会引发以下异常:
com.acme.crypto.InvalidHMACException: Incorrect HMAC value.
at com.acme.crypto.AES256JNCryptor.decryptV3Data(AES256JNCryptor.java:248)
at com.acme.crypto.AES256JNCryptor.decryptV3Data(AES256JNCryptor.java:323)
at com.acme.crypto.AES256JNCryptor.decryptData(AES256JNCryptor.java:280)
com.acme.crypto.CryptorException: Unrecognised version number: 61.
at com.acme.crypto.AES256JNCryptor.decryptData(AES256JNCryptor.java:283)
以下是用于发送加密数据的相关客户端代码片段(iOS/Objective-C):
// add line break and send message to server
NSString* message = [NSString stringWithFormat:@"%@\n", output];
NSData* data = [[NSData alloc] initWithData:[message dataUsingEncoding:NSUTF8StringEncoding
allowLossyConversion:NO]];
// encrypt outgoing data with AES-256
NSError *error1;
NSData *cypher = [RNEncryptor encryptData:data
withSettings:kRNCryptorAES256Settings
password:@"mypassword"
error:&error1];
// write encrypted data to output stream
if (error1==nil) {
NSInteger result = [outputStream write:[cypher bytes] maxLength:[cypher length]];
} else {
NSLog(@"Encryption of outgoing data failed: %@", error1);
}
这是在其套接字(Linux/Java)上接收加密数据的相应服务器代码:
// initializing cryptor object during object construction
JNCryptor cryptor = new AES256JNCryptor();
// setting up the input stream on the server socket
BufferedReader input = new BufferedReader(new InputStreamReader(socket.getInputStream(), StandardCharsets.UTF_8));
// this is within the client thread ...
String line;
while((line=input.readLine())!=null) {
try {
// ... the exception is thrown at the following line ...
byte[] decrypted = cryptor.decryptData(line.getBytes(), password.toCharArray());
line = new String(decrypted, StandardCharsets.UTF_8);
// message handling ...
} catch (Exception ex) {
// print exception ...
}
}
有人知道我做错了什么吗?在发送数据之前,我是否必须使用 Base64 或类似的编码?任何帮助高度赞赏。
编辑:这是解决方案。我现在不使用基于字符的输入流,而是直接使用套接字中的 InputStream 来读取原始字节以提供解密算法:
@Override
public void run() {
try {
int bytes;
byte[] buffer = new byte[4096];
while((bytes=input.read(buffer))!=-1) {
try {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
baos.write(buffer, 0, bytes);
byte[] decrypted = cryptor.decryptData(baos.toByteArray(), password.toCharArray());
String line = new String(decrypted, StandardCharsets.UTF_8);
// handle input ...
} catch (Exception ex) {
// handle exception ...
}
}
} catch (Exception ex) {
// handle exception ...
}
}