简体   繁体   中英

Encryption of the audio file in iPhone

I want to encrypt the audio file; how do I achieve it in the iPhone? Is there any framework to get this done?

This is the code I am using to encrypt the file,

NSData *inputData = [NSData dataWithContentsOfFile:localfilePath.path]; 

NSString *encryptKey=[NSString stringWithString:@"nywleS"];





  CCCryptorStatus status = kCCSuccess;
                  NSData *encrypted = [inputData dataEncryptedUsingAlgorithm:kCCAlgorithmAES128 key:encryptKey initializationVector:@"Selwyn" options:0 error:&status];


NSData *decryptedData = [outputData decryptedAES256DataUsingKey:encryptKey error:nil]; 

But the original file size becomes less after encryption in CBS mode. And also after decryption, it's the same size as encrypted.

iOS supports CommonCrypto (just like Mac OSX does). That will let you encrypt and decrypt any type of data, including audio.

There are several questions on how to use CommonCrypto in your applications.

poupou's answer should ultimately get credit, as he is answering the original question. I'm posting this as an answer since reference code was asked of me in comments above:

From Obfuscate / Encrypt a String , modified for NSData:

- (NSData *)obfuscate:(NSData *)data withKey:(NSString *)key
{
  NSData *result = [data mutableCopy];

  // Get pointer to data to obfuscate
  char *dataPtr = (char *) [result mutableBytes];

  // Get pointer to key data
  char *keyData = (char *) [[key dataUsingEncoding:NSUTF8StringEncoding] bytes];

  // Points to each char in sequence in the key
  char *keyPtr = keyData;
  int keyIndex = 0;

  // For each character in data, xor with current value in key
  for (int x = 0; x < [data length]; x++) 
  {
    // Replace current character in data with 
    // current character xor'd with current key value.
    // Bump each pointer to the next character
    *dataPtr = *dataPtr++ ^ *keyPtr++; 

    // If at end of key data, reset count and 
    // set key pointer back to start of key value
    if (++keyIndex == [key length])
      keyIndex = 0, keyPtr = keyData;
  }

  return [result autorelease];
}

Again, this is intended to prevent casual copying of the audio file (as mentioned in my comments), not as a secure end-to-end encryption technique.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM