5

I have seen the example here. All well and good and I understand it, however it relies on the bounceycastle library for the provider. I don't want to bundle any additional third party libraries with my app if I can help it. I don't need fort knox style security, just some basic symmetric encryption for transmitting over the wire. How can I do basic encryption on Android with out third party libraries?

thanks

Community
  • 1
  • 1
MalcomTucker
  • 7,407
  • 14
  • 72
  • 93

2 Answers2

5

I don't want to bundle any additional third party libraries with my app if I can help it.

You do not need third party libraries to use javax.crypto. There are online samples of using javax.crypto, such as this and this. If you need a Base64 encoder, there is one in Android 2.2, or there are open source implementations available for that as well.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • Do you know which crypto algorithms that Android BC supports? I know it is a crippled version of BC but can't find what it actually supports. – w.donahue Sep 28 '11 at 02:46
  • @metalideath: Off the top of my head, no. Also, somebody repackaged BC as org.spongycastle to get past the "crippled version" stuff, in case what you need is not available via the `javax.crypto` package. – CommonsWare Sep 28 '11 at 11:14
5

Well BouncyCastle is included in Android, as you can see if you would try to include it you would get: D/dalvikvm( 9268): DexOpt: not verifying 'Lorg/bouncycastle/x509/extension/SubjectKeyIdentifierStructure;': multiple definitions and so on.

However, not all algorithms are implemented - you would get an NoSuchAlgorithmException. In Android 2.2 I found these to be implemented:

PBEWITHSHAAND128BITAES-CBC-BC PBEWITHSHAAND3-KEYTRIPLEDES-CBC 1.2.840.113549.1.1.7 PBEWITHSHA256AND256BITAES-CBC-BC PBEWITHSHAAND192BITAES-CBC-BC DESEDE DES 1.2.840.113549.3.7 PBEWITHSHAAND2-KEYTRIPLEDES-CBC 1.3.14.3.2.7 PBEWITHSHA256AND192BITAES-CBC-BC PBEWITHSHAAND256BITAES-CBC-BC PBEWITHSHAAND40BITRC2-CBC AES 2.16.840.1.101.3.4.1.42 PBEWITHSHA256AND128BITAES-CBC-BC 2.16.840.1.101.3.4.1.22 2.16.840.1.101.3.4.1.2

Cpt.Ohlund
  • 2,589
  • 1
  • 20
  • 15
  • 1
    I'm getting a shortbufferexception on the call to doFinal when encrypting (using the linked example) - any ideas? – MalcomTucker Aug 25 '10 at 12:28
  • Well, the example runs fine on my Nexus one, 2.2. And you shouldn't get a shortbufferexception, but you can always try to set: byte[] cipherText = new byte[2048]; To se if the encryption works, although you would get some junk in the printouts. – Cpt.Ohlund Aug 25 '10 at 12:57
  • 1
    my cipherText is much larger than that - some 3800 bytes. Does the byte[].length need to be a multiple of 16? – MalcomTucker Aug 25 '10 at 15:15