Write NSString to file

by srudenko 26. March 2010 12:04

 

//Method writes a string to a text file

-(void) writeToTextFile{

//get the documents directory:

NSArray *paths = NSSearchPathForDirectoriesInDomains

(NSDocumentDirectory, NSUserDomainMask, YES);

NSString *documentsDirectory = [paths objectAtIndex:0];

//make a file name to write the data to using the documents directory:

NSString *fileName = [NSString stringWithFormat:@"%@/textfile.txt"

  documentsDirectory];

//create content - four lines of text

NSString *content = @"One\nTwo\nThree\nFour\nFive";

//save content to the documents directory

[content writeToFile:fileName 

  atomically:NO 

encoding:NSStringEncodingConversionAllowLossy 

  error:nil];

}

 

Currently rated 3.5 by 2 people

  • Currently 3.5/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags:

Send NSString to windows Web Service through SOAP

by srudenko 26. March 2010 10:11

- (void) Send :(NSString*) tarXML

{

    recordResults = FALSE;

    

    NSString *soapMessage = [NSString stringWithFormat:

                             @"<?xml version=\"1.0\" encoding=\"utf-8\"?>\n"

                             "<soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">\n"

                             "<soap:Body>\n"

                             "<TestDecrypt xmlns=\"http://tempuri.org/\">\n"

                             "<tarData>%@</tarData>\n"

                             "</TestDecrypt>\n"

                             "</soap:Body>\n"

                             "</soap:Envelope>\n", tarXML

                             ];

    NSLog(soapMessage);

    

    NSURL *url = [NSURL URLWithString:@"http://sergeynotebook/WebService1/Service1.asmx"];

    NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:url];

    NSString *msgLength = [NSString stringWithFormat:@"%d", [soapMessage length]];

    

    [theRequest addValue: @"text/xml; charset=utf-8" forHTTPHeaderField:@"Content-Type"];

    [theRequest addValue: @"http://tempuri.org/TestDecrypt" forHTTPHeaderField:@"SOAPAction"];

    [theRequest addValue: msgLength forHTTPHeaderField:@"Content-Length"];

    [theRequest setHTTPMethod:@"POST"];

    [theRequest setHTTPBody: [soapMessage dataUsingEncoding:NSUTF8StringEncoding]];

    

    NSURLConnection *theConnection = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self];

    

    if( theConnection )

    {

        webData = [[NSMutableData data] retain];

    }

    else

    {

        NSLog(@"theConnection is NULL");

    }

    

}

 

-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response

{

    [webData setLength: 0];

}

-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data

{

    [webData appendData:data];

}

-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error

{

    NSLog(@"ERROR with theConenction");

    [connection release];

    [webData release];

}

-(void)connectionDidFinishLoading:(NSURLConnection *)connection

{

    NSLog(@"DONE. Received Bytes: %d", [webData length]);

    NSString *theXML = [[NSString alloc] initWithBytes: [webData mutableBytes] length:[webData length] encoding:NSUTF8StringEncoding];

    NSLog(theXML);

    [theXML release];

    

    

    [connection release];

    [webData release];

}

Currently rated 5.0 by 2 people

  • Currently 5/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags:

General

Encrypt string with certificate from p12 file

by srudenko 26. March 2010 10:04

#import "NSDataAdditions.h"

 

@implementation Cryptography

 

const size_t BUFFER_SIZE = 240;

const size_t CIPHER_BUFFER_SIZE = 1024;

const uint32_t PADDING = kSecPaddingPKCS1;

 

- (NSString*) test1

{

    uint8_t *plainBuffer;

    uint8_t *decryptedBuffer;

    const char inputString[] = "some data";

    int len = strlen(inputString);

    

    plainBuffer = (uint8_t *)calloc(len, sizeof(uint8_t));

    

    strncpy( (char *)plainBuffer, inputString, len);

    

    

    NSString * path = [[NSBundle mainBundle] pathForResource:@"test1" ofType:@"p12"];

    assert(path != nil);

    

    NSData * data = [NSData dataWithContentsOfFile:path];

    assert(data != nil);

    

    

    CFArrayRef tmpCFArrayRef = CFArrayCreate(kCFAllocatorDefault, NULL, 0, NULL);//(CFArrayRef)items;

    

    NSMutableDictionary * options = [[NSMutableDictionary alloc] init];

// Set the public key query dictionary.

    [options setObject:@"some password for p12 file" forKey:(id)kSecImportExportPassphrase];

    SecPKCS12Import((CFDataRef) data, (CFDictionaryRef)options, &tmpCFArrayRef);

    

    NSMutableDictionary * items = (NSMutableDictionary*) [tmpCFArrayRef objectAtIndex:0];

    kCFAllocatorDefault, (const void **) &cert, 1, NULL);

    

    SecTrustRef trust = (SecTrustRef)[items objectForKey:(id)kSecImportItemTrust];

    

    pub_key_leaf = SecTrustCopyPublicKey(trust);

    int cipherBufferTotalSize = ceil(len/(float)BUFFER_SIZE)*256;

    uint8_t * cipherBuffer = (uint8_t *)calloc(cipherBufferTotalSize, sizeof(uint8_t));

    

    int procPlainBuffer = 0;

    int procCipherBuffer = 0;

    while (procPlainBuffer < len) {

        

        uint8_t * plainBufferChunc = (uint8_t *)calloc(BUFFER_SIZE, sizeof(uint8_t));

        uint8_t * cipherBufferChunc = (uint8_t *)calloc(256, sizeof(uint8_t));

        

        memcpy(plainBufferChunc, plainBuffer + procPlainBuffer, BUFFER_SIZE);

        

        [self encryptChunk :plainBufferChunc :cipherBufferChunc];

        procPlainBuffer += BUFFER_SIZE;

        

        memcpy(cipherBuffer + procCipherBuffer, cipherBufferChunc, 256);

        procCipherBuffer += 256;

        

        free(cipherBufferChunc);

        free(plainBufferChunc);

    }

    

    NSData* finalData = [[[NSData alloc] initWithBytes:cipherBuffer

                                                length:cipherBufferTotalSize] autorelease];

    

    NSString* retRes = [finalData base64Encoding];

    

    return retRes;

}

Currently rated 3.0 by 1 people

  • Currently 3/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags:

General

Powered by BlogEngine.NET 1.4.5.0
Theme by Mads Kristensen