Giter Site home page Giter Site logo

fbencryptor's Introduction

CCCrypt wrapper class

FBEncryptor is enabled to encrypt/decrypt a message. Supported encryption algorithm is AES 256 bit only. Additionally BASE64 encode/decode is supported.

Usage

(1) Encrypt/decrypt plain text message with Base64 encoding

Encrypt)

NSString* encrypted = [FBEncryptorAES encryptBase64String:@"Hello"
										        keyString:@"somekey"
											separateLines:NO];

The output string is encoded with Base64.

example)
message: @"Hello"
output : @"gT2IUF9Jzmn7wglXk3XC3w=="

if 'separateLines:' is NO, no CR/LF characters will be added. Otherwise a CR/LF pair will be added every 64 encoded chars.

Decrypt)

NSString* decrypted = [FBEncryptorAES decryptBase64String:encrypted
										        keyString:key];

(2) Encrypt/decrypt binary data

NSData* encryptedData = [FBEncryptorAES encryptData:data
										    keyData:key
											     iv:iv];

NSData* decryptedData = [FBEncryptorAES decryptData:encryptData
										    keyData:key
											     iv:iv];

The iv is called 'initailization vector' for CBC mode. it is abled to be set nil.

(3) Generate iv

NSData* iv = [FBEncryptorAES generateIv];

It generates a 16 bytes random binary value. You can use this value for +encryptData:keyData:iv:.

(4) Utiities

NSData* iv = [FBEncryptorAES generateIv];
NSString* hexString = [FBEncryptorAES hexStringForData:iv];

example: @"b20cd8d972e65762824cc3190040388c"

NSData* bin = [FBEncryptorAES dataForHexString:hexString];

Features

  • Only support AES256/CBC/PKCS7padding
  • Base64 encoding is suported

Customize

It is able to change below constants:

FBEncryptor.h
#define FBENCRYPT_KEY_SIZE      kCCKeySizeAES256

If you want to use AES 128 bit key, you can set the constant like below:

#define FBENCRYPT_KEY_SIZE      kCCKeySizeAES128

NOTE: If you change the constant, the testcase will be failed (it is made for AES 256 bit key).

Installation

You should copy below files to your projects.

FBEncryptor.h
FBEncryptor.m
NSData+Base64.h
NSData+Base64.m

Etc

Special thanks for Matt Gallagher (author of NSData+Base64 code).

"Cocoa with Love: Base64 encoding options on the Mac and iPhone" http://cocoawithlove.com/2009/06/base64-encoding-options-on-mac-and.html

License

(1) FBEncryptorAES.m/FBEncryptorAES.h

MIT

Copyright (c) 2011 Hiroshi Hashiguchi

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

(2) NSData+Base64.m/NSData+Base64.h

Created by Matt Gallagher on 2009/06/03. Copyright 2009 Matt Gallagher. All rights reserved.

This software is provided 'as-is', without any express or implied warranty. In no event will the authors be held liable for any damages arising from the use of this software. Permission is granted to anyone to use this software for any purpose, including commercial applications, and to alter it and redistribute it freely, subject to the following restrictions:

  1. The origin of this software must not be misrepresented; you must not claim that you wrote the original software. If you use this software in a product, an acknowledgment in the product documentation would be appreciated but is not required.
  2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
  3. This notice may not be removed or altered from any source distribution.

fbencryptor's People

Contributors

lakesoft avatar xcatsan avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

fbencryptor's Issues

Issue while decrypting using openssl.

I want to decrypt the text by openssl command. Please help me.

I am using the following command but could not able to decrypt it:
$ openssl enc -d -aes256 -in text.txt -out decrypt.txt

I am getting bad magic number error.

It is working fine on IOS end but not working with openssl.

For ARC Optimizing and Analized Build warning

Changed as you can see in these lines.
Is It Fine?

//
// base64EncodedString
//
// Creates an NSString object that contains the base 64 encoding of the
// receiver's data. Lines are broken at 64 characters long.
//
// returns an autoreleased NSString being the base 64 representation of the
// receiver.
//

  • (NSString *)base64EncodedString
    {
    size_t outputLength;
    char *outputBuffer = NewBase64Encode([self bytes], [self length], true, &outputLength);

    // Delete the Autorelease in ORIG
    NSString *result = [[NSString alloc]
    initWithBytes:outputBuffer
    length:outputLength
    encoding:NSASCIIStringEncoding];
    free(outputBuffer);
    return result;
    }

// added by Hiroshi Hashiguchi

  • (NSString *)base64EncodedStringWithSeparateLines:(BOOL)separateLines
    {
    // ORIG size_t outputLength;
    size_t outputLength = 0;
    char *outputBuffer = NewBase64Encode([self bytes], [self length], separateLines, &outputLength);

    // Delete the Autorelease in ORIG
    NSString *result = [[NSString alloc]
    initWithBytes:outputBuffer
    length:outputLength
    encoding:NSASCIIStringEncoding];
    free(outputBuffer);
    return result;
    }

Mismatch with other resources

Hi,
As I'm testing your encryptor, the out put is not matched with http://aesencryption.net/.
We need to use AES 256 in both server side and the iOS app but I can not get the right out put for any data.
For ex:
NSString* encrypted = [FBEncryptorAES encryptBase64String:@"hello"
keyString:@"somekey"
separateLines:NO];
returns me: gCSyt7HUKZY/cUda3zcb4w==

but the http://aesencryption.net/ returns : i6PoffMYyd1ZLQSFl03DxWmIbD5dvOQRXLmjNnHmmjM=

As you both said the out puts are Base64 too.

Where is the problem? Please help me out.

Thanks.

problem in decrypting

when i am using a new encrypted code to decrypted then only one char string is showing with save key and iv

Data decryption truncated

This happens only in iOS6.

I'm using [FBEncryptorAES decryptData: key: iv:] and everything decrypts fine in ios 5.1.1 and below.
But when i'm doing this in iOS6, my text seem to be truncated at random lengths.

In certain cases [ERROR] failed to decrypt| CCCryptoStatus: -4304

Very helpful encryption library for CCCrypt. Thank you for sharing. For certain strings decryption works but some times decryption fails and the error message I get is [ERROR] failed to decrypt| CCCryptoStatus: -4304. I haven't modified the configuration. Any ideas?

FBEncrypter compatibility with android.

Hi there,

Are you provide support for android also . if not then how can I use this library to fulfil my need in android also or if you can suggest me another other encryption library that work same as FBEncryptor then it is very helpful for me.

Thanks.

I got a warning generateIv method in FBEncryptorAES.m

generateIv Method

+ (NSData*)generateIv
{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        srand(time(NULL));
    });

    char cIv[FBENCRYPT_BLOCK_SIZE];
    for (int i=0; i < FBENCRYPT_BLOCK_SIZE; i++) {
        cIv[i] = rand() % 256;
    }
    return [NSData dataWithBytes:cIv length:FBENCRYPT_BLOCK_SIZE];
}

The warning...
FBEncryptorAES.m:156:15: Implicit conversion loses integer precision: 'time_t' (aka 'long') to 'unsigned int'

php encrypted string can not be decrypted

in PHP , I do this to encrypt, but the encrypted string can not be decrypted with FBEncryptor.
Am I doing something wrong?

rtrim(
base64_encode(
mcrypt_encrypt(
MCRYPT_RIJNDAEL_256,
$sSecretKey, $sValue,
MCRYPT_MODE_ECB,
mcrypt_create_iv(
mcrypt_get_iv_size(
MCRYPT_RIJNDAEL_256,
MCRYPT_MODE_ECB
),
MCRYPT_RAND)
)
), "\0"
);

Use correct terminology in readme

Hi. The readme states the following:

"Encrypt/decrypt plain text message with Base64 encoding"

Since when is Base64 considered encryption? It has absolutely nothing to do with encryption and relying on base64 for security purposes is just wrong.

crypte/decrypte issues just on io 6

first i want to thank you for charing this lib.
I use FBEncryptor in one of my projects and it work fine but when i test in ios 6 the decrypt function always return "0" ( all good statue ), if i put the good password, the data returned after decrypt is good to, but with a wrong password, data is not nil and statue is "0".
I don't know if the wifi problem in ios 6 is the same as mine, because if users change on router to WPA2 TKIP from AES it work fine.

is this an issue in ios 6 or i can do something to test the file 'validity' after decrypt ?

its really an 'emergency' so thank you for help .

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.