Comments (2)
Hi,
this works fine with C# and android but not working with IOS. Finally found the below solution which working fine in IOS.
#import <CommonCrypto/CommonCryptor.h>
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
NSString *sData = @"Meet me at the secret location at 8pm";
NSString *sIv = @"4QesEr03HwE5H1C+ICw7SA==";
NSString *sKey = @"ovA6siPkyM5Lb9oNcnxLz676K6JK6FrJKk4efEUWBzg=";
NSData *dData = [sData dataUsingEncoding:NSUTF8StringEncoding];
NSData *dEncrypt = [self doCipher:dData key:sKey iv:sIv context:kCCEncrypt];
NSData *base64 = [dEncrypt base64EncodedDataWithOptions:0];
NSString *sBase64 = [[NSString alloc] initWithData:base64 encoding:NSUTF8StringEncoding];
NSLog(@"Base64 String: %@",sBase64);
NSData *dDecrypt= [self decrypt:dEncrypt key:sKey iv:sIv];
NSString *sDecrypt = [[NSString alloc] initWithData:dDecrypt encoding:NSUTF8StringEncoding];
NSLog(@"Decrypted Data: %@",sDecrypt);
}
- (NSData *)doCipher:(NSData *)plainText
key:(NSString *)key
iv:(NSString *)iv
context:(CCOperation)encryptOrDecrypt
{
NSUInteger dataLength = [plainText length];
size_t buffSize = dataLength + kCCBlockSizeAES128;
void *buff = malloc(buffSize);
size_t numBytesEncrypted = 0;
NSData *dIv = [iv dataUsingEncoding:NSUTF8StringEncoding];
NSData *dKey = [key dataUsingEncoding:NSUTF8StringEncoding];
CCCryptorStatus status = CCCrypt(encryptOrDecrypt,
kCCAlgorithmAES128,
kCCOptionPKCS7Padding,
dKey.bytes, kCCKeySizeAES256,
dIv.bytes,
[plainText bytes], [plainText length],
buff, buffSize,
&numBytesEncrypted);
if (status == kCCSuccess) {
return [NSData dataWithBytesNoCopy:buff length:numBytesEncrypted];
}
free(buff);
return nil;
}
from cross-platform-aes-encryption.
I was tried to make an IOS implementation working, but with no success. Alternative fr this example is CryptoSwift library its working with C# and Android examples. Its simply to import .xcodeproj file from an github project, and add extension for a String:
import Foundation
import CryptoSwift
extension String {
func aesEncrypt(key: String, iv: String) throws -> String{
let data = self.dataUsingEncoding(NSUTF8StringEncoding)
let enc = try AES(key: key, iv: iv, blockMode:.CBC).encrypt(data!.arrayOfBytes(), padding: PKCS7())
let encData = NSData(bytes: enc, length: Int(enc.count))
let base64String: String = encData.base64EncodedStringWithOptions(NSDataBase64EncodingOptions(rawValue: 0));
let result = String(base64String)
return result
}
func aesDecrypt(key: String, iv: String) throws -> String {
let data = NSData(base64EncodedString: self, options: NSDataBase64DecodingOptions(rawValue: 0))
let dec = try AES(key: key, iv: iv, blockMode:.CBC).decrypt(data!.arrayOfBytes(), padding: PKCS7())
let decData = NSData(bytes: dec, length: Int(dec.count))
let result = NSString(data: decData, encoding: NSUTF8StringEncoding)
return String(result!)
}
}
Works perfect.
from cross-platform-aes-encryption.
Related Issues (20)
- Encrypt in java and Decrypt in IOS - Not working HOT 1
- Decryption result getting null in IOS
- Encryption in c# is broken if string to encode contains non English characters (with fix)
- iOS Swift Problem HOT 2
- encryption and decryption error when I encrypt a long string with C#
- How to implement encryption and decryption in PHP? HOT 1
- Encrypted string in C# and Decryption IOS vise versa not working
- Facing problem while decryption in iphone5
- Cancelled issue reporter's mistake
- string decryption failed in c# which encrypted in objective c
- Android encryption and decryption facing issue
- The key is not correctly converted to hex string in iOS 13 beta
- Add authentication
- How can i use it in Swift 5 or 4 HOT 1
- .Net Support
- Unable to decrypt my encrypted data. HOT 2
- Refactor - c#, android and web
- Request for a feature in Golang
- Cut big Sentence after Decrypt
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from cross-platform-aes-encryption.