How to decrypt AES 128 with swift using CryptoSwift framework - ios

I'm working on swift project that is scanning QR codes and getting encrypted AES-128 data that needs to be decrypted.
I'm using a framework that is called CryptoSwift.
I have the key and data from QR code that holds the encrypted data.
var key = "B7zqj4TAXnPevYZAR4T26969"
var qrData = "zWDzClfre4aOjTumzGsnpqh4Sje7sFsbKceA3/OSH3nKRwc7/6fYUajBr/bLh9BB"
Edit: Sorry, I forgot to mention that the encryption made with:
Cipher : AES-128
Mode of Operation : CBC
Padding : PKCS7
Key derived from : Simple decoding
I'd like to know what approaches I should do in order to decrypt this QR data.
Any help would be nice.

See this SO answer for sample Swift AES code.
When you say: "Cipher : AES-128" that is ambiguous. presumably you mean a 128-bit key size. The key supplied is 24 bytes which would be a key size of 192-bits.
CBC mode requires an iv but none is specified. Many inmplementations will use 0x00 bytes by default but that is not guaranteed, Common Crypto does. But it is always best to supply the iv.
What do you expect wen you say: "Key derived from : Simple decoding"? The current best practives solution is PBKDF2.
You need a Bridging header and add #import in it. If you dont have a bridging header let the system add one for out, see Adding a Bridging Header, you can delete the .m file after step 3.
You also need to add Security.framework to the project. What version if Swift are you using.

Related

How to add integrate code from Github into your existing project

I want to get the device location in the application that I want to develop. I have created a SWIFT project and I want to add the code here
https://github.com/intuit/LocationManager
into my existing swift project.
So here it says
Manually from GitHub
Download all the files in the INTULocationManager subdirectory.
Add the source files to your Xcode project (drag and drop is easiest).
Import the INTULocationManager.h to your bridging header.
Swift: Add #import "INTULocationManager.h" to your bridging header.
So basically can I just drag the source code to my project?
Also how do I create the bridging header?
then let say how do I use it in my ViewController?
Thanks
Maybe you should consider using CocoaPods as it may be easier for you to integrate, and in the long-run, is easier to update.
To answer your specific question about a manual installation:
Yes, you can just drag the source code into your project. Instructions for creating the bridging header can be found on the internet (for example, here).
Next, as the instructions say, add #import "INTULocationManager.h" to your bridging header, e.g:
#ifndef TestBridgingHeader_Bridging_Header_h
#define TestBridgingHeader_Bridging_Header_h
#import "INTULocationManager.h"
#endif /* TestBridgingHeader_Bridging_Header_h */
To use the library, you have to translate the example given on the Github page from Objective-C to Swift.
let locMgr = INTULocationManager.sharedInstance()
locMgr.requestLocationWithDesiredAccuracy(INTULocationAccuracy.City, timeout: 10, block: { currentLocation, achievedAccuracy, status in
if status == .Success {
} else if status == .TimedOut {
} else {
}
})
This is made easier by remembering a few rules:
Method calls in Obj-C are called via enclosing square brackets (e.g. [INTULocationManager sharedInstance]) while in Swift they use dot-syntax (e.g. INTULocationManager.sharedInstance())
Enums such as INTULocationAccuracyCity generally get translated to only their last part (.City in this case). Swift infers that .City is equivalent to INTULocationAccuracy.City.
Obj-C blocks such as INTULocationRequestBlock are equivalent to Swift closures of the same type.

Using NS_ENUM from Objective-C in Swift

I am using a third party library in my Objective-C project that has an enum defined as:
typedef NS_ENUM(NSUInteger, RJBEvent)
{
RJB_EVENT_OK = 1,
RJB_EVENT_ERROR=2,
RJB_EVENT_START = 4,
};
Then in Objective-C, I can do the following:
[self.rjbLib listenForEvents:(RJB_EVENT_START|RJB_EVENT_OK|RJB_EVENT_ERROR)];
As an exercise to teach myself Swift, I'm porting the app. All's well until I run up against using this enum. There's a ton of info out there about how (or how not to) use enums in Swift, but very little to describe this bitmask-style usage. I've got this, and it compiles, but I'm not receiving the expected event notifications.
let rjbEventsMask : UInt32 = UInt32(RJBEvent.RJB_EVENT_OK.rawValue |
RJBEvent.RJB_EVENT_ERROR.rawValue |
RJBEvent.RJB_EVENT_START.rawValue)
I do see a suggestion on NSHipster that I may need to change the third-party header file to use NS_OPTIONS. I'm going to try that, but changing the developer's provided .h file is a bit dangerous, so it's not my preferred approach.
Any guidance is appreciated.
Thanks!
Rob

Why do we get the address of AVCaptureSessionInterruptionReasonKey in AVCam sample code?

I am reading this sample code (AVCam) about AVCaptureSession and etc. And I notice this following line (link to the code):
// In iOS 9 and later, the userInfo dictionary contains information
// on why the session was interrupted.
if ( &AVCaptureSessionInterruptionReasonKey ) {
...
}
The comment in the code make sense. But what doesn't make sense to me is why do we want to get the address of AVCaptureSessionInterruptionReasonKey. It is defined as the following (in AVCaptureSection.h):
AVF_EXPORT NSString *const AVCaptureSessionInterruptionReasonKey NS_AVAILABLE_IOS(9_0);
If the key is defined, how will its address be nil? If this key is not defined, the code will never be compiled, right? Could someone explain to me how this if statement work?
AVCaptureSessionInterruptionReasonKey was added in iOS 9.0. Such an if statement is needed only if your app also supports iOS 8 or earlier.
When the code is run on a device with iOS 9 or later, the value will be non-nil and the if statement will be true. On devices with iOS 8 or earlier, the value will be nil and the if statement will be false.
If your app only supports iOS 9 or later, there is no need for the if statement.
Read the SDK Compatibility Guide in the iOS docs for more details on this type of check.

How to set up the iOS Telegram Source Code

Hi I have downloaded the iOS source code for iOS but for some reason I cannot seem to find the place where the API_ID is anywhere. Please can you tell me where this information is in the source code so I can add in my own.
Add a file called "config.h" beside Telegraph.xcodeproj and place this code in that:
#define SETUP_API_ID(apiId) apiId = YOUR_API_ID;
#define SETUP_API_HASH(apiHash) apiHash = YOUR_API_HASH;
Then replace YOUR_API_ID and YOUR_API_HASH with values obtained from telegram api support.
Then compile and enjoy it!
Try to use the project search and put API, it`ll return the files where is.

Where is the class CryptoCommon in xamarin

Im trying to use the CryptoCommon class but unable the find it in the monotuch assembly.
I found the assembly Mono.Security.Cryptography, does it have the same performance as the CryptoCommon class?
Thanks!!
CommonCrypto is used internally inside Xamarin.iOS, this is not something extra - i.e. there's no need to opt-in or opt-out.
What it means is that it's use is totally transparent to your code. If an algorithm is available in CommonCrypto then using the classic .NET type will use it.
E.g.
// this will use CommonCrypto. AES is supported by CommonCrypto
// if your device supports it AES can be hardware accelerated
var aes = Aes.Create ();
// this will also use CommonCrypto. SHA-1 is supported by CommonCrypto
// if your device supports it SHA-1 can be hardware accelerated
var sha = new SHA1Managed ();
// this will not use CommonCrypto since the algorithm is not supported by Apple
var r = RIPEMD160.Create ();
More information about CommonCrypto can be found on my blog.

Resources