encryption_interceptor 1.0.0
encryption_interceptor: ^1.0.0 copied to clipboard
A Dio interceptor that encrypts requests and decrypts responses using AES encryption, ensuring secure data transmission.
EncryptionInterceptor #
encryption_interceptor
is a Dio interceptor that automatically encrypts outgoing requests and decrypts incoming responses using AES encryption. This ensures secure communication between the client and the server, even without HTTPS.
π Features #
β
AES Encryption (256-bit) β Protects request and response data
β
Automatic Request Encryption β Encrypts request bodies before sending
β
Automatic Response Decryption β Decrypts responses before passing to the app
β
Optional GET Request Encryption β Choose whether to encrypt GET requests
β
Skip Encryption for Specific Requests β Use a custom header (skip-encryption
)
β
Lightweight & Easy to Use β Plug-and-play with Dio
π¦ Installation #
Add the package to your pubspec.yaml
:
dependencies:
encryption_interceptor: ^1.0.0
Or install via CLI:
flutter pub add encryption_interceptor
π§ Usage #
1οΈβ£ Add the Interceptor to Dio #
import 'package:dio/dio.dart';
import 'package:encryption_interceptor/encryption_interceptor.dart';
void main() {
final dio = Dio();
// Add the interceptor with encryption enabled for all requests
dio.interceptors.add(EncryptionInterceptor("my_super_secret_key"));
// Making an encrypted request
dio.post("https://api.example.com/login", data: {
"username": "test",
"password": "securepassword"
});
}
2οΈβ£ Enabling GET Request Encryption #
By default, GET requests are not encrypted because they typically donβt have a body.
If your API requires encrypted GET requests, enable it:
dio.interceptors.add(EncryptionInterceptor("my_secret_key", enableGetEncryption: true));
3οΈβ£ Skipping Encryption for Specific Requests #
If you need to send a plain request without encryption, use the "skip-encryption"
header:
dio.post(
"https://api.example.com/data",
data: {"public_info": "This should not be encrypted"},
options: Options(headers: {"skip-encryption": true}),
);
π How It Works #
1οΈβ£ Before Sending a Request
- The request body is encrypted using AES 256-bit encryption
- The encrypted data is sent as:
{ "payload": "ENCRYPTED_DATA_HERE" }
2οΈβ£ When Receiving a Response
- The interceptor automatically decrypts the response body
- The decrypted JSON is returned to the app in its original format
π‘ Security Notes #
- AES encryption ensures high-level security, but it is always recommended to use HTTPS alongside it.
- Do not expose the secret key in your frontend applications.
- Ensure your server can decrypt AES-encoded data before implementing this interceptor.