diff --git a/lib_base/src/main/java/com/android/base/utils/security/AESCipherStrategy.java b/lib_base/src/main/java/com/android/base/utils/security/AESCipherStrategy.java
index 9cfa9c7..f9ce0a5 100644
--- a/lib_base/src/main/java/com/android/base/utils/security/AESCipherStrategy.java
+++ b/lib_base/src/main/java/com/android/base/utils/security/AESCipherStrategy.java
@@ -1,7 +1,5 @@
package com.android.base.utils.security;
-import com.android.base.utils.security.util.AESUtils;
-
import java.io.UnsupportedEncodingException;
/**
@@ -28,15 +26,15 @@ public class AESCipherStrategy extends CipherStrategy {
@Override
public String decrypt(String encryptContent) {
- byte[] encrypByte = decodeConvert(encryptContent);
- byte[] decryptByte = AESUtils.decryptData(encrypByte, key);
+ byte[] encryptByte = decodeConvert(encryptContent);
+ byte[] decryptByte = AESUtils.decryptData(encryptByte, key);
String result = "";
try {
result = new String(decryptByte, CHARSET);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
-
return result;
}
-}
+
+}
\ No newline at end of file
diff --git a/lib_base/src/main/java/com/android/base/utils/security/util/AESUtils.java b/lib_base/src/main/java/com/android/base/utils/security/AESUtils.java
similarity index 79%
rename from lib_base/src/main/java/com/android/base/utils/security/util/AESUtils.java
rename to lib_base/src/main/java/com/android/base/utils/security/AESUtils.java
index c75275d..32fe918 100644
--- a/lib_base/src/main/java/com/android/base/utils/security/util/AESUtils.java
+++ b/lib_base/src/main/java/com/android/base/utils/security/AESUtils.java
@@ -14,9 +14,9 @@
* limitations under the License.
*/
-package com.android.base.utils.security.util;
+package com.android.base.utils.security;
-import java.io.UnsupportedEncodingException;
+import java.nio.charset.StandardCharsets;
import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
@@ -25,10 +25,11 @@ import javax.crypto.spec.SecretKeySpec;
* AES 对称加密
*/
public class AESUtils {
+
/**
* 算法/模式/填充 *
*/
- private static final String CipherMode = "AES";
+ private static final String CIPHER_MODE = "AES";
/**
* 创建密钥
@@ -38,7 +39,7 @@ public class AESUtils {
* @return SecretKeySpec 实例
*/
private static SecretKeySpec generateAESKey(String password) {
- byte[] data = null;
+ byte[] data;
StringBuilder sb = new StringBuilder();
sb.append(password);
while (sb.length() < 16)
@@ -46,8 +47,8 @@ public class AESUtils {
if (sb.length() > 16)
sb.setLength(16);
try {
- data = sb.toString().getBytes("UTF-8");
- return new SecretKeySpec(data, "AES");
+ data = sb.toString().getBytes(StandardCharsets.UTF_8);
+ return new SecretKeySpec(data, CIPHER_MODE);
} catch (Exception e) {
e.printStackTrace();
return null;
@@ -64,10 +65,9 @@ public class AESUtils {
public static byte[] encryptData(byte[] content, String password) {
try {
SecretKeySpec key = generateAESKey(password);
- Cipher cipher = Cipher.getInstance(CipherMode);
+ Cipher cipher = Cipher.getInstance(CIPHER_MODE);
cipher.init(Cipher.ENCRYPT_MODE, key);
- byte[] result = cipher.doFinal(content);
- return result;
+ return cipher.doFinal(content);
} catch (Exception e) {
e.printStackTrace();
}
@@ -84,13 +84,12 @@ public class AESUtils {
public static String encryptData(String content, String password) {
byte[] data = null;
try {
- data = content.getBytes("UTF-8");
+ data = content.getBytes(StandardCharsets.UTF_8);
} catch (Exception e) {
e.printStackTrace();
}
data = encryptData(data, password);
- String result = byte2hex(data);
- return result;
+ return byte2hex(data);
}
/**
@@ -103,7 +102,7 @@ public class AESUtils {
public static byte[] decryptData(byte[] content, String password) {
try {
SecretKeySpec key = generateAESKey(password);
- Cipher cipher = Cipher.getInstance(CipherMode);
+ Cipher cipher = Cipher.getInstance(CIPHER_MODE);
cipher.init(Cipher.DECRYPT_MODE, key);
return cipher.doFinal(content);
} catch (Exception e) {
@@ -126,26 +125,21 @@ public class AESUtils {
if (data == null)
return null;
String result = null;
- try {
- result = new String(data, "UTF-8");
- } catch (UnsupportedEncodingException e) {
- e.printStackTrace();
- }
+ result = new String(data, StandardCharsets.UTF_8);
return result;
}
/**
* 字节数组转成16进制字符串
*
- * @param b
* @return 16进制字符串
*/
- public static String byte2hex(byte[] b) { // 一个字节的数,
- StringBuffer sb = new StringBuffer(b.length * 2);
- String tmp = "";
- for (int n = 0; n < b.length; n++) {
+ public static String byte2hex(byte[] bytes) { // 一个字节的数,
+ StringBuilder sb = new StringBuilder(bytes.length * 2);
+ String tmp;
+ for (byte theByte : bytes) {
// 整数转成十六进制表示
- tmp = (Integer.toHexString(b[n] & 0XFF));
+ tmp = (Integer.toHexString(theByte & 0XFF));
if (tmp.length() == 1) {
sb.append("0");
}
@@ -155,7 +149,7 @@ public class AESUtils {
}
/**
- * 将hex字符串转换成字节数组 *
+ * 将hex字符串转换成字节数组
*
* @param inputString 16进制的字符串
* @return 字节数组
diff --git a/lib_base/src/main/java/com/android/base/utils/security/DESCipherStrategy.java b/lib_base/src/main/java/com/android/base/utils/security/DESCipherStrategy.java
index 10e48a3..116ce4f 100644
--- a/lib_base/src/main/java/com/android/base/utils/security/DESCipherStrategy.java
+++ b/lib_base/src/main/java/com/android/base/utils/security/DESCipherStrategy.java
@@ -1,8 +1,6 @@
package com.android.base.utils.security;
-import com.android.base.utils.security.util.DESUtils;
-
import java.io.UnsupportedEncodingException;
/**
@@ -32,8 +30,8 @@ public class DESCipherStrategy extends CipherStrategy {
@Override
public String decrypt(String encryptContent) {
- byte[] encrypByte = decodeConvert(encryptContent);
- byte[] decryptByte = DESUtils.decrypt(encrypByte, key);
+ byte[] encryptByte = decodeConvert(encryptContent);
+ byte[] decryptByte = DESUtils.decrypt(encryptByte, key);
String result = "";
try {
result = new String(decryptByte, CHARSET);
diff --git a/lib_base/src/main/java/com/android/base/utils/security/util/DESUtils.java b/lib_base/src/main/java/com/android/base/utils/security/DESUtils.java
similarity index 86%
rename from lib_base/src/main/java/com/android/base/utils/security/util/DESUtils.java
rename to lib_base/src/main/java/com/android/base/utils/security/DESUtils.java
index dd06852..53334dd 100644
--- a/lib_base/src/main/java/com/android/base/utils/security/util/DESUtils.java
+++ b/lib_base/src/main/java/com/android/base/utils/security/DESUtils.java
@@ -14,7 +14,7 @@
* limitations under the License.
*/
-package com.android.base.utils.security.util;
+package com.android.base.utils.security;
import java.security.SecureRandom;
@@ -23,9 +23,7 @@ import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;
-/**
- * Created by Administrator on 2015/11/11.
- */
+@SuppressWarnings("WeakerAccess")
public class DESUtils {
/**
@@ -33,7 +31,6 @@ public class DESUtils {
*
* @param bytesContent 待加密内容
* @param key 加密的密钥
- * @return
*/
public static byte[] encrypt(byte[] bytesContent, String key) {
try {
@@ -43,8 +40,7 @@ public class DESUtils {
SecretKey securekey = keyFactory.generateSecret(desKey);
Cipher cipher = Cipher.getInstance("DES");
cipher.init(Cipher.ENCRYPT_MODE, securekey, random);
- byte[] result = cipher.doFinal(bytesContent);
- return result;
+ return cipher.doFinal(bytesContent);
} catch (Throwable e) {
e.printStackTrace();
}
@@ -56,20 +52,20 @@ public class DESUtils {
*
* @param content 待解密内容
* @param key 解密的密钥
- * @return
*/
public static byte[] decrypt(byte[] content, String key) {
try {
SecureRandom random = new SecureRandom();
DESKeySpec desKey = new DESKeySpec(key.getBytes());
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
- SecretKey securekey = keyFactory.generateSecret(desKey);
+ SecretKey secretKey = keyFactory.generateSecret(desKey);
Cipher cipher = Cipher.getInstance("DES");
- cipher.init(Cipher.DECRYPT_MODE, securekey, random);
+ cipher.init(Cipher.DECRYPT_MODE, secretKey, random);
return cipher.doFinal(content);
} catch (Throwable e) {
e.printStackTrace();
}
return null;
}
-}
+
+}
\ No newline at end of file
diff --git a/lib_base/src/main/java/com/android/base/utils/security/MD5Utils.java b/lib_base/src/main/java/com/android/base/utils/security/MD5Utils.java
new file mode 100644
index 0000000..074f138
--- /dev/null
+++ b/lib_base/src/main/java/com/android/base/utils/security/MD5Utils.java
@@ -0,0 +1,67 @@
+package com.android.base.utils.security;
+
+import java.io.FileInputStream;
+import java.io.InputStream;
+import java.nio.charset.StandardCharsets;
+import java.security.MessageDigest;
+import java.security.NoSuchAlgorithmException;
+
+public class MD5Utils {
+
+ private static final char[] HEX_DIGITS = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};
+
+ private static String toHexString(byte[] bytes) {
+ StringBuilder sb = new StringBuilder(bytes.length * 2);
+ for (byte b : bytes) {
+ sb.append(HEX_DIGITS[(b & 0xf0) >>> 4]);
+ sb.append(HEX_DIGITS[b & 0x0f]);
+ }
+ return sb.toString();
+ }
+
+ /**
+ * 文件加密
+ */
+ public static String md5file(String filename) {
+ InputStream fis;
+ byte[] buffer = new byte[1024];
+ int numRead = 0;
+ MessageDigest md5;
+ try {
+ fis = new FileInputStream(filename);
+ md5 = MessageDigest.getInstance("MD5");
+ while ((numRead = fis.read(buffer)) > 0) {
+ md5.update(buffer, 0, numRead);
+ }
+ fis.close();
+ return toHexString(md5.digest());
+ } catch (Exception e) {
+ System.out.println("error");
+ return null;
+ }
+ }
+
+ /**
+ * 字符串加密
+ */
+ public static String md5(String string) {
+ byte[] hash;
+ try {
+ hash = MessageDigest.getInstance("MD5").digest(string.getBytes(StandardCharsets.UTF_8));
+ } catch (NoSuchAlgorithmException e) {
+ throw new RuntimeException("Huh, MD5 should be supported?", e);
+ }
+
+ StringBuilder hex = new StringBuilder(hash.length * 2);
+
+ for (byte b : hash) {
+ if ((b & 0xFF) < 0x10) {
+ hex.append("0");
+ }
+ hex.append(Integer.toHexString(b & 0xFF));
+ }
+
+ return hex.toString();
+ }
+
+}
\ No newline at end of file
diff --git a/lib_base/src/main/java/com/android/base/utils/security/RSACipherStrategy.java b/lib_base/src/main/java/com/android/base/utils/security/RSACipherStrategy.java
index 99490b6..50b6ca2 100644
--- a/lib_base/src/main/java/com/android/base/utils/security/RSACipherStrategy.java
+++ b/lib_base/src/main/java/com/android/base/utils/security/RSACipherStrategy.java
@@ -1,7 +1,5 @@
package com.android.base.utils.security;
-import com.android.base.utils.security.util.RSAUtils;
-
import java.io.InputStream;
import java.security.PrivateKey;
import java.security.PublicKey;
@@ -67,4 +65,4 @@ public class RSACipherStrategy extends CipherStrategy {
return new String(decryptByte);
}
-}
+}
\ No newline at end of file
diff --git a/lib_base/src/main/java/com/android/base/utils/security/util/RSAUtils.java b/lib_base/src/main/java/com/android/base/utils/security/RSAUtils.java
similarity index 85%
rename from lib_base/src/main/java/com/android/base/utils/security/util/RSAUtils.java
rename to lib_base/src/main/java/com/android/base/utils/security/RSAUtils.java
index 8b72374..f3b081b 100644
--- a/lib_base/src/main/java/com/android/base/utils/security/util/RSAUtils.java
+++ b/lib_base/src/main/java/com/android/base/utils/security/RSAUtils.java
@@ -14,7 +14,7 @@
* limitations under the License.
*/
-package com.android.base.utils.security.util;
+package com.android.base.utils.security;
import android.util.Base64;
@@ -39,16 +39,14 @@ import javax.crypto.Cipher;
/**
* @author Mr.Zheng
- * @date 2014年8月22日 下午1:44:23
*/
public final class RSAUtils {
+
private final static String KEY_PAIR = "RSA";
private final static String CIPHER = "RSA/ECB/PKCS1Padding";
/**
* 随机生成RSA密钥对(默认密钥长度为1024)
- *
- * @return
*/
public static KeyPair generateRSAKeyPair() {
return generateRSAKeyPair(1024);
@@ -57,9 +55,7 @@ public final class RSAUtils {
/**
* 随机生成RSA密钥对
*
- * @param keyLength 密钥长度,范围:512~2048
- * 一般1024
- * @return
+ * @param keyLength 密钥长度,范围:512~2048,一般1024
*/
public static KeyPair generateRSAKeyPair(int keyLength) {
try {
@@ -98,7 +94,6 @@ public final class RSAUtils {
*
* @param encryptedData 经过encryptedData()加密返回的byte数据
* @param privateKey 私钥
- * @return
*/
public static byte[] decryptData(byte[] encryptedData, PrivateKey privateKey) {
try {
@@ -113,8 +108,6 @@ public final class RSAUtils {
/**
* 通过公钥byte[](publicKey.getEncoded())将公钥还原,适用于RSA算法
*
- * @param keyBytes
- * @return
* @throws NoSuchAlgorithmException
* @throws InvalidKeySpecException
*/
@@ -122,15 +115,12 @@ public final class RSAUtils {
InvalidKeySpecException {
X509EncodedKeySpec keySpec = new X509EncodedKeySpec(keyBytes);
KeyFactory keyFactory = KeyFactory.getInstance(KEY_PAIR);
- PublicKey publicKey = keyFactory.generatePublic(keySpec);
- return publicKey;
+ return keyFactory.generatePublic(keySpec);
}
/**
* 通过私钥byte[]将公钥还原,适用于RSA算法
*
- * @param keyBytes
- * @return
* @throws NoSuchAlgorithmException
* @throws InvalidKeySpecException
*/
@@ -138,16 +128,12 @@ public final class RSAUtils {
InvalidKeySpecException {
PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(keyBytes);
KeyFactory keyFactory = KeyFactory.getInstance(KEY_PAIR);
- PrivateKey privateKey = keyFactory.generatePrivate(keySpec);
- return privateKey;
+ return keyFactory.generatePrivate(keySpec);
}
/**
* 使用N、e值还原公钥
*
- * @param modulus
- * @param publicExponent
- * @return
* @throws NoSuchAlgorithmException
* @throws InvalidKeySpecException
*/
@@ -157,27 +143,21 @@ public final class RSAUtils {
BigInteger bigIntPrivateExponent = new BigInteger(publicExponent);
RSAPublicKeySpec keySpec = new RSAPublicKeySpec(bigIntModulus, bigIntPrivateExponent);
KeyFactory keyFactory = KeyFactory.getInstance(KEY_PAIR);
- PublicKey publicKey = keyFactory.generatePublic(keySpec);
- return publicKey;
+ return keyFactory.generatePublic(keySpec);
}
/**
* 使用N、d值还原私钥
*
- * @param modulus
- * @param privateExponent
- * @return
* @throws NoSuchAlgorithmException
* @throws InvalidKeySpecException
*/
- public static PrivateKey getPrivateKey(String modulus, String privateExponent)
- throws NoSuchAlgorithmException, InvalidKeySpecException {
+ public static PrivateKey getPrivateKey(String modulus, String privateExponent) throws NoSuchAlgorithmException, InvalidKeySpecException {
BigInteger bigIntModulus = new BigInteger(modulus);
BigInteger bigIntPrivateExponent = new BigInteger(privateExponent);
RSAPublicKeySpec keySpec = new RSAPublicKeySpec(bigIntModulus, bigIntPrivateExponent);
KeyFactory keyFactory = KeyFactory.getInstance(KEY_PAIR);
- PrivateKey privateKey = keyFactory.generatePrivate(keySpec);
- return privateKey;
+ return keyFactory.generatePrivate(keySpec);
}
/**
@@ -203,12 +183,7 @@ public final class RSAUtils {
}
/**
- * 从字符串中加载私钥
- * 加载时使用的是PKCS8EncodedKeySpec(PKCS#8编码的Key指令)。
- *
- * @param privateKeyStr
- * @return
- * @throws Exception
+ * 从字符串中加载私钥,加载时使用的是PKCS8EncodedKeySpec(PKCS#8编码的Key指令)。
*/
public static PrivateKey loadPrivateKey(String privateKeyStr) throws Exception {
try {
@@ -244,10 +219,6 @@ public final class RSAUtils {
/**
* 从文件中加载私钥
- *
- * @param
- * @return 是否成功
- * @throws Exception
*/
public static PrivateKey loadPrivateKey(InputStream in) throws Exception {
try {
@@ -261,28 +232,25 @@ public final class RSAUtils {
/**
* 读取密钥信息
+ *
+ *
* -------------------- * CONTENT * -------------------- - * - * @param in - * @return - * @throws IOException + **/ private static String readKey(InputStream in) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(in)); - String readLine = null; + String readLine; StringBuilder sb = new StringBuilder(); while ((readLine = br.readLine()) != null) { if (readLine.charAt(0) == '-') { continue; - } else { - sb.append(readLine); - sb.append('\r'); } + sb.append(readLine); + sb.append('\r'); } - return sb.toString(); } -} +} \ No newline at end of file diff --git a/lib_base/src/main/java/com/android/base/utils/security/SHAUtils.java b/lib_base/src/main/java/com/android/base/utils/security/SHAUtils.java new file mode 100644 index 0000000..b8622ec --- /dev/null +++ b/lib_base/src/main/java/com/android/base/utils/security/SHAUtils.java @@ -0,0 +1,32 @@ +package com.android.base.utils.security; + +import java.nio.charset.StandardCharsets; +import java.security.MessageDigest; +import java.security.NoSuchAlgorithmException; + +/** + * @author Ztiany + * Email: ztiany3@gmail.com + * Date : 2019-08-15 14:50 + */ +@SuppressWarnings("WeakerAccess") +public class SHAUtils { + + private static final String SHA256 = "SHA256"; + + public static byte[] toSHA256(String content) { + return toSHA256(content.getBytes(StandardCharsets.UTF_8)); + } + + public static byte[] toSHA256(byte[] bytes) { + try { + MessageDigest md = MessageDigest.getInstance(SHA256); + md.update(bytes); + return md.digest(); + } catch (NoSuchAlgorithmException e) { + e.printStackTrace(); + } + return null; + } + +} diff --git a/lib_base/src/main/java/com/android/base/utils/security/SimpleRsa.java b/lib_base/src/main/java/com/android/base/utils/security/SimpleRsa.java index c8c43c1..3a95a0d 100644 --- a/lib_base/src/main/java/com/android/base/utils/security/SimpleRsa.java +++ b/lib_base/src/main/java/com/android/base/utils/security/SimpleRsa.java @@ -5,6 +5,7 @@ import android.util.Base64; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.InputStream; +import java.nio.charset.StandardCharsets; import java.security.KeyFactory; import java.security.NoSuchAlgorithmException; import java.security.NoSuchProviderException; @@ -45,7 +46,7 @@ public class SimpleRsa { PublicKey pubKey = getPublicKeyFromX509(ALGORITHM, RSA_PUBLIC_KEY); Cipher cipher = Cipher.getInstance(TRANSFORMATION); cipher.init(Cipher.ENCRYPT_MODE, pubKey); - byte plaintext[] = content.getBytes("UTF-8"); + byte[] plaintext = content.getBytes(StandardCharsets.UTF_8); byte[] output = cipher.doFinal(plaintext); return new String(Base64.encode(output, Base64.DEFAULT)); } catch (Exception e) { @@ -78,7 +79,7 @@ public class SimpleRsa { } writer.write(cipher.doFinal(block)); } - return new String(writer.toByteArray(), "utf-8"); + return new String(writer.toByteArray(), StandardCharsets.UTF_8); } catch (Exception e) { return null; } diff --git a/lib_base/src/main/java/com/android/base/utils/security/util/MD5Util.java b/lib_base/src/main/java/com/android/base/utils/security/util/MD5Util.java deleted file mode 100644 index fad4e41..0000000 --- a/lib_base/src/main/java/com/android/base/utils/security/util/MD5Util.java +++ /dev/null @@ -1,113 +0,0 @@ -package com.android.base.utils.security.util; - -import java.security.MessageDigest; -import java.security.NoSuchAlgorithmException; - -public class MD5Util { - - private final static String[] strDigits = {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C", "D", "E", "F"}; - - private static String byteToArrayString(byte bByte) { - int iRet = bByte; - if (iRet < 0) { - iRet += 256; - } - int iD1 = iRet / 16; - int iD2 = iRet % 16; - return strDigits[iD1] + strDigits[iD2]; - } - - private static String byteToString(byte[] bByte) { - StringBuilder sBuffer = new StringBuilder(); - for (byte aBByte : bByte) { - sBuffer.append(byteToArrayString(aBByte)); - } - return sBuffer.toString(); - } - - /** - * 32 位 MD5加密 - * - * @param str 待加密的字符串 - * @return result - */ - public static String encrypt(String str) { - String result = null; - try { - result = str; - MessageDigest md = MessageDigest.getInstance("MD5"); - result = byteToString(md.digest(str.getBytes())); - } catch (NoSuchAlgorithmException ex) { - ex.printStackTrace(); - } - return result; - } - - - public static String encrypt(String algorithm, String str) { - try { - MessageDigest md = MessageDigest.getInstance(algorithm); - md.update(str.getBytes()); - StringBuilder sb = new StringBuilder(); - byte[] bytes = md.digest(); - for (int i = 0; i < bytes.length; i++) { - int b = bytes[i] & 0xFF; - if (b < 0x10) { - sb.append('0'); - } - sb.append(Integer.toHexString(b)); - } - return sb.toString(); - } catch (Exception e) { - return ""; - } - } - - public static String hashKeyForDisk(String key) { - String cacheKey; - try { - final MessageDigest mDigest = MessageDigest.getInstance("MD5"); - mDigest.update(key.getBytes()); - cacheKey = bytesToHexString(mDigest.digest()); - } catch (NoSuchAlgorithmException e) { - cacheKey = String.valueOf(key.hashCode()); - } - return cacheKey; - } - - private static String bytesToHexString(byte[] bytes) { - StringBuilder sb = new StringBuilder(); - for (int i = 0; i < bytes.length; i++) { - String hex = Integer.toHexString(0xFF & bytes[i]); - if (hex.length() == 1) { - sb.append('0'); - } - sb.append(hex); - } - return sb.toString(); - } - - - public static String getMd5Value(String secret) { - try { - MessageDigest bmd5 = MessageDigest.getInstance("MD5"); - bmd5.update(secret.getBytes()); - int i; - StringBuffer buf = new StringBuffer(); - byte[] b = bmd5.digest(); - for (int offset = 0; offset < b.length; offset++) { - i = b[offset]; - if (i < 0) - i += 256; - if (i < 16) - buf.append("0"); - buf.append(Integer.toHexString(i)); - } - return buf.toString(); - } catch (NoSuchAlgorithmException e) { - e.printStackTrace(); - } - return ""; - } - -}