EncryptHelper
EncryptHelper copied to clipboard
Android data localized safely encryption/decryption tool. 数据安全本地化加密解密工具。
EncryptHelper
Android data localized safely encryption/decryption tool. 数据安全本地化加密解密工具。
Gradle
root project:build.gradle
allprojects {
repositories {
...
maven { url "https://jitpack.io" }
}
}
app:build.gradle
dependencies {
// e.g. compile 'com.github.woxingxiao:EncryptHelper:1.2'
compile 'com.github.woxingxiao:EncryptHelper:${LATEST_VERSION}'
}
Theory
AES(对称加密)的Key想要保存在KeyStore里,需要Api 23才被支持,但是RSA(非对称加密)不受限制(Api >= 18)。
因此使用RSA加密AES Key(对称加密密钥)保存到本地(本库保存在SharedPreferences
),需要时使用RSA解密AES Key,
最终使用解密后的AES Key来加密和解密。
过程:
- 使用KeyStore生成随机的RSA Key(非对称加密密钥,包含公钥和私钥,Alias本库使用应用的包名);
- 生成AES Key,并用RSA PublicKey(公钥)加密后存入
SharedPreferences
; - 从
SharedPreferences
取出AES Key,并用RSA PrivateKey(私钥)解密AES Key,使用解密后的AES Key来加密和解密。
Usage (Api >= 18)
EncryptHelper mEncryptHelper = new EncryptHelper(getApplicationContext());
mEncryptHelper.encrypt(plainText);
mEncryptHelper.decrypt(encryptedText);
Thanks
License
Copyright 2017 woxingxiao
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.