• 2015-02-03

    iOS Objective-C RSA encrypt with only public key and descrypt with PHP

    Views: 62798 | 15 Comments

    If you found this post via Google, I am sure this is the final link you click. Your problem will be solved with a single function without any dependency!

    You may have read these posts:

    The first post helps, but it is too old, and it doesn’t provide the full code, the author is so mean!

    The second is nonsense, because you people finding this post must have the exact need as me: iOS RSA encrypt with ONLY the public key. NO .der file, NO CA, NO bullshit!

    OK, lets go on.

    You generate RSA public key and private key with PHP

    $config = array(
    		"digest_alg" => "sha512",
    		"private_key_bits" => 1024,
    		"private_key_type" => OPENSSL_KEYTYPE_RSA,
    		);
    $res = openssl_pkey_new($config);
    openssl_pkey_export($res, $privKey);
    $pubKey = openssl_pkey_get_details($res);
    $pubKey = $pubKey["key"];
    var_dump($privKey, $pubKey); // now you get keys in plain text
    

    It is like:

    -----BEGIN PRIVATE KEY-----
    MIICdwIBADANBgkqhkiG9w0BAQEFAASCAmEwggJdAgEAAoGBAMQKGp7zSUktNOQk
    PdfcvJ3sWP7O46ENmSO4s0+iDdhHbR7klyt2oj0gXM1sAJj8ZBWFudu8GpiDKqXw
    N88IZemfT5c1LEQshTD1WHfZC6EY2vf9MGl5yGtV5WkU8vDJpg0STUJrCAcJ5Cp/
    m5qqJqgEM5Op6jHzwtzWV3syx+CBAgMBAAECgYEApSzqPzE3d3uqi+tpXB71oY5J
    cfB55PIjLPDrzFX7mlacP6JVKN7dVemVp9OvMTe/UE8LSXRVaFlkLsqXC07FJjhu
    wFXHPdnUf5sanLLdnzt3Mc8vMgUamGJl+er0wdzxM1kPTh0Tmq+DSlu5TlopAHd5
    IqF3DYiORIen3xIwp0ECQQDj6GFaXWzWAu5oUq6j1msTRV3mRZnx8Amxt1ssYM0+
    JLf6QYmpkGFqiQOhHkMgVUwRFqJC8A9EVR1eqabcBXbpAkEA3DQfLVr94vsIWL6+
    VrFcPJW9Xk28CNY6Xnvkin815o2Q0JUHIIIod1eVKCiYDUzZAYAsW0gefJ49sJ4Y
    iRJN2QJAKuxeQX2s/NWKfz1rRNIiUnvTBoZ/SvCxcrYcxsvoe9bAi7KCMdxObJkn
    hNXFQLav39wKbV73ESCSqnx7P58L2QJABmhR2+0A5EDvvj1WpokkqPKmfv7+ELfD
    HQq33LvU4q+N3jPn8C85ZDedNHzx57kru1pyb/mKQZANNX10M1DgCQJBAMKn0lEx
    QH2GrkjeWgGVpPZkp0YC+ztNjaUMJmY5g0INUlDgqTWFNftxe8ROvt7JtUvlgtKC
    XdXQrKaEnpebeUQ=
    -----END PRIVATE KEY-----
    
    -----BEGIN PUBLIC KEY-----
    MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDEChqe80lJLTTkJD3X3Lyd7Fj+
    zuOhDZkjuLNPog3YR20e5JcrdqI9IFzNbACY/GQVhbnbvBqYgyql8DfPCGXpn0+X
    NSxELIUw9Vh32QuhGNr3/TBpechrVeVpFPLwyaYNEk1CawgHCeQqf5uaqiaoBDOT
    qeox88Lc1ld7MsfggQIDAQAB
    -----END PUBLIC KEY-----
    

    Then you send the public key to iOS app

    So, your Objective-C gets only this data(no other bullshit!):

    -----BEGIN PUBLIC KEY-----
    MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDEChqe80lJLTTkJD3X3Lyd7Fj+
    zuOhDZkjuLNPog3YR20e5JcrdqI9IFzNbACY/GQVhbnbvBqYgyql8DfPCGXpn0+X
    NSxELIUw9Vh32QuhGNr3/TBpechrVeVpFPLwyaYNEk1CawgHCeQqf5uaqiaoBDOT
    qeox88Lc1ld7MsfggQIDAQAB
    -----END PUBLIC KEY-----
    

    You need a function to do RSA encryption

    + (NSString *)encryptString:(NSString *)str publicKey:(NSString *)pubKey;
    

    YES! No SecRef, No Security.h, with exactly one function, of two arguments.

    Now your code is as clean as

    NSString *pubkey = get_public_key_string_with_http();
    NSString *ret = [RSA encryptString:@"hello world!" publicKey:pubkey];
    NSLog(@"encrypted: %@", ret);
    

    Screw 100% percent security!

    Many Apps don’t need 100% percent security, it only needs encryption, with RSA public key only!

    Objective-C RSA encrypt with public key, full code on GitHub: https://github.com/ideawu/Objective-C-RSA

    Posted by ideawu at 2015-02-03 12:49:01 Tags: ,
  • 2015-02-02

    Add Pull to Refresh feature to your iOS App

    Views: 31013 | No Comments

    With CocoaUI’s ITable view controller, you can easily add Pull to Refresh feature to your iOS App, you can add “Pull Down to Refresh” and “Pull Up to Load More” at the same time.

    if(!self.headerRefreshControl){
        IRefreshControl *header = [[IRefreshControl alloc] init];
        self.headerRefreshControl = header;
    }
    if(!self.footerRefreshControl){
        IRefreshControl *footer = [[IRefreshControl alloc] init];
        self.footerRefreshControl = footer;
    }
    

    That’s it, put this code in your view controller’s viewDidLoad method, then your App has the ability of “Pull to Refresh”.

    Continue reading »

    Posted by ideawu at 2015-02-02 14:41:19 Tags: ,
  • 2015-01-29

    iOS display bottom bar with inputAccessoryView

    Views: 21712 | No Comments

    You can display a navigation toolbar at the bottom of the screen, the bottom bar is managed by the navigation controller.

    [self.navigationController setToolbarHidden:NO];
    [self.navigationController.toolbar setBarStyle:UIBarStyleDefault];
    self.navigationController.toolbar.translucent = NO;
    

    But there are some bad sides of this approach:

    • you are not able to change its height(not easily)
    • the bottom bar will be covered by keyboard

    Continue reading »

    Posted by ideawu at 2015-01-29 14:18:51 Tags:
  • 2015-01-26

    Flow Layout UI for iOS Apps

    Views: 24140 | No Comments

    Flow Layout is the key of Web UI, it is a more human natural mechanism of laying out UI/GUI widgets in the screen. With Flow Layout, UI widgets like buttons, labels, text-inputs are placed one by one into an area(rectangle) of the screen, each widget occupy its own area, normally, widgets are not overlapped.

    Years ago, there is only Fixed Layout for iOS Apps development, Fixed Layout requres developers to determine the exact position(x, y) and size(width, height) of each widget, so it is so limited functionality, one of its most noticable weakness is that it can not properly deal with iPhone 4 and iPhone 5, because they have different screen resolutions.

    Later Apple introduced Auto Layout layout, which is yet another Relative Layout. Flow Layout and Auto Layout(Relative Layout) are both adaptive UI layout mechanisms, both work fine with multi screen resolutions.

    But the most significant between them is that:

    • Flow Layout focus on one widget at a time.
    • Auto Layout requires two or more widgets to work around.

    Apparently, dealing with one thing is simpler than dealing with many things. So, Flow Layout is superior to Auto Layout, not mention that Apple’s Auto Layout over complicated.

    Continue reading »

    Posted by ideawu at 2015-01-26 12:42:30 Tags: ,
  • 2015-01-26

    ITable demo – fixed header for UITableView datagrid

    Views: 21545 | No Comments

    The table has header row(like th in html) that anchors at the top.

    //
    //  ITableFixedHeaderDemo.h
    //  IKit
    //
    //  Created by ideawu on 15-1-22.
    //  Copyright (c) 2015年 ideawu. All rights reserved.
    //
    
    #import "IKit/IKit.h"
    
    @interface ITableFixedHeaderDemo : ITable
    
    @end
    
    //
    //  ITableFixedHeaderDemo.m
    //  IKit
    //
    //  Created by ideawu on 15-1-22.
    //  Copyright (c) 2015 ideawu. All rights reserved.
    //
    
    #import "ITableFixedHeaderDemo.h"
    
    @implementation ITableFixedHeaderDemo
    
    - (id)init{
        self = [super init];
        self.navigationItem.title = @"ITableFixedHeaderDemo";
        self.tableView.backgroundColor = [UIColor groupTableViewBackgroundColor];
    
        {
            ITableRow *headerRow = [[ITableRow alloc] initWithNumberOfColumns:3];
            [headerRow.style set:@"font-weight: bold; text-align: center; background: #6cf;"];
            [headerRow column:0 setText:@"Id"];
            [headerRow column:1 setText:@"Name"];
            [headerRow column:2 setText:@"Age"];
            self.tableHeader = headerRow;
        }
        
        for(int i=0; i<20; i++){
            ITableRow *row = [[ITableRow alloc] initWithNumberOfColumns:3];
            [row.style set:@"text-align: center; border-bottom: 1 solid #eee; background: #fff;"];
            [row column:0 setText:[NSString stringWithFormat:@"%d", i+1]];
            [row column:1 setText:[NSString stringWithFormat:@"name-%d", i+1]];
            [row column:2 setText:[NSString stringWithFormat:@"%d", rand()%50+1]];
            [self addIViewRow:row];
        }
        
        return self;
    }
    
    @end
    
    Posted by ideawu at 11:37:23 Tags: ,
|<<<123>>>| 1/3 Pages, 12 Results.