1

I have a android project and used the RSA encryption, and it works ok between the android client and java server. The public key and private key was generated using android API. Now i want to use the same RSA on the IOS project and use the same keys. Unfortunately it doesn't work well. I can encrypt and decrypt just on the IOS client, and it right. When decrypted on the java server, it got error. Using kSecPaddingPKCS1 for encrypting, and RSA/NONE/OAEPWithSHA1AndMGF1Padding for decrypting.

reference: 1.RSA: encrypt in iOS, decrypt in Java 2.RSA implementations in Objective C

Can anyone help, thank you.

Community
  • 1
  • 1
user6299706
  • 101
  • 8

2 Answers2

1

I can see (with my limited knowledge of cryptography ) you are using different algorithms to encrypt and decrypt, and they are not compatible.

Change kSecPaddingPKCS1 with kSecPaddingOAEP as suggests the accepted answer of the first link.

pedrofb
  • 37,271
  • 5
  • 94
  • 142
  • Yes, I had replaced kSecPaddingPKCS1 with kSecPaddingOAEP, and it didn't work too. I can encrypt and decrypt in my App client. @pedrofb, thank you – user6299706 Jan 10 '17 at 06:10
-1

So you say you can encrypt and decrypt in iOS cilent, it express that your public-key and private-key are right. Therefore the problem is between cilent and server. I have been ever done this in my project, so I could some point for you.

1.RSA-key has some different bit. Like 512bit, 1024bit. You should check this.Encrypt in iOS use the same bit with decrypt in java server.

2.Do you use additional encryption. Like base64, you also check that you use the same style with your java server.

this is a related article : http://www.jianshu.com/p/db85399e8a76 Hope could help you.

pusswzy
  • 37
  • 4
  • Thank you. I am sure using the same bit between IOS and Java. And i used the base64 too. But it still didn't work. – user6299706 Jan 09 '17 at 01:49
  • Maybe the issue is in base64. You could try to use other style in base64, like websafe/NoPadding/Padding and so on. It is important to use the same style in base64. – pusswzy Jan 09 '17 at 10:08