簡體   English   中英

iOS加密AES128 / CBC / nopadding為什么不起作用?

[英]iOS encryption AES128/CBC/nopadding why is not working?

我有一個應用程序,需要使用AES / CBC /無填充編碼一些數據。 該應用程序也移植到Android上。 那里的編碼是這樣完成的:

byte[] encodedKey = getKey();
    SecretKeySpec skeySpec = new SecretKeySpec(encodedKey, "AES");
    AlgorithmParameterSpec paramSpec = new IvParameterSpec(initializationVector);

    Cipher cipher = Cipher.getInstance("AES/CBC/NoPadding");
    cipher.init(Cipher.ENCRYPT_MODE, skeySpec, paramSpec);

    int blockSize = cipher.getBlockSize();
    int diffSize = decrypted.length % blockSize;
    System.out.println("Cipher size: " + blockSize);
    System.out.println("Current size: " + decrypted.length);
    if (diffSize != 0) {
      diffSize = blockSize - diffSize;
      byte[] oldDecrypted = decrypted;
      decrypted = new byte[decrypted.length + diffSize];
      System.arraycopy(oldDecrypted, 0, decrypted, 0, oldDecrypted.length);
      for (int i = 0; i < diffSize; i++) {
        decrypted[i + oldDecrypted.length] = " ".getBytes()[0];
      }
      System.out.println("New size: " + decrypted.length);

    }
    return cipher.doFinal(decrypted);

initializationVector看起來像這樣:

private byte[] initializationVector = new byte[] { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };

在iOS上我有這樣的加密:

- (NSData *)AES128EncryptWithKey:(NSString *)key 
{
    // 'key' should be 16 bytes for AES128, will be null-padded otherwise
    char keyPtr[kCCKeySizeAES128+1]; // room for terminator (unused)
    bzero(keyPtr, sizeof(keyPtr)); // fill with zeroes (for padding)


    // fetch key data
    [key getCString:keyPtr maxLength:sizeof(keyPtr) encoding:NSUTF8StringEncoding];

    NSUInteger dataLength = [self length];

    //See the doc: For block ciphers, the output size will always be less than or 
    //equal to the input size plus the size of one block.
    //That's why we need to add the size of one block here
    size_t bufferSize = dataLength + kCCBlockSizeAES128;
    void *buffer = malloc(bufferSize);

    size_t numBytesEncrypted = 0;
    CCCryptorStatus cryptStatus = CCCrypt(kCCEncrypt, 
                                          kCCAlgorithmAES128, 
                                          0x0000,
                                          keyPtr, 
                                          kCCKeySizeAES128,
                                          NULL /* initialization vector (optional) */,
                                          [self bytes], dataLength, /* input */
                                          buffer, bufferSize, /* output */
                                          &numBytesEncrypted);
    if (cryptStatus == kCCSuccess) {
        //the returned NSData takes ownership of the buffer and will free it on deallocation
        return [NSData dataWithBytesNoCopy:buffer length:numBytesEncrypted];
    }

    free(buffer); //free the buffer;
    return nil;
}

上述方法是NSData類別的一部分。 該方法調用如下:

NSData *data = [@"4915200456727" dataUsingEncoding:NSUTF8StringEncoding];
    NSData *cipher  = [data AES128EncryptWithKey:@"@x#zddXekZerBBw6"];
    NSString *ecriptedString = [NSString stringWithFormat:@"%.*s", [cipher length], [cipher bytes]];

我遇到的問題是我在iOS和Android上沒有收到相同的加密數據。 在iOS上,加密數據的長度為0個字節。

你能否給出關於如何使用帶有CBC的AES128加密字符串的任何指針,並且沒有填充,也許是一個例子?

謝謝

我找到了解決問題的方法。 為了使加密工作沒有填充,我必須添加0x0000而不是kCCOptionPKCS7Padding或kCCOptionECBMode被處理。

此外,如果需要編碼的數據不具有kCCKeySizeAES128(16)的長度倍數,則必須將保存數據的向量調整為具有kCCKeySizeAES128的長度倍數並且空值填充某些內容。 我添加了空格。

    - (NSData *)AES128EncryptWithKey:(NSString *)key
{
    char keyPtr[kCCKeySizeAES128+1];
    bzero(keyPtr, sizeof(keyPtr));

    [key getCString:keyPtr maxLength:sizeof(keyPtr) encoding:NSUTF8StringEncoding];

    int dataLength = [self length];
    int diff = kCCKeySizeAES128 - (dataLength % kCCKeySizeAES128);
    int newSize = 0;

    if(diff > 0)
    {
        newSize = dataLength + diff;
    }

    char dataPtr[newSize];
    memcpy(dataPtr, [self bytes], [self length]);
    for(int i = 0; i < diff; i++)
    {
        dataPtr[i + dataLength] = 0x20;
    }

    size_t bufferSize = newSize + kCCBlockSizeAES128;
    void *buffer = malloc(bufferSize);

    size_t numBytesEncrypted = 0;
    CCCryptorStatus cryptStatus = CCCrypt(kCCEncrypt,
                                          kCCAlgorithmAES128,
                                          0x0000, //No padding
                                          keyPtr,
                                          kCCKeySizeAES128,
                                          NULL,
                                          dataPtr,
                                          sizeof(dataPtr),
                                          buffer,
                                          bufferSize,
                                          &numBytesEncrypted);

    if(cryptStatus == kCCSuccess)
    {
        return [NSData dataWithBytesNoCopy:buffer length:numBytesEncrypted];
    }

    return nil;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM