Class Cipher
Convenience entry points for symmetric (AES) and asymmetric (RSA)
encryption. The actual algorithms run on the platform's native crypto
provider -- this class is just a thin, friendly facade over the
CodenameOneImplementation crypto bridge.
Recommended transformations
- AES:
AES/GCM/NoPaddingfor authenticated encryption (uses a 12-byte nonce and produces ciphertext with a 16-byte tag appended). Falls back toAES/CBC/PKCS5Paddingif GCM is unavailable on a target platform. - RSA:
RSA/ECB/OAEPWithSHA-256AndMGF1Paddingfor new code,RSA/ECB/PKCS1Paddingonly for interop with old systems.
Example: AES-GCM round-trip
SecretKey key = KeyGenerator.aes(256);
byte[] nonce = SecureRandom.bytes(12);
byte[] cipher = Cipher.aesEncrypt(Cipher.AES_GCM, key, nonce, null, "secret".getBytes("UTF-8"));
byte[] plain = Cipher.aesDecrypt(Cipher.AES_GCM, key, nonce, null, cipher);
Example: RSA-OAEP round-trip
KeyPair kp = KeyGenerator.rsa(2048);
byte[] cipher = Cipher.rsaEncrypt(Cipher.RSA_OAEP_SHA256, kp.getPublicKey(), data);
byte[] plain = Cipher.rsaDecrypt(Cipher.RSA_OAEP_SHA256, kp.getPrivateKey(), cipher);
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final StringAES/CBC/NoPadding-- raw CBC, caller must pre-pad to a 16-byte boundary.static final StringAES/CBC/PKCS5Padding-- block-chained AES with PKCS#5 padding.static final StringAES/ECB/PKCS5Padding-- legacy interop only.static final StringAES/GCM/NoPadding-- recommended authenticated mode for AES.static final StringRSA/ECB/OAEPWithSHA-256AndMGF1Padding-- recommended RSA encryption transformation.static final StringRSA/ECB/PKCS1Padding-- legacy RSA padding, kept for interop. -
Method Summary
Modifier and TypeMethodDescriptionstatic byte[]aesDecrypt(String transformation, SecretKey key, byte[] iv, byte[] aad, byte[] ciphertext) Decrypts AES ciphertext produced byaesEncrypt(String, SecretKey, byte[], byte[], byte[]).static byte[]aesEncrypt(String transformation, SecretKey key, byte[] iv, byte[] aad, byte[] plaintext) Encrypts with AES.static byte[]rsaDecrypt(String transformation, PrivateKey key, byte[] ciphertext) Decrypts an RSA ciphertext.static byte[]rsaEncrypt(String transformation, PublicKey key, byte[] plaintext) Encrypts a small amount of data with RSA.
-
Field Details
-
AES_GCM
AES/GCM/NoPadding-- recommended authenticated mode for AES.- See Also:
-
AES_CBC_PKCS5
AES/CBC/PKCS5Padding-- block-chained AES with PKCS#5 padding.- See Also:
-
AES_CBC
AES/CBC/NoPadding-- raw CBC, caller must pre-pad to a 16-byte boundary.- See Also:
-
AES_ECB_PKCS5
AES/ECB/PKCS5Padding-- legacy interop only. ECB leaks structure; avoid for new designs.- See Also:
-
RSA_OAEP_SHA256
RSA/ECB/OAEPWithSHA-256AndMGF1Padding-- recommended RSA encryption transformation.SHA-256 is used for the label hash and for MGF1. That is worth stating because the JCE reading of this name leaves MGF1 on SHA-1, and this API deliberately does not: Web Crypto's
RSA-OAEPtakes a single hash and applies it to both, as does Apple's SecKey, so the split pairing is not expressible on the JavaScript or iOS ports at all. SHA-256 for both is the only choice every port can produce, so it is the one that lets ciphertext cross between them. The JavaSE and Android ports pass an explicitOAEPParameterSpecrather than inheriting their provider's default.Interoperating with an external system that uses the JCE default (SHA-1 MGF1) therefore needs that system to name SHA-256 for the mask as well.
- See Also:
-
RSA_PKCS1
RSA/ECB/PKCS1Padding-- legacy RSA padding, kept for interop.- See Also:
-
-
Method Details
-
aesEncrypt
public static byte[] aesEncrypt(String transformation, SecretKey key, byte[] iv, byte[] aad, byte[] plaintext) Encrypts with AES.
Parameters
-
transformation: one ofAES_GCM,AES_CBC_PKCS5,AES_CBC,AES_ECB_PKCS5 -
key: AES key (16, 24 or 32 bytes for AES-128/192/256) -
iv: initialisation vector for CBC (16 bytes) / nonce for GCM (12 bytes recommended). Pass null for ECB. -
aad: associated authenticated data -- GCM only, may be null -
plaintext: data to encrypt
-
-
aesDecrypt
public static byte[] aesDecrypt(String transformation, SecretKey key, byte[] iv, byte[] aad, byte[] ciphertext) Decrypts AES ciphertext produced byaesEncrypt(String, SecretKey, byte[], byte[], byte[]). For GCM mode, the auth tag is part of the ciphertext (last 16 bytes) -- a tag mismatch raisesCryptoException. -
rsaEncrypt
Encrypts a small amount of data with RSA. The plaintext size is bounded by the modulus minus padding overhead (e.g. ~190 bytes max for RSA-2048 + OAEP-SHA-256); use AES with an RSA-wrapped AES key for larger payloads. -
rsaDecrypt
Decrypts an RSA ciphertext.
-