Android常用的数据加密方式代码详解
/**************************************************
* 1.什么是RSA 非对称加密?
*
* 2.
*************************************************/
private final static String RSA = "RSA";
//加密方式 RSA
public final static int DEFAULT_KEY_SIZE = 1024;
private final static int DECRYPT_LEN = DEFAULT_KEY_SIZE / 8;
//解密长度
private final static int ENCRYPT_LEN = DECRYPT_LEN - 11;
//加密长度
private static final String DES_CBC_PKCS5PAD = "DES/CBC/PKCS5Padding";
//加密填充方式
private final static int MODE_PRIVATE = 1;
//私钥加密
private final static int MODE_PUBLIC = 2;
//公钥加密
/**
* 随机生成RSA密钥对,包括PublicKey,PrivateKey
*
* @param keyLength 秘钥长度,范围是 512~2048,一般是1024
* @return KeyPair
*/
public static KeyPair generateRSAKeyPair(int keyLength) {
try {
KeyPairGenerator kpg = KeyPairGenerator.getInstance("RSA");
kpg.initialize(keyLength);
return kpg.genKeyPair();
}
catch (NoSuchAlgorithmException e) {
e.printStackTrace();
return null;
}
}
/**
* 得到私钥
*
* @return PrivateKey
* @throws NoSuchAlgorithmException
* @throws InvalidKeySpecException
*/
public static PrivateKey getPrivateKey(String key) throws NoSuchAlgorithmException, InvalidKeySpecException, NoSuchProviderException {
byte[] privateKey = Base64.decode(key, Base64.URL_SAFE);
PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(privateKey);
KeyFactory kf = KeyFactory.getInstance(RSA);
return kf.generatePrivate(keySpec);
}
/**
* 得到公钥
*
* @param key
* @return PublicKey
* @throws NoSuchAlgorithmException
* @throws InvalidKeySpecException
*/
public static PublicKey getPublicKey(String key) throws NoSuchAlgorithmException, InvalidKeySpecException, NoSuchProviderException {
byte[] publicKey = Base64.decode(key, Base64.URL_SAFE);
X509EncodedKeySpec keySpec = new X509EncodedKeySpec(publicKey);
KeyFactory kf = KeyFactory.getInstance(RSA);
return kf.generatePublic(keySpec);
}
/**
* 私钥加密
*
* @param data
* @param key
* @return
* @throws Exception
*/
public static byte[] encryptByRSA(byte[] data, Key key) throws Exception {
// 数据加密
Cipher cipher = Cipher.getInstance(RSA);
cipher.init(Cipher.ENCRYPT_MODE, key);
return cipher.doFinal(data);
}
/**
* 公钥解密
*
* @param data 待解密数据
* @param key 密钥
* @return byte[] 解密数据
*/
public static byte[] decryptByRSA(byte[] data, Key key) throws Exception {
// 数据解密
Cipher cipher = Cipher.getInstance(RSA);
cipher.init(Cipher.DECRYPT_MODE, key);
return cipher.doFinal(data);
}
public static void encryptByRSA(String source, String dest, Key key) {
rasEncrypt(MODE_ENCRYPTION, source, dest, key);
}
public static void decryptByRSA(String source, String dest, Key key) {
rasEncrypt(MODE_DECRYPTION, source, dest, key);
}
public static void rasEncrypt(int mode, String source, String dest, Key key) {
Log.i(TAG, "start===encryptByRSA mode--->>" + mode);
FileInputStream fis = null;
FileOutputStream fos = null;
try {
fis = new FileInputStream(source);
fos = new FileOutputStream(dest);
int size = mode == MODE_ENCRYPTION ? ENCRYPT_LEN : DECRYPT_LEN;
byte buff[] = new byte[size];
byte buffResult[];
while ((fis.read(buff)) != -1) {
buffResult = mode == MODE_ENCRYPTION ? encryptByRSA(buff, key) : decryptByRSA(buff, key);
if (buffResult != null) {
fos.write(buffResult);
}
}
Log.i(TAG, "end===encryptByRSA");
}
catch (IOException e) {
e.printStackTrace();
Log.e(TAG, "encryptByRSA failed err: " + e.getMessage());
}
catch (Exception e) {
e.printStackTrace();
}
finally {
try {
if (fis != null) {
fis.close();
}
if (fos != null) {
fos.close();
}
}
catch (IOException e) {
e.printStackTrace();
}
}
}
- .NET Core系列之MemoryCache 初识
- 007手机一键Root(安机网一键Root) v3.0 官方最新版 一键ROOT您的Android手机
- 12306密码被盗了怎么办?12306密码外泄解决方法
- 12个字的qq网名
- 150M迷你型无线路由器怎么设置?
- 192.168.1.1打不开怎么办?路由器192.168.1.1打不开的原因以及解决办法
- 2011年电子报合订本 电子报 编辑部 中文 PDF版 [84M]
- 2015年1月15日小米新旗舰发布会现场图文直播
- 2016.3.1vivo Xplay5新品发布会现场视频直播 优酷直播
- 2016华为P9发布会视频直播地址 4月15日华为P9国行发布会直播