@@ -421,6 +421,61 @@ public void testDecryptManipulatedVersionFlag() {
421421 }
422422 }
423423
424+ @ Test
425+ public void testLegacyEncryptionAndDecryption () {
426+ // Set to API 16 (Legacy)
427+ setFinalStatic (Build .VERSION .class , "SDK_INT" , Build .VERSION_CODES .JELLY_BEAN );
428+
429+ String testData = "test data for legacy encryption" ;
430+ String encrypted = encryptor .encrypt (testData );
431+ String decrypted = encryptor .decrypt (encrypted );
432+
433+ assertEquals ("Legacy encryption/decryption should work on API 16" , testData , decrypted );
434+
435+ // Verify it's using legacy encryption
436+ byte [] encryptedBytes = Base64 .decode (encrypted , Base64 .NO_WRAP );
437+ assertEquals ("Should use legacy encryption flag" , 0 , encryptedBytes [0 ]);
438+
439+ // Test on API 18
440+ setFinalStatic (Build .VERSION .class , "SDK_INT" , Build .VERSION_CODES .JELLY_BEAN_MR2 );
441+ String decryptedOnApi18 = encryptor .decrypt (encrypted );
442+ assertEquals ("Legacy data should be decryptable on API 18" , testData , decryptedOnApi18 );
443+
444+ String encryptedOnApi18 = encryptor .encrypt (testData );
445+ String decryptedFromApi18 = encryptor .decrypt (encryptedOnApi18 );
446+ assertEquals ("API 18 encryption/decryption should work" , testData , decryptedFromApi18 );
447+
448+ // Verify API 18 also uses legacy encryption
449+ byte [] api18EncryptedBytes = Base64 .decode (encryptedOnApi18 , Base64 .NO_WRAP );
450+ assertEquals ("Should use legacy encryption flag on API 18" , 0 , api18EncryptedBytes [0 ]);
451+ }
452+
453+ @ Test
454+ public void testModernEncryptionAndDecryption () {
455+ String testData = "test data for modern encryption" ;
456+
457+ // Test on API 19 (First modern version)
458+ setFinalStatic (Build .VERSION .class , "SDK_INT" , Build .VERSION_CODES .KITKAT );
459+ String encryptedOnApi19 = encryptor .encrypt (testData );
460+ String decryptedOnApi19 = encryptor .decrypt (encryptedOnApi19 );
461+ assertEquals ("Modern encryption should work on API 19" , testData , decryptedOnApi19 );
462+
463+ byte [] api19EncryptedBytes = Base64 .decode (encryptedOnApi19 , Base64 .NO_WRAP );
464+ assertEquals ("Should use modern encryption flag on API 19" , 1 , api19EncryptedBytes [0 ]);
465+
466+ // Test on API 23
467+ setFinalStatic (Build .VERSION .class , "SDK_INT" , Build .VERSION_CODES .M );
468+ String decryptedOnApi23 = encryptor .decrypt (encryptedOnApi19 );
469+ assertEquals ("API 19 data should be decryptable on API 23" , testData , decryptedOnApi23 );
470+
471+ String encryptedOnApi23 = encryptor .encrypt (testData );
472+ String decryptedFromApi23 = encryptor .decrypt (encryptedOnApi23 );
473+ assertEquals ("API 23 encryption/decryption should work" , testData , decryptedFromApi23 );
474+
475+ byte [] api23EncryptedBytes = Base64 .decode (encryptedOnApi23 , Base64 .NO_WRAP );
476+ assertEquals ("Should use modern encryption flag on API 23" , 1 , api23EncryptedBytes [0 ]);
477+ }
478+
424479 private static void setFinalStatic (Class <?> clazz , String fieldName , Object newValue ) {
425480 try {
426481 Field field = clazz .getDeclaredField (fieldName );
0 commit comments