java - 来自目标 C 的 AES 加密字符串未被 Java 解密函数解密
问题描述
嗨,我已经在目标 C 中使用 BBAES 库加密了一个字符串。之后我通过 webservices 将它传递给服务器。但我不断从服务器收到此错误,error = "java.lang.IllegalStateException: Expected BEGIN_OBJECT but was STRING at line 1 column 1 path $"
我可以使用相同的库在本地解密该字符串,但 Java 解密会引发此错误。(当从 Android 发送加密字符串时,此代码正确解密)
JAVA设置及功能:
/** The Constant PASS_PHRASE. */
private static final String PASS_PHRASE = "PASS";
/** The Constant ITERATION_COUNT. */
private static final int ITERATION_COUNT = 1000;
/** The Constant KEY_SIZE. */
private static final int KEY_SIZE = 128;
/**
* Instantiates a new aes util.
*/
public AesUtil() {
this.keySize = KEY_SIZE;
this.iterationCount = ITERATION_COUNT;
try {
cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
} catch (NoSuchAlgorithmException | NoSuchPaddingException e) {
throw fail(e);
}
}
/**
* Decrypt.
*
* @param salt
* the salt
* @param iv
* the iv
* @param passphrase
* the passphrase
* @param ciphertext
* the ciphertext
* @return the string
*/
public String decrypt(String salt, String iv, String passphrase, String ciphertext) {
try {
SecretKey key = generateKey(salt, passphrase);
byte[] decrypted = doFinal(Cipher.DECRYPT_MODE, key, iv, base64(ciphertext));
return new String(decrypted, "UTF-8");
} catch (UnsupportedEncodingException e) {
throw fail(e);
}
}
/**
* Generate key.
*
* @param salt
* the salt
* @param passphrase
* the passphrase
* @return the secret key
*/
private SecretKey generateKey(String salt, String passphrase) {
try {
SecretKeyFactory factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1");
KeySpec spec = new PBEKeySpec(passphrase.toCharArray(), hex(salt), iterationCount, keySize);
SecretKey key = new SecretKeySpec(factory.generateSecret(spec).getEncoded(), "AES");
return key;
} catch (NoSuchAlgorithmException | InvalidKeySpecException e) {
throw fail(e);
}
}
/**
* Random.
*
* @param length
* the length
* @return the string
*/
public static String random(int length) {
byte[] salt = new byte[length];
new SecureRandom().nextBytes(salt);
return hex(salt);
}
IOS代码:
// BBAESSaltDefaultLength = 16 bytes
NSData* salt = [BBAES randomDataWithLength:BBAESSaltDefaultLength];
// BBAESPBKDF2DefaultIterationsCount = 1000 , keysize = 128
NSData *key = [BBAES keyBySaltingPassword:@"PASS" salt:salt keySize:BBAESKeySize128 numberOfIterations:BBAESPBKDF2DefaultIterationsCount];
NSString *secretMessage = [NSString stringWithFormat:@"{\"cli\":\"%@\",\"work\":\"%@\"}",[[NSUserDefaults standardUserDefaults] objectForKey:@"cli"],[[NSUserDefaults standardUserDefaults] objectForKey:@"work"]];
NSLog(@"Original message: %@", secretMessage);
NSData *BBIV = [BBAES randomIV];
NSData *encryptedBBData = [BBAES encryptedDataFromData:data IV:BBIV key:key options:BBAESEncryptionOptionsIncludeIV]; // This generates 64 byts
NSString *encryptedBBString = [secretMessage bb_AESEncryptedStringForIV:BBIV key:key options:BBAESEncryptionOptionsIncludeIV];
NSLog(@"Encrypted message: %@", encryptedBBString);
NSString *decryptedMessage = [encryptedBBString bb_AESDecryptedStringForIV:nil key:key];
NSLog(@"Decrypted message: %@", decryptedMessage);
NSString *saltBBString = salt.hexadecimalString;
NSString *IVBBString = BBIV.hexadecimalString;
[request setHTTPBody:[[NSString stringWithFormat:@"{\"id\":\"%@\",\"mx_id\":\"%@\",\"payload\":\"%@\"}",IVBBString,saltBBString,encryptedBBString] dataUsingEncoding:NSUTF8StringEncoding]];
可能有什么问题?你能帮帮我吗?
编辑 :
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60.0];
[request setHTTPMethod:@"POST"]; NSString *authString = [@"Bearer " stringByAppendingFormat:@"%@",[[NSUserDefaults standardUserDefaults] objectForKey:@"jwtAuthToken"] ];
[request setValue:authString forHTTPHeaderField:@"Authorization"];
[request setValue:@"mobile" forHTTPHeaderField:@"source"];
[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[request setValue:@"application/json" forHTTPHeaderField:@"Accept"];
NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
解决方案
推荐阅读
- angular - Angular 材质深色主题
- ionic-framework - 位置不可用电容器离子
- arrays - 运行包含大量原始数据的查询
- firebase - Firebase 云消息传递 click_action 不起作用
- python - 哪里 col 等于 value 复制到单独的 col - Pandas
- c# - 滑动以验证 puppteer-sharp 失败
- sap-cloud-sdk - 模拟 Https 目标以连接到 OAuth2 身份验证后面的系统
- java - 在 Spin Object Camunda 中添加新对象
- html - 我无法使用 angularjs 在文本框中粘贴中文内容
- powerbi - 折线图和堆积柱形图中的列值占列的百分比,而不是总计的百分比