[php] Convert encrypted data to string? #959
Replies: 53 comments
-
It is not much related to CryptoSwift, more stackoverflow question. Anyway, depends what format is expected by your server you may expect different result. It may be Base64, or hex representation of bytes.
|
Beta Was this translation helpful? Give feedback.
-
The format of this
is not the same as PHP's function base64encode()... do you know anything about that? How to match it up to PHP function would be awesome. Would help alot thanks! |
Beta Was this translation helpful? Give feedback.
-
for the same input data, base64 output have to be the same. Are you sure input data is the same? |
Beta Was this translation helpful? Give feedback.
-
Yes I am sure the input data is the same. The PHP function always returns two == at the end of the string while iOS produces only one = at the end of the string. It also worries me because iOS has options for base64 encoding while PHP doesn't |
Beta Was this translation helpful? Give feedback.
-
It looks like input to base64 encryption methods are different, but if you say it's not... I don't know. Actually I'm not familiar if there is difference between iOS and PHP in regard of Base64. I would recommend double check encryption output result. |
Beta Was this translation helpful? Give feedback.
-
Yes I will double check, I have another question though. Is the AES example provided AES-256? It just says aes.encrypt but how do I know its 256? I know nothing about encryption stuff... |
Beta Was this translation helpful? Give feedback.
-
The PHP side is AES-256 that is why I'm asking. |
Beta Was this translation helpful? Give feedback.
-
256 (bits) is length of the key. If you key is 32 bytes long, then it's AES-256. |
Beta Was this translation helpful? Give feedback.
-
So if PHP and iOS have a 16 character key then they should both still product same output? I have 16 chars instead of 32. I am doing this to create a key: iOS PHP |
Beta Was this translation helpful? Give feedback.
-
16 bytes long key is for AES-128. I don't know how "Crypt" class is working, couldn't find it on php documentation site. |
Beta Was this translation helpful? Give feedback.
-
Sorry I forgot to include I am using a PHP framework called Laravel. Here is docs on Crypt class: https://github.com/laravel/framework/blob/4.2/src/Illuminate/Encryption/Encrypter.php |
Beta Was this translation helpful? Give feedback.
-
I might be doing something wrong on PHP side |
Beta Was this translation helpful? Give feedback.
-
This PHP class doesn't just do AES encryption. It is doing a much more by constructing actual message. if you need to replicate this output format you have to handle it on iOS by yourself. |
Beta Was this translation helpful? Give feedback.
-
But isn't the crypto swift padding too? The option to add padding can be turned on to match that encryption on PHp? Sent from my iPhone
|
Beta Was this translation helpful? Give feedback.
-
it is, just php encrypt() is doing much more, is calculating MAC, creating JSON, combine IV and cipher result, then this all is encoded with base64
|
Beta Was this translation helpful? Give feedback.
-
Ok thanks for the help! I will attempt what you stated in making the IV random. I am very happy that you showed me how to do all of this. |
Beta Was this translation helpful? Give feedback.
-
Ok good news! I believe I got it :D Swift: https://gist.github.com/bradbernard/2a7af4c2200cb3794768 Sigh of relief! You are awesome. |
Beta Was this translation helpful? Give feedback.
-
thats good, you made it! awsome. Have a good week. |
Beta Was this translation helpful? Give feedback.
-
Thanks! You too :D Sent from my iPhone
|
Beta Was this translation helpful? Give feedback.
-
@krzyzanowskim and @bradbernard, thanks a lot for the awesome library and providing code samples for php and Swift 👍 . However I found few issues on the Swift code and using other related resources I found, I've updated the code for latest version of CryptoSwift and made some improvements as well. If you find anything wrong, you are more than welcome to let me know :) I'm posting it here for anyone who comes here looking for answers. Swift: PHP compatible Swift AES Encryption using CryptoSwift Thank you |
Beta Was this translation helpful? Give feedback.
-
Hello, this is my encrypt function public func Encrypt(plainText text:String, plainText key:String)->String{
} and i get an error like this "Block size and Initialization Vector must be the same length!" I know there is a problem with the bytes because it requires a key with 16 bytes but please can you help me how to fix this? |
Beta Was this translation helpful? Give feedback.
-
@xhuliohasa it may be everything, but you did not show us input data. this is crucial. Please make sure that IV is in the right size of AES.blockSize |
Beta Was this translation helpful? Give feedback.
-
how do i assign it because i'm new in swift and it's crypting, i know this is a beginner's question but please help me |
Beta Was this translation helpful? Give feedback.
-
Thank you @krzyzanowskim for these clarifications. However, these examples are from 2015 and now in 2017 I believe much has changed in CryptoSwift and Swift. For example, from your code on Feb 2, 2015, the following line doesn't work at all anymore:
Xcode claims Cipher.AES is not available or something to that effect. Could you produce an similar example with BASE64 decoding and all that uses the "try AES()" way of doing this, which I guess is a more current form in CryptoSwift? Something like this I guess: I tried putting in your code from FEB 2, 2015, but Xcode doesn't accept much of it anymore, force-modifies it and I'm not sure if it's working as planned after all those Xcode syntax modifications. Also thus a clean modern example would be very much appreciated. Thank you in advance. |
Beta Was this translation helpful? Give feedback.
-
In particular with live-data and my modifications, I'm getting this error, or nils:
|
Beta Was this translation helpful? Give feedback.
-
@teppotk look at README, check Playground it's all there. |
Beta Was this translation helpful? Give feedback.
-
@krzyzanowskim Thanks, I've actually got things working pretty well in Swift with my own test data, but not with actual data that I have incoming. The problem comes when incoming string has 16 bytes of IV included in the beginning. Once I get that subrange out from the beginning, the rest i.e. the message, doesn't decrypt anymore. CryptoSwift complains about padding error. Since there's no .padding attribute anymore (I believe), I've tried creating my own padding algorithm, but I don't think it works as it should as output is garbled and doesn't decrypt right. This case is not shown in the current README I believe. Would it be possible to add IV subrange removal + padding handling somewhere in the README? This particular example below is probably what I'd need. But this old code (again from FEB 2, 2015) goes all red in Xcode and doesn't work anymore:
|
Beta Was this translation helpful? Give feedback.
-
Please help,in my case im not using IV and keyStr just convert string this is my code: var strTest = "asdfasdf"
but in this line: and this line too: please help |
Beta Was this translation helpful? Give feedback.
-
@ferdinandharmaputra you cannot "not use IV" in CBC mode. I believe there is an AES example in README, did you try it? |
Beta Was this translation helpful? Give feedback.
-
I did this is my code do {
// ciphertext += try encryptor.makeEncryptor().finish()
but still difference with what backend and android team expected my android team use this private static SecretKeySpec generateMySQLAESKey(String key, String encoding) { but I get stuck to converted as swift 3.2 |
Beta Was this translation helpful? Give feedback.
-
I need to send encrypted data as string to server.
I tried :
let encryptedString = NSString(data: encryptedData, encoding: NSUTF8StringEncoding)
println(encryptedString)
but the result is nil
So can you add a function to convert encrypted data to string?
Beta Was this translation helpful? Give feedback.
All reactions