- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我想使用 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 ...
}
}
最佳答案
InvalidHMACException
表示您的密码不正确或您的数据已损坏。
(我的 Java 有点弱,但我相当确定我对此处文档的理解是正确的。)
我怀疑您的问题是您正在使用 InputStreamReader
。这从字节流桥接到字符流(在你的例子中是 UTF-8 流)。密文是完全随机的字节,在大多数情况下是非法的 UTF-8 字符(不仅是“乱码”,而且实际上是非法的且不可解码)。我当然希望在 bytes->utf8->bytes 的往返过程中出现损坏。
您根本不需要在这里使用 InputStreamReader
。摆脱它。
关于java - 为什么 JNCryptor/RNCryptor 库会抛出 InvalidHMACException?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32116108/
我以下面的代码为例。如果我错了,请帮助我。 - (void)storeToKeychain { kPassword = [self computeSHA256DigestForString:[NSSt
我正在处理一个项目,我们需要加密一些用户凭据(用户名、密码、用户 ID 等)以传递到我们的服务器。我们决定使用 AES256 来实现这一点。在对这个主题进行一些研究时,很难避免注意到那里的一个项目,R
我有一些用 RNCryptor 加密的图像文件,我想解密它们并将它们用作 Framelayout 的背景。我浏览了样本文件,但我的应用程序在一段时间后关闭并且没有显示任何图像。我用 JNCryptor
我的应用中有很多短字符串需要解密,解密速度很关键。 目前我正在试验 RNCryptor 但发现它的默认设置对于我的用例来说有点慢。否则它是一个很棒的框架:-) 加密只会进行一次,因此其性能并不重要。
这是 Asynchronously decrypt a large file with RNCryptor on iOS 的后续行动 我已经成功地使用这篇文章中描述的方法异步解密了一个大型下载文件 (
我用过RNCryptor在 Objective-C 中成功,现在需要从 Javascript 加密我的 iOS 应用程序的数据。但是,这个简单的测试失败了...... function testE
我有一个 iOS7 应用程序,它使用 RNCryptor 来保持它的文档安全地隐藏起来。 问题是,由于该应用程序有点偏执,它非常自由地使用加密,这似乎导致偶尔的双重加密。 (出于复杂的原因,主要与背景
解密时出现错误:无法执行操作 RNCryptorError 1我不明白我做错了什么。这是我的代码块 最佳答案 对于可能在此处搜索的任何人:这是 RNCryptor#174 的副本,您可能也想在那里阅读
我正在使用最新的 RNCryptor 加密文件数据,然后将其保存到磁盘。当我尝试加密大文件(超过 150MB)时,我收到内存警告并且内存增加得非常快。 我尝试了以下解决方案,但没有一个对我有用: Me
我尝试将 RNCryptor 导入我的项目以加密一些字符串。但是我很难理解如何将其导入到我的 objective-c 项目中。我通过拖放到我的项目中复制了 RNCryptor/RNCryptor.sw
我在 iOS 中使用 RNCryptor 成功加密/解密数据。 我正在尝试获取要发送到服务器的公钥,以便它可以加密一些数据。 NSString *saltString = @"salt'n'peppa
我已经使用此代码通过 RNcryptor 成功加密了一个文本文件 NSFileManager *filemgr; NSData *databuffer; filemgr = [NSFileManage
iOS 开发新手,尝试编码数据以在 db 中安全存储数据。 我在这里找到了当前示例:https://github.com/RNCryptor/RNCryptor-objc 这是我的代码。 NSStri
我正在使用 RNCryptor 加密 iOS 上的 NSData 并将其发送到 Python 服务器。到目前为止,一切都是正确的。当我尝试在服务器端解密 NSData 时,我的问题就来了。 RNCry
这是我使用 RNCryptor 加密/解密我发送到 Web 服务的 JSON 字符串的方法。我正在使用静态 IV 变量,这可能是不好的做法,但请不要关注它。这是我的做法: 注意:我正在使用 Matt
我在我的应用程序中使用 RNCryptor。我使用密码为纯文本生成了加密字符串,当我尝试使用密码解密加密字符串时,它没有给我原始纯文本,而是给了我一些随机字符串。 我想我在字符串编码/解码中遗漏了一些
我正在尝试使用 RNCryptor 的 AES256 实现来使图像加密/解密工作图书馆。 这是我目前的代码: //Encrypt file /** * var encryptedData = RNC
我从 iOS 应用程序中调用的 PHP API 要求以某种自定义方式对有效负载进行加密。我在使用 RNCryptor 在 Objective-C 中复制这种方法时遇到了麻烦。 以下是用于加密字符串的
如何使用 RNCryptor (AES128CBC) 解密 NSData?我已经尝试理解文档:https://github.com/RNCryptor/RNCryptor-Spec/blob/mast
我想使用 AES256 对称加密我的 iPhone 应用程序和我的 Java 服务器套接字。我目前正在使用 Rob Napier 的 RNCryptor/JNCryptor 库。 iPhone 上的加
我是一名优秀的程序员,十分优秀!