How do I convert a byte array to UIImage - ios
I have a webservice which sends me back an array of bytes like this:
<ax23:IMGBLOB>-119,80,78,71,13,10,26,10,0,0,0,13,73,72,68,82,0,0,0,100,0,0,0,106,8,3,0,0,0,125,54,4,22,0,0,0,51,80,76,84,69,0,0,0,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-73,-107,-30,-126,0,0,0,16,116,82,78,83,0,16,32,48,64,80,96,112,-128,-112,-96,-80,-64,-48,-32,-16,84,-32,-88,-56,0,0,2,126,73,68,65,84,120,-38,-19,90,65,-110,-124,32,12,4,68,64,69,-32,-1,-81,-35,-53,-78,-96,40,4,-79,-70,118,-86,-90,-113,51,106,-101,116,-120,33,-127,-3,43,72,101,-84,13,127,-80,-42,40,-7,42,-63,100,108,-72,-124,53,-45,75,38,-84,62,84,-32,-41,97,-125,-72,118,-95,9,-89,-7,0,-123,-56,-115,-88,-101,35,-98,90,-79,-122,14,-84,-113,-84,49,62,116,-63,-101,126,-71,-9,112,-58,-66,25,45,127,-95,-51,118,113,-127,-20,52,-29,124,-1,34,121,-31,78,-71,-100,-119,76,-113,-32,-5,-111,65,-117,-37,43,-11,-23,82,65,118,-107,63,40,42,91,-21,-24,-96,12,-47,101,-22,64,33,8,-122,31,104,20,-123,99,9,9,-106,104,-67,-80,-7,123,-79,38,-78,-73,114,51,35,99,118,29,44,-21,-45,-27,-59,-13,59,-87,-66,-14,-118,117,66,-91,120,89,104,-102,-69,7,57,124,114,20,-11,101,-118,119,-2,40,-37,-91,69,35,111,99,-60,-25,28,99,44,-2,46,46,-9,-100,99,-108,101,-81,-28,-85,-56,49,-50,98,-86,-126,56,-50,6,-64,93,41,75,-23,44,63,13,86,29,-2,-34,97,38,69,-33,32,-44,-83,-61,-72,-89,-26,4,122,102,-14,-68,-8,-93,16,100,80,-106,-14,-123,69,52,113,102,47,96,-114,79,19,-105,-122,88,-10,10,-74,43,83,-72,-49,-87,-57,33,-82,84,-47,45,-43,-71,-115,5,92,-97,-10,-102,37,-72,104,72,-43,126,111,120,-81,41,46,91,-20,-51,-16,-115,-9,-88,94,83,100,-15,-53,116,79,18,97,37,113,-35,23,-17,-19,99,30,-72,5,-79,-24,45,-77,-108,63,-77,-22,54,9,-67,-24,-43,39,-17,-104,40,123,-101,-124,94,-58,-120,83,2,-77,77,111,-79,112,-126,-99,-88,-2,-78,-57,39,24,2,73,-62,-62,-119,53,-5,49,-128,101,23,73,-16,21,9,-53,-57,-86,72,73,35,73,112,-110,17,-18,81,-71,93,59,-103,36,-63,-118,-74,40,38,-41,125,35,-112,-108,48,-68,-107,-118,109,78,98,-70,72,8,-27,-84,73,36,-55,119,68,18,-54,86,-79,-44,-102,16,92,44,84,-79,93,74,35,7,73,74,-97,1,72,66,-104,-21,36,60,102,-78,33,18,-53,10,8,8,9,-64,93,31,41,-4,90,13,97,-56,98,-124,-92,21,72,-126,28,77,-11,27,37,-43,67,62,90,-112,-49,47,-94,-112,24,40,-119,4,-71,36,-126,20,119,-112,50,21,89,112,-89,-51,68,-101,-60,78,-99,91,7,-56,38,8,-79,-99,-125,108,76,-79,91,-20,-9,-101,5,54,122,6,-46,-10,64,52,112,32,-83,40,68,83,13,-47,30,-124,52,58,17,45,91,68,-13,25,-46,70,71,12,4,16,-93,13,-64,-112,102,37,-113,-101,-74,-66,113,-45,22,18,7,98,112,6,29,1,-30,-121,-103,-8,-79,108,57,96,118,-75,1,-77,43,6,-52,-128,81,57,116,-24,-113,63,-66,-128,63,-120,-127,63,82,66,63,28,99,56,-2,-104,15,-2,-64,-46,-8,-47,-85,47,-66,-8,-30,-77,-16,3,-74,-58,-81,-89,-116,-22,56,-74,0,0,0,0,73,69,78,68,-82,66,96,-126,####</ax23:IMGBLOB>
This array of bytes are used in Java for obtaining an image, I've been trying with several methods without success, I will post one of them below:
-(UIImageView *)convierteImagen: (NSMutableString *)cadenaImagen{
//NSString to NSData (temporary)
NSData *tempData = [cadenaImagen dataUsingEncoding:NSUTF8StringEncoding];
//NSLog(#"datos crudos: %#", tempData);
//2.NSData to Bytes
const void *bytes2 = [tempData bytes];
int8_t *bytes = (int8_t*)bytes2;
int len = tempData.length;
cadenaImagen = [[NSMutableString alloc]init];
[cadenaImagen appendString:#"["];
for (int i = 0; i < len; i++) {
if (i) [cadenaImagen appendString:#","];
[cadenaImagen appendFormat:#"%d", bytes[i]];
}
[cadenaImagen appendString:#"]"];
//3.Bytes to NSData
NSData *datos2 = [NSData dataWithBytes:bytes length:len];
return [[UIImageView alloc]initWithImage:[UIImage imageWithData:datos2]];
}
and the way I call this method is:
- (void)viewDidLoad
{
[super viewDidLoad];
NSMutableString *imagen = [[NSMutableString alloc] initWithString:#"-119,80,78,71,13,10,26,10,0,0,0,13,73,72,68,82,0,0,0,100,0,0,0,106,8,3,0,0,0,125,54,4,22,0,0,0,51,80,76,84,69,0,0,0,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-73,-107,-30,-126,0,0,0,16,116,82,78,83,0,16,32,48,64,80,96,112,-128,-112,-96,-80,-64,-48,-32,-16,84,-32,-88,-56,0,0,2,126,73,68,65,84,120,-38,-19,90,65,-110,-124,32,12,4,68,64,69,-32,-1,-81,-35,-53,-78,-96,40,4,-79,-70,118,-86,-90,-113,51,106,-101,116,-120,33,-127,-3,43,72,101,-84,13,127,-80,-42,40,-7,42,-63,100,108,-72,-124,53,-45,75,38,-84,62,84,-32,-41,97,-125,-72,118,-95,9,-89,-7,0,-123,-56,-115,-88,-101,35,-98,90,-79,-122,14,-84,-113,-84,49,62,116,-63,-101,126,-71,-9,112,-58,-66,25,45,127,-95,-51,118,113,-127,-20,52,-29,124,-1,34,121,-31,78,-71,-100,-119,76,-113,-32,-5,-111,65,-117,-37,43,-11,-23,82,65,118,-107,63,40,42,91,-21,-24,-96,12,-47,101,-22,64,33,8,-122,31,104,20,-123,99,9,9,-106,104,-67,-80,-7,123,-79,38,-78,-73,114,51,35,99,118,29,44,-21,-45,-27,-59,-13,59,-87,-66,-14,-118,117,66,-91,120,89,104,-102,-69,7,57,124,114,20,-11,101,-118,119,-2,40,-37,-91,69,35,111,99,-60,-25,28,99,44,-2,46,46,-9,-100,99,-108,101,-81,-28,-85,-56,49,-50,98,-86,-126,56,-50,6,-64,93,41,75,-23,44,63,13,86,29,-2,-34,97,38,69,-33,32,-44,-83,-61,-72,-89,-26,4,122,102,-14,-68,-8,-93,16,100,80,-106,-14,-123,69,52,113,102,47,96,-114,79,19,-105,-122,88,-10,10,-74,43,83,-72,-49,-87,-57,33,-82,84,-47,45,-43,-71,-115,5,92,-97,-10,-102,37,-72,104,72,-43,126,111,120,-81,41,46,91,-20,-51,-16,-115,-9,-88,94,83,100,-15,-53,116,79,18,97,37,113,-35,23,-17,-19,99,30,-72,5,-79,-24,45,-77,-108,63,-77,-22,54,9,-67,-24,-43,39,-17,-104,40,123,-101,-124,94,-58,-120,83,2,-77,77,111,-79,112,-126,-99,-88,-2,-78,-57,39,24,2,73,-62,-62,-119,53,-5,49,-128,101,23,73,-16,21,9,-53,-57,-86,72,73,35,73,112,-110,17,-18,81,-71,93,59,-103,36,-63,-118,-74,40,38,-41,125,35,-112,-108,48,-68,-107,-118,109,78,98,-70,72,8,-27,-84,73,36,-55,119,68,18,-54,86,-79,-44,-102,16,92,44,84,-79,93,74,35,7,73,74,-97,1,72,66,-104,-21,36,60,102,-78,33,18,-53,10,8,8,9,-64,93,31,41,-4,90,13,97,-56,98,-124,-92,21,72,-126,28,77,-11,27,37,-43,67,62,90,-112,-49,47,-94,-112,24,40,-119,4,-71,36,-126,20,119,-112,50,21,89,112,-89,-51,68,-101,-60,78,-99,91,7,-56,38,8,-79,-99,-125,108,76,-79,91,-20,-9,-101,5,54,122,6,-46,-10,64,52,112,32,-83,40,68,83,13,-47,30,-124,52,58,17,45,91,68,-13,25,-46,70,71,12,4,16,-93,13,-64,-112,102,37,-113,-101,-74,-66,113,-45,22,18,7,98,112,6,29,1,-30,-121,-103,-8,-79,108,57,96,118,-75,1,-77,43,6,-52,-128,81,57,116,-24,-113,63,-66,-128,63,-120,-127,63,82,66,63,28,99,56,-2,-104,15,-2,-64,-46,-8,-47,-85,47,-66,-8,-30,-77,-16,3,-74,-58,-81,-89,-116,-22,56,-74,0,0,0,0,73,69,78,68,-82,66,96,-126"];
[self.view addSubview:[self convierteImagen:imagen]];
}
Any help I will appreciate :)
try this:
-(UIImageView *)convierteImagen: (NSMutableString *)cadenaImagen{
NSArray *strings = [cadenaImagen componentsSeparatedByString:#","];
unsigned c = strings.count;
uint8_t *bytes = malloc(sizeof(*bytes) * c);
unsigned i;
for (i = 0; i < c; i++)
{
NSString *str = [strings objectAtIndex:i];
int byte = [str intValue];
bytes[i] = (uint8_t)byte;
}
NSData *datos = [NSData dataWithBytes:bytes length:c];
UIImage *image = [UIImage imageWithData:datos];
return [[UIImageView alloc]initWithImage:image];
}
on viewdidLoad is the same code you put:
- (void)viewDidLoad
{
[super viewDidLoad];
NSMutableString *imagen = [[NSMutableString alloc] initWithString:#"-119,80,78,71,13,10,26,10,0,0,0,13,73,72,68,82,0,0,0,100,0,0,0,106,8,3,0,0,0,125,54,4,22,0,0,0,51,80,76,84,69,0,0,0,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-73,-107,-30,-126,0,0,0,16,116,82,78,83,0,16,32,48,64,80,96,112,-128,-112,-96,-80,-64,-48,-32,-16,84,-32,-88,-56,0,0,2,126,73,68,65,84,120,-38,-19,90,65,-110,-124,32,12,4,68,64,69,-32,-1,-81,-35,-53,-78,-96,40,4,-79,-70,118,-86,-90,-113,51,106,-101,116,-120,33,-127,-3,43,72,101,-84,13,127,-80,-42,40,-7,42,-63,100,108,-72,-124,53,-45,75,38,-84,62,84,-32,-41,97,-125,-72,118,-95,9,-89,-7,0,-123,-56,-115,-88,-101,35,-98,90,-79,-122,14,-84,-113,-84,49,62,116,-63,-101,126,-71,-9,112,-58,-66,25,45,127,-95,-51,118,113,-127,-20,52,-29,124,-1,34,121,-31,78,-71,-100,-119,76,-113,-32,-5,-111,65,-117,-37,43,-11,-23,82,65,118,-107,63,40,42,91,-21,-24,-96,12,-47,101,-22,64,33,8,-122,31,104,20,-123,99,9,9,-106,104,-67,-80,-7,123,-79,38,-78,-73,114,51,35,99,118,29,44,-21,-45,-27,-59,-13,59,-87,-66,-14,-118,117,66,-91,120,89,104,-102,-69,7,57,124,114,20,-11,101,-118,119,-2,40,-37,-91,69,35,111,99,-60,-25,28,99,44,-2,46,46,-9,-100,99,-108,101,-81,-28,-85,-56,49,-50,98,-86,-126,56,-50,6,-64,93,41,75,-23,44,63,13,86,29,-2,-34,97,38,69,-33,32,-44,-83,-61,-72,-89,-26,4,122,102,-14,-68,-8,-93,16,100,80,-106,-14,-123,69,52,113,102,47,96,-114,79,19,-105,-122,88,-10,10,-74,43,83,-72,-49,-87,-57,33,-82,84,-47,45,-43,-71,-115,5,92,-97,-10,-102,37,-72,104,72,-43,126,111,120,-81,41,46,91,-20,-51,-16,-115,-9,-88,94,83,100,-15,-53,116,79,18,97,37,113,-35,23,-17,-19,99,30,-72,5,-79,-24,45,-77,-108,63,-77,-22,54,9,-67,-24,-43,39,-17,-104,40,123,-101,-124,94,-58,-120,83,2,-77,77,111,-79,112,-126,-99,-88,-2,-78,-57,39,24,2,73,-62,-62,-119,53,-5,49,-128,101,23,73,-16,21,9,-53,-57,-86,72,73,35,73,112,-110,17,-18,81,-71,93,59,-103,36,-63,-118,-74,40,38,-41,125,35,-112,-108,48,-68,-107,-118,109,78,98,-70,72,8,-27,-84,73,36,-55,119,68,18,-54,86,-79,-44,-102,16,92,44,84,-79,93,74,35,7,73,74,-97,1,72,66,-104,-21,36,60,102,-78,33,18,-53,10,8,8,9,-64,93,31,41,-4,90,13,97,-56,98,-124,-92,21,72,-126,28,77,-11,27,37,-43,67,62,90,-112,-49,47,-94,-112,24,40,-119,4,-71,36,-126,20,119,-112,50,21,89,112,-89,-51,68,-101,-60,78,-99,91,7,-56,38,8,-79,-99,-125,108,76,-79,91,-20,-9,-101,5,54,122,6,-46,-10,64,52,112,32,-83,40,68,83,13,-47,30,-124,52,58,17,45,91,68,-13,25,-46,70,71,12,4,16,-93,13,-64,-112,102,37,-113,-101,-74,-66,113,-45,22,18,7,98,112,6,29,1,-30,-121,-103,-8,-79,108,57,96,118,-75,1,-77,43,6,-52,-128,81,57,116,-24,-113,63,-66,-128,63,-120,-127,63,82,66,63,28,99,56,-2,-104,15,-2,-64,-46,-8,-47,-85,47,-66,-8,-30,-77,-16,3,-74,-58,-81,-89,-116,-22,56,-74,0,0,0,0,73,69,78,68,-82,66,96,-126"];
[self.view addSubview:[self convierteImagen: imagen]];
}
this is what I got with your byte array:
so I think this should solve your problem ;)
Related
HashValue mis match in PayUMoney framework, iOS - Objective-C
I got error like Hash value Mismatch from PayUMoney payment gateway Here is my code which I did, please let me know where is issue PUMTxnParam *txnParam= [[PUMTxnParam alloc] init]; //Set the parameters txnParam.phone = [[NSUserDefaults standardUserDefaults] valueForKey:#"mobile"]; txnParam.email = [[NSUserDefaults standardUserDefaults] valueForKey:#"username"]; txnParam.amount = _txtCash.text; txnParam.environment = PUMEnvironmentProduction; txnParam.firstname = [[NSUserDefaults standardUserDefaults] valueForKey:#"name1"]; txnParam.key = #"xxxxxxxx"; txnParam.merchantid = #"xxxxxxx"; txnParam.txnID = [txnData valueForKey:#"txnid"]; txnParam.surl = #"https://google.com/"; txnParam.furl = #"https://google.com/"; txnParam.productInfo = #"My App"; txnParam.udf1 = #""; txnParam.udf2 = #""; txnParam.udf3 = #""; txnParam.udf4 = #""; txnParam.udf5 = #""; txnParam.hashValue = [self createSHA512:[NSString stringWithFormat:#"%#|%#|%#|%#|%#|%#|%#|%#|%#|%#|%#||||||%#|",txnParam.key,txnParam.txnID,txnParam.amount,txnParam.productInfo,txnParam.firstname,txnParam.email,txnParam.udf1,txnParam.udf2,txnParam.udf3,txnParam.udf4,txnParam.udf5,#"dxltF25QEp"]]; -(NSString *)createSHA512:(NSString *)string { const char *cstr = [string cStringUsingEncoding:NSUTF8StringEncoding]; NSData *data = [NSData dataWithBytes:cstr length:string.length]; uint8_t digest[CC_SHA512_DIGEST_LENGTH]; CC_SHA512(data.bytes, (CC_LONG)data.length, digest); NSMutableString* output = [NSMutableString stringWithCapacity:CC_SHA512_DIGEST_LENGTH * 2]; for(int i = 0; i < CC_SHA512_DIGEST_LENGTH; i++) [output appendFormat:#"%02x", digest[i]]; return output; } Please help to clear this out.
Cocoa pods and Github have their old libraries, I just copied the library from their example which is in GitHub and it works fine :)
NSString from NSData is incomplete
When I use [[NSString alloc] initWithData:subdata encoding:encoding] method , I find the string is incomplete. Why? Any restrictions in NSString? Then,I suspect that the data is too large,so I create a new method to Transfer. -(NSString *)stringFromData:(NSData *)data encoding:(NSStringEncoding)encoding{ NSInteger DataLen = data.length; NSInteger segLen = 5000; NSInteger times = DataLen/segLen; NSInteger fristLen = DataLen%segLen; NSMutableString *str = [NSMutableString string]; [str appendString:[[NSString alloc] initWithData:[data subdataWithRange:NSMakeRange(0, fristLen)] encoding:encoding]]; NSLog(#"%#",str); while (times--) { NSData *subdata = [data subdataWithRange:NSMakeRange(fristLen, segLen)] ; if (subdata) { NSString*substr = [[NSString alloc] initWithData:subdata encoding:encoding]; //tag1 NSLog(#"%#",substr); if (substr) { [str appendString:substr]; } fristLen += segLen; }else{ break; } } return str; } but in tag1, I find the string is null in some conditions. What is the problem?
How can we encrypt bytes through DES/CBC/NOPADDING in Objective C?
I am trying to encrypt bytes data through DES/CBS/NOPADDING algorithm but unable to get correct output. What I have tried is given below in code. Please have a look and suggest. #import "STEncryptViewController.h" #import <CommonCrypto/CommonCryptor.h> #import "STEncryptorDES.h" #import "NSData+Base64.h" #import <CommonCrypto/CommonDigest.h> #define KEY #"MY_TEST_KEY" #interface STEncryptViewController () #end #implementation STEncryptViewController #synthesize message; #synthesize encrypted; #synthesize decrypted; #synthesize key; #synthesize outputMessage; - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil { self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; if (self) { // Custom initialization } return self; } - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view from its nib. } - (IBAction)encrypt:(id)sender { const unsigned char bytes[] = {65,17,17,17,17,17,17,17}; NSData *data = [NSData dataWithBytes:bytes length:sizeof(bytes)]; NSString *str = self.key.text; NSData* data12 = [str dataUsingEncoding:NSUTF8StringEncoding]; NSUInteger len = str.length; uint8_t *bytes1 = (uint8_t *)[data12 bytes]; NSMutableString *result = [NSMutableString stringWithCapacity:len * 3]; // [result appendString:#"["]; //Key convertion int i = 0; while(i < len){ if (i) { [result appendString:#","]; } [result appendFormat:#"%d", bytes1[i]]; i++; } // [result appendString:#"]"]; NSLog (#"String is %#",str); NSLog (#"Byte array is %#",result); // Byte iv1 [] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}; Byte iv [] = {1, 35, 69, 103, -119, -85, -51, -17}; NSData *ivData = [NSData dataWithBytes:iv length:sizeof(iv)]; NSData *encryptedData = [STEncryptorDES encryptData1:data key:[NSData dataWithBytes:iv1 length:sizeof(iv1)] iv:ivData]; NSLog(#"encrypted : %#", [encryptedData base64EncodedString]); self.outputMessage.text = [NSString stringWithFormat:#"Encrypted String ::>> %#",[encryptedData base64EncodedString]]; [self doCipher:nil enc:kCCEncrypt]; } - (NSData *)md5DataFromString:(NSString *)input { const char *cStr = [input UTF8String]; unsigned char digest[16]; CC_MD5( cStr, strlen(cStr), digest ); // This is the md5 call return [NSData dataWithBytes:digest length:CC_MD5_DIGEST_LENGTH]; } - (IBAction)decrypt:(id)sender { NSData *valueData = [self hexToBytes:self.decrypted.text]; NSData *keyData = [self md5DataFromString:self.key.text]; Byte iv [] = {1, 35, 69, 103, -119, -85, -51, -17}; NSData *ivData = [NSData dataWithBytes:iv length:sizeof(iv)]; NSData *decryptedData = [STEncryptorDES decryptData:valueData key:keyData iv:ivData]; NSLog(#"decrypted : %#", [[NSString alloc] initWithData:decryptedData encoding:NSASCIIStringEncoding]); self.outputMessage.text = [NSString stringWithFormat:#"Decrypted String ::>> %#", [[NSString alloc] initWithData:decryptedData encoding:NSASCIIStringEncoding]]; } - (NSString*)doCipher:(NSString*)plainText enc:(CCOperation)encryptOrDecrypt{ NSData *key123 = [self hexToBytes:#"MY_TEST_KEY"]; const void *vplainText; size_t plainTextBufferSize; if (encryptOrDecrypt == kCCDecrypt) { NSData *EncryptData = [self hexToBytes:#"5454545454545454"]; // NSData *EncryptData =[NSData dataWithBase64EncodedString:plainText]; plainTextBufferSize = [EncryptData length]; vplainText = [EncryptData bytes]; } else { plainTextBufferSize = [plainText length]; vplainText = (const void *) [plainText UTF8String]; } CCCryptorStatus ccStatus; uint8_t *bufferPtr = NULL; size_t bufferPtrSize = 0; size_t movedBytes = 0; // uint8_t ivkCCBlockSize3DES; bufferPtrSize = (plainTextBufferSize + kCCBlockSize3DES) & ~(kCCBlockSize3DES - 1); bufferPtr = malloc( bufferPtrSize * sizeof(uint8_t)); memset((void *)bufferPtr, 0x0, bufferPtrSize); unsigned char cKey[FBENCRYPT_KEY_SIZE]; bzero(cKey, sizeof(cKey)); [key123 getBytes:cKey length:FBENCRYPT_KEY_SIZE]; //unsigned char result1[24]= {0,0,0,0,0,0,0,0,0,0,0,00,0,0,0,0,0,0,0,00,00,00,00,0}; unsigned char IV3[8]={1, 35, 69, 103, -119, -85, -51, -17}; uint8_t iv[kCCBlockSize3DES]; memset((void *) iv, 0x0, (size_t) sizeof(iv)); ccStatus = CCCrypt(encryptOrDecrypt, kCCAlgorithm3DES, 0x0000 , cKey, //"123456789012345678901234", //key kCCKeySize3DES, IV3 , //iv, vplainText, //plainText, plainTextBufferSize, (void *)bufferPtr, bufferPtrSize, &movedBytes); //if (ccStatus == kCCSuccess) NSLog(#"SUCCESS"); /*else*/ if (ccStatus == kCCParamError) return #"PARAM ERROR"; else if (ccStatus == kCCBufferTooSmall) return #"BUFFER TOO SMALL"; else if (ccStatus == kCCMemoryFailure) return #"MEMORY FAILURE"; else if (ccStatus == kCCAlignmentError) return #"ALIGNMENT"; else if (ccStatus == kCCDecodeError) return #"DECODE ERROR"; else if (ccStatus == kCCUnimplemented) return #"UNIMPLEMENTED"; NSString *result; if (encryptOrDecrypt == kCCDecrypt) { result = [ [NSString alloc] initWithData: [NSData dataWithBytes:(const void *)bufferPtr length:(NSUInteger)movedBytes] encoding:NSASCIIStringEncoding]; } else { NSData *myData = [NSData dataWithBytes:(const void *)bufferPtr length:(NSUInteger)movedBytes]; NSLog(#"data is: %#", myData); // result = [NSData base64StringFromData:myData length:myData.length]; // result = [[NSString alloc]initWithData:myData encoding:NSUTF8StringEncoding]; result = nil; } return result; } -(NSData*)hexToBytes:(NSString *)hex { NSString *safeStr = [hex stringByReplacingOccurrencesOfString:#"0" withString:#"A"]; NSMutableData* data = [NSMutableData data]; int idx; for (idx = 0; idx+2 <= safeStr.length; idx+=2) { NSRange range = NSMakeRange(idx, 2); NSString* hexStr = [safeStr substringWithRange:range]; NSScanner* scanner = [NSScanner scannerWithString:hexStr]; unsigned int intValue; [scanner scanHexInt:&intValue]; [data appendBytes:&intValue length:1]; } NSLog(#"%lu",(unsigned long)data.hash); return data; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } #end This was my all effort. Currently it is working but not expected output getting. Please suggest me what am I missing here?
One glaring error is that STEncryptorDES is used and is specifies padding but the question states that padding should be used. Observations: - You call doCipher with nil and do not use it's return value, delete the unneeded/unused code from the question. - There is a lot of code to perform a rather simple operation. - It seems that the test data variable is named iv1, use a better name if that is correct. - DES should not be used for new work, only when necessary to interoperate with existing work, it is not very secure. New work should use AES. - #synthesize has not been needed for several years.
In cryptography "DESede/CBC/NoPadding" use following code might be help for you. Byte iv [] = {1, 35, 69, 103, -119, -85, -51, -17}; NSData *ivData = [NSData dataWithBytes:iv length:sizeof(iv)]; NSData key = ["MY_TEST_KEY" dataUsingEncoding:NSUTF8StringEncoding] char cKey[key.length]; bzero(cKey, sizeof(cKey)); [key getBytes:cKey length:key.length]; // setup iv char cIv[kCCBlockSizeDES]; bzero(cIv, kCCBlockSizeDES); if (ivData) { [ivData getBytes:cIv length:kCCBlockSizeDES]; } // setup output buffer size_t bufferSize = [data length] + kCCBlockSizeDES; void *buffer = malloc(bufferSize); // do encrypt size_t encryptedSize = 0; CCCryptorStatus cryptStatus = CCCrypt(kCCEncrypt, kCCAlgorithm3DES, ccNoPadding, cKey, key.length, cIv, [data bytes], [data length], buffer, bufferSize, &encryptedSize); if (cryptStatus == kCCSuccess) { resultData = [NSData dataWithBytes:buffer length:encryptedSize]; free(buffer); } else { free(buffer); NSLog(#"[ERROR] failed to encrypt|CCCryptoStatus: %d", cryptStatus); return nil; }
iOS Adding Dynamic Values to Array
I want to add dynamic values of UIImage in array which initialised in the for loop, So I created a NSMutableArray to set object first in for loop and later tried to retrieve values from that. Please can you look where I am going wrong and what should be best approch while adding the dynamic values. NSMutableDictionary * dic= [[NSMutableDictionary alloc]init]; NSMutableArray *cImages = [[NSMutableArray alloc]init]; for(int i=0; i<5; i++) { NSString * imageKey= [NSString stringWithFormat:#"image%i",i+1]; NSData *imageData = [NSData dataWithContentsOfURL:[NSURL URLWithString: [[results objectAtIndex:i]valueForKey:imageKey]]]; UIImage *imageOne = [UIImage imageWithData:imageData]; [dic setObject:imageOne forKey:imageKey]; } for (NSString *key in dic) { NSString *sprite = [dic objectForKey:key]; //adding below to the array [cImages addObject:sprite]; }
What's wrong with just adding UIImage's to the mutable array? NSMutableArray *cImages = [[NSMutableArray alloc]init]; for(int i = 0; i < 5; i++) { NSString *imageKey= [NSString stringWithFormat:#"image%i", i+1]; UIImage *imageToAdd = [UIImage imageNamed:imageKey]; [cImages addObject:imageToAdd]; } // Using the images for (UIImage *image in cImages) { // Do whatever u want with each image. } If you really need to use a dictionary to access the UIImage's by the names "image1, image2" etc you can do this: NSDictionary *dict = [[NSDictionary alloc] init]; for (int i = 0; i < 5; i++) { NSString *imageKey = [NSString stringWithFormat:#"image%i", i+1]; UIImage *image = [UIImage imageNamed:#"nameOfImageFile.png"]; [dict setObject:image forKey:imageKey]; } // Now you can access the UIImage values by their names (image1,image2,image3,image4,image5) UIImage *imageToUse = [dict valueForKey:#"image1"]; (haven't test these, but it's fairly standard).
How do I send an array of integers via GKSession bluetooth
I have a problem with sending an array of integers via GKSession. This is how I did it: To send it. -(void)sendData { NSMutableArray *myArray = [[NSMutableArray alloc] init]; [myArray addObject:[NSNumber numberWithInteger:snakeHead.position.x]]; [myArray addObject:[NSNumber numberWithInteger:snakeHead.position.y]]; [myArray addObject:[NSNumber numberWithInteger:direction.x]]; [myArray addObject:[NSNumber numberWithInteger:direction.y]]; [myArray addObject:[NSNumber numberWithInteger:bodyOffsetX]]; [myArray addObject:[NSNumber numberWithInteger:bodyOffsetY]]; [myArray addObject:[NSNumber numberWithInteger:amountBodies]]; NSData* encodedArray = [NSKeyedArchiver archivedDataWithRootObject:myArray]; [snakeSession sendData:encodedArray toPeers:snakePeers withDataMode:GKSendDataReliable error:nil]; [encodedArray release]; } The sendData function is called from a scheduler. To receive it. -(void)receiveData:(NSData *)data fromPeer:(NSString *)peer inSession:(GKSession *)session context:(void *)context { NSArray *hisArray = [NSKeyedUnarchiver unarchiveObjectWithData:data]; snakeHead2.position = ccp([[hisArray objectAtIndex:0] integerValue], [[hisArray objectAtIndex:1] integerValue]); direction2 = ccp([[hisArray objectAtIndex:2] integerValue], [[hisArray objectAtIndex:3] integerValue]); bodyOffsetX2 = [[hisArray objectAtIndex:4] integerValue]; bodyOffsetY2 = [[hisArray objectAtIndex:5] integerValue]; amountBodies2 = [[hisArray objectAtIndex:6] integerValue]; } This does not work and the game crashes. So what am I doing wrong here?
heres what I would do. it even sends less bytes! (faster) // to send int snakeHead_position_x = 0; int snakeHead_position_y = 0; int direction_x = 0; int direction_y = 0; int bodyOffsetX = 0; int bodyOffsetY = 0; int amountBodies = 0; NSString *package = [NSString stringWithFormat:#"%i|%i|%i|%i|%i|%i|%i", snakeHead_position_x, snakeHead_position_y, direction_x, direction_y, bodyOffsetX, bodyOffsetY, amountBodies]; NSData *data = [package dataUsingEncoding:NSASCIIStringEncoding]; assert(data); // this is critical don't remove! //printf("sending %u bytes...\n", data.length); [snakeSession sendData:data toPeers:snakePeers withDataMode:GKSendDataReliable error:nil]; // to retrieve NSArray *intArray = [[[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding] componentsSeparatedByString:#"|"]; int snakeHead_position_x = [intArray[0] intValue]; int snakeHead_position_y = [intArray[1] intValue]; int direction_x = [intArray[2] intValue]; int direction_y = [intArray[3] intValue]; int bodyOffsetX = [intArray[4] intValue]; int bodyOffsetY = [intArray[5] intValue]; int amountBodies = [intArray[6] intValue];