Java解密功能未解密来自目标C的AES加密字符串

时间:2018-06-20 17:02:50

标签: java objective-c encryption aes pbkdf2

嗨,我已经使用目标C中的BBAES库对字符串进行了加密。之后,我通过webservices将其传递给服务器。但是我继续从服务器获取此错误,错误=“ java.lang.IllegalStateException:预期为BEGIN_OBJECT,但在第1行第1列的路径$处是STRING”

我能够使用相同的库在本地解密该字符串,但是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];

0 个答案:

没有答案
相关问题