简体   繁体   中英

Encrypt a string with C# and decrypt it on iOS and Android

I'm developing a WCF Web Service with C# and .NET Framework 4.0. I'm also developing an iOS and Android client to connect with it.

On server side, I have to generate an aleatory password, encrypt it, and send it back to iOS client or to an Android client.

I don't know how can I encrypt a string with .NET Framework. Searching on Internet, I see that there are algorithms to do it: MD5, RSA, etc. (I'm not sure if I have understood how to encrypt a string because I haven't do a string encryption yet).

If I use MD5 to encrypt a password, what do I have to do to decrypt it on iOS and on Android?

What do you recommend me?

Encrypting the string is easy in .Net. You can use the built in System.Security.Cryptography.MD5.

Here is a link for that: http://msdn.microsoft.com/en-us/library/system.security.cryptography.md5.aspx

Here is a question very similar to yours regarding encrypting/decryption between languages.. mfanto has a good answer.

Best practices for passing encrypted data between different programming languages

The question is how serious your encryption must be? MD5 is not a serious thing. And as for RSA, you should take into the account that this is an asyn method of encryption which consumes a pair of public and private keys. Depending on your goals it would be possible to say whether RSA is good enough for you or not. And of course, never use public libraries for encryption(including MS) if you develop something serious.

I wrote RSA and AES implementation using CommonCrypto, implementation is done in order to be interoperable with .NET

Check it out

https://github.com/ozgurshn/EncryptionForiOS

Creating an md5 hash of a Keyword is easy in .net:

MD5 hashKey = new MD5CryptoServiceProvider();
var hashedKey = hashKey.ComputeHash(ASCIIEncoding.ASCII.GetBytes("password"));

Note: hashed key is byte array, you can build a string from it or do further encrpytion (fe change into digits, convert it to hex...). You need using System.Security.Cryptography;

On Ios/Android you have to do the same, encrypt the string with the MD5algorithm (which is alway the same, thats why it works) and compare the two results.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM