/**
* 亦或加解密,适合对整个文件的部分加密,比如文件头部,和尾部
* 对file文件头部和尾部加密,适合zip压缩包加密
*
* @param source 需要加密的文件
* @param det 加密后保存文件名
* @param key 加密key
*/
public static void encryptionFile(File source, File det, int key) {
FileInputStream fis = null;
FileOutputStream fos = null;
try {
fis = new FileInputStream(source);
fos = new FileOutputStream(det);
int size = 2048;
byte buff[] = new byte[size];
int count = fis.read(buff);
/**zip包头部加密*/
for (int i = 0; i < count; i++) {
fos.write(buff[i] ^ key);
}
while (true) {
count = fis.read(buff);
/**zip包结尾加密*/
if (count < size) {
for (int j = 0; j < count; j++) {
fos.write(buff[j] ^ key);
}
break;
}
fos.write(buff, 0, count);
}
fos.flush();
}
catch (IOException e) {
e.printStackTrace();
}
finally {
try {
if (fis != null) {
fis.close();
}
if (fos != null) {
fos.close();
}
}
catch (IOException e) {
e.printStackTrace();
}
}
}
/**
* 亦或加解密,适合对整个文件加密
*
* @param source 需要加密文件的路径
* @param det 加密后保存文件的路径
* @param key 加密秘钥key
*/
private static void encryptionFile(String source, String det, int key) {
FileInputStream fis = null;
FileOutputStream fos = null;
try {
fis = new FileInputStream(source);
fos = new FileOutputStream(det);
int read;
while ((read = fis.read()) != -1) {
fos.write(read ^ key);
}
fos.flush();
}
catch (IOException e) {
e.printStackTrace();
}
finally {
try {
if (fis != null) {
fis.close();
}
if (fos != null) {
fos.close();
}
}
catch (IOException e) {
e.printStackTrace();
}
}
}
private static final String TAG = "EncryptUtils";
private final static int MODE_ENCRYPTION = 1;
private final static int MODE_DECRYPTION = 2;
private final static String AES_KEY = "xjp_12345!^-=42#";
//AES 秘钥key,必须为16位
private static byte[] encryption(int mode, byte[] content, String pwd) {
try {
Cipher cipher = Cipher.getInstance("AES/CFB/NoPadding");
//AES加密模式,CFB 加密模式
SecretKeySpec keySpec = new SecretKeySpec(pwd.getBytes("UTF-8"), "AES");
//AES加密方式
IvParameterSpec ivSpec = new IvParameterSpec(pwd.getBytes("UTF-8"));
cipher.init(mode == MODE_ENCRYPTION ? Cipher.ENCRYPT_MODE : Cipher.DECRYPT_MODE, keySpec, ivSpec);
return cipher.doFinal(content);
}
catch (NoSuchAlgorithmException | NoSuchPaddingException |
InvalidKeyException | IllegalBlockSizeException |
BadPaddingException | InvalidAlgorithmParameterException e) {
e.printStackTrace();
Log.e(TAG, "encryption failed... err: " + e.getMessage());
}
catch (Exception e) {
e.printStackTrace();
Log.e(TAG, "encryption1 failed ...err: " + e.getMessage());
}
return null;
}
/**
* AES 加密
*
* @param source 需要加密的文件路径
* @param dest 加密后的文件路径
*/
public static void encryptByAES(String source, String dest) {
encryptByAES(MODE_ENCRYPTION, source, dest);
}
public static void encryptByAES(int mode, String source, String dest) {
Log.i(TAG, "start===encryptByAES");
FileInputStream fis = null;
FileOutputStream fos = null;
try {
fis = new FileInputStream(source);
fos = new FileOutputStream(dest);
int size = 2048;
byte buff[] = new byte[size];
byte buffResult[];
while ((fis.read(buff)) != -1) {
buffResult = encryption(mode, buff, AES_KEY);
if (buffResult != null) {
fos.write(buffResult);
}
}
Log.i(TAG, "end===encryptByAES");
}
catch (IOException e) {
e.printStackTrace();
Log.e(TAG, "encryptByAES failed err: " + e.getMessage());
}
finally {
try {
if (fis != null) {
fis.close();
}
if (fos != null) {
fos.close();
}
}
catch (IOException e) {
e.printStackTrace();
}
}
}
/**
* AES 解密
*
* @param source 需要解密的文件路径
* @param dest 解密后保存的文件路径
*/
public static void decryptByAES(String source, String dest) {
encryptByAES(MODE_DECRYPTION, source, dest);
}
/**************************************************
* 1.什么是RSA 非对称加密?
* <p>
* 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();
}
}
}
public String getMD5Code(String info) {
try {
MessageDigest md5 = MessageDigest.getInstance("MD5");
md5.update(info.getBytes("UTF-8"));
byte[] encryption = md5.digest();
StringBuffer strBuf = new StringBuffer();
for (int i = 0; i < encryption.length; i++) {
if (Integer.toHexString(0xff & encryption[i]).length() == 1) {
strBuf.append("0").append(
Integer.toHexString(0xff & encryption[i]));
} else {
strBuf.append(Integer.toHexString(0xff & encryption[i]));
}
}
return strBuf.toString();
}
catch (Exception e) {
// TODO: handle exception
return "";
}
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有