开发者

base64 decryption in iphone

开发者 https://www.devze.com 2023-02-01 13:49 出处:网络
i am Getting the image from the .net web server which is in base64 encoded from. By googling i found this source for the encryption and decryption

i am Getting the image from the .net web server which is in base64 encoded from.

By googling i found this source for the encryption and decryption

i tried this

#import <Foundation/Foundation.h>

@class NSString;

@interface NSData (NSDataAdditions)

+ (NSData *) base64DataFromString:(NSString *)string;

-------------------------------------------

#import "NSDataAdditions.h"

@implementation NSData (NSDataAdditions)

+ (NSData *) base64DataFromString: (NSString *)string {
  unsigned long ixtext, lentext;
  unsigned char ch, input[4], output[3];
  short i, ixinput;
  Boolean flignore, flendtext = false;
  const char *temporary;
  NSMutableData *result;

  if (!string)
    return [NSData data];

  ixtext = 0;
  temporary = [string UTF8String];
  lentext = [string length];
  result = [NSMutableData dataWithCapacity: lentext];
  ixinput = 0;
  while (true) {
    if (ixtext >= lentext)
      break;
    ch = temporary[ixtext++];
    flignore = false;
    if ((ch >= 'A') && (ch <= 'Z'))
      ch = ch - 'A';
    else if ((ch >= 'a') && (ch <= 'z'))
      ch = ch - 'a' + 26;
    else if ((ch >= '0') && (ch <= '9'))
      ch = ch - '0' + 52;
    else if (ch == '+')
      ch = 62;
    else if (ch == '=')
      flendtext = true;
    else if (ch == '/')
      ch = 63;
    else
      flignore = true; 

    if (!flignore) {
      short ctcharsi开发者_StackOverflow中文版nput = 3;
      Boolean flbreak = false;

      if (flendtext) {
         if (ixinput == 0)
           break;                
         if ((ixinput == 1) || (ixinput == 2)) {
           ctcharsinput = 1;
         else
           ctcharsinput = 2;

         ixinput = 3;
         flbreak = true;
      }

      input[ixinput++] = ch;

      if (ixinput == 4)
        ixinput = 0;

      output[0] = (input[0] << 2) | ((input[1] & 0x30) >> 4);
      output[1] = ((input[1] & 0x0F) << 4) | ((input[2] & 0x3C) >> 2);
      output[2] = ((input[2] & 0x03) << 6) | (input[3] & 0x3F);

      for (i = 0; i < ctcharsinput; i++)
        [result appendBytes: &output[i] length: 1];
    }

    if (flbreak)
      break;
  }
  return result;
}

@end

but it shows error by resolving errors it is not decrypted.

And i try this link

But i did n't understand through that link.

can any one pls help me.

Post some code for base64 decryption in iphone.

Thank u in advance.


You should use a Base64 teststring like VGVzdHN0cmluZw== to test this method.

Use http://www.motobit.com/util/base64-decoder-encoder.asp to debug your data.

Edit
You should convert your string online to TEST it in your Objective C Code! You cleary didn't pass the method a real Base64 encoded string.

0

精彩评论

暂无评论...
验证码 换一张
取 消