Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8055937
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T08:33:20+00:00 2026-06-05T08:33:20+00:00

I have an app that needs to encode some data using AES/CBC/no padding. The

  • 0

I have an app that needs to encode some data using AES/CBC/no padding. The app is ported also on android. There the encoding is done like this:

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);

the initializationVector looks like this:

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

on iOS i have something like this for encryption:

- (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;
}

the method described above is part of a category over NSData.
the method is called like this:

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

the problem that i have is that i don’t receive the same encrypted data on iOS and android. On iOS the encrypted data has 0 bytes in length.

Could you give any pointers on how to encrypt a string using AES128 with CBC and no padding and perhaps an example?

Thank you

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T08:33:22+00:00Added an answer on June 5, 2026 at 8:33 am

    I found the solution to my problem. In order to make the encryption work without padding i had to add 0x0000 instead of kCCOptionPKCS7Padding or kCCOptionECBMode which are treated.

    Also if the data that needs to be encoded doesn’t have a length multiple of kCCKeySizeAES128 ( 16 ) then the vector that holds the data must be resized to have the length multiple with kCCKeySizeAES128 and the empty values filled with something. I added spaces.

        - (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;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a MySQL/Rails app that needs search. Here's some info about the data:
I have an app that needs to process some packets from a UDP broadcast,
I have a wpf app that needs to communicate(exchange data) with a custom designed
I have an app that has a bunch of Core Data that it needs
My android app needs to fetch some info from a php server that hosts
We have a .NET application that needs to pass some data over to a
I have an app that needs to update a large amount of data over
I have an app that needs to read a PDF file from the file
We have an app that needs to access network resources. It's written in VB.Net.
I have an app that needs to be able use either an sqlite3 datebase

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.