提交 b45808d3 编写于 作者: B Blankj

see 04/22 log

上级 84d515f2
......@@ -109,6 +109,7 @@ public class SpanActivity extends BaseBackActivity {
initAnimSpan();
startAnim();
tvAboutSpan.setText(new SpanUtils()
.appendLine("SpanUtils").setBackgroundColor(Color.LTGRAY).setBold().setForegroundColor(Color.YELLOW).setAlign(Layout.Alignment.ALIGN_CENTER)
.appendLine("前景色").setForegroundColor(Color.GREEN)
......
......@@ -202,6 +202,8 @@ encrypt3DES, encrypt3DES2HexString, encrypt3DES2Base64: 3DES 加密
decrypt3DES, decryptHexString3DES, decryptBase64_3DES : 3DES 解密
encryptAES, encryptAES2HexString, encryptAES2Base64 : AES 加密
decryptAES, decryptHexStringAES, decryptBase64AES : AES 解密
encryptRSA, encryptRSA2HexString, encryptRSA2Base64 : RSA 加密
decryptRSA, decryptHexStringRSA, decryptBase64RSA : RSA 解密
```
* ### 文件相关 -> [FileIOUtils.java][fileio.java] -> [Test][fileio.test]
......@@ -654,7 +656,7 @@ showCustomLong : 显示长时自定义吐司
cancel : 取消吐司显示
```
* ### Uri 相关 -> [UriUtils.java][uri.java]
* ### URI 相关 -> [UriUtils.java][uri.java]
```
getUriForFile: 获取文件 URI
```
......
......@@ -7,12 +7,20 @@ import java.io.FileInputStream;
import java.io.IOException;
import java.security.DigestInputStream;
import java.security.InvalidKeyException;
import java.security.Key;
import java.security.KeyFactory;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.security.spec.AlgorithmParameterSpec;
import java.security.spec.InvalidKeySpecException;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;
import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.Mac;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
......@@ -925,6 +933,181 @@ public final class EncryptUtils {
}
}
///////////////////////////////////////////////////////////////////////////
// RSA encryption
///////////////////////////////////////////////////////////////////////////
/**
* Return the Base64-encode bytes of RSA encryption.
*
* @param data The data.
* @param key The key.
* @param isPublicKey True to use public key, false to use private key.
* @param transformation The name of the transformation, e.g., <i>RSA/CBC/PKCS1Padding</i>.
* @return the Base64-encode bytes of RSA encryption
*/
public static byte[] encryptRSA2Base64(final byte[] data,
final byte[] key,
final boolean isPublicKey,
final String transformation) {
return base64Encode(encryptRSA(data, key, isPublicKey, transformation));
}
/**
* Return the hex string of RSA encryption.
*
* @param data The data.
* @param key The key.
* @param isPublicKey True to use public key, false to use private key.
* @param transformation The name of the transformation, e.g., <i>RSA/CBC/PKCS1Padding</i>.
* @return the hex string of RSA encryption
*/
public static String encryptRSA2HexString(final byte[] data,
final byte[] key,
final boolean isPublicKey,
final String transformation) {
return bytes2HexString(encryptRSA(data, key, isPublicKey, transformation));
}
/**
* Return the bytes of RSA encryption.
*
* @param data The data.
* @param key The key.
* @param isPublicKey True to use public key, false to use private key.
* @param transformation The name of the transformation, e.g., <i>RSA/CBC/PKCS1Padding</i>.
* @return the bytes of RSA encryption
*/
public static byte[] encryptRSA(final byte[] data,
final byte[] key,
final boolean isPublicKey,
final String transformation) {
return rsaTemplate(data, key, isPublicKey, transformation, true);
}
/**
* Return the bytes of RSA decryption for Base64-encode bytes.
*
* @param data The data.
* @param key The key.
* @param isPublicKey True to use public key, false to use private key.
* @param transformation The name of the transformation, e.g., <i>RSA/CBC/PKCS1Padding</i>.
* @return the bytes of RSA decryption for Base64-encode bytes
*/
public static byte[] decryptBase64RSA(final byte[] data,
final byte[] key,
final boolean isPublicKey,
final String transformation) {
return decryptRSA(base64Decode(data), key, isPublicKey, transformation);
}
/**
* Return the bytes of RSA decryption for hex string.
*
* @param data The data.
* @param key The key.
* @param isPublicKey True to use public key, false to use private key.
* @param transformation The name of the transformation, e.g., <i>RSA/CBC/PKCS1Padding</i>.
* @return the bytes of RSA decryption for hex string
*/
public static byte[] decryptHexStringRSA(final String data,
final byte[] key,
final boolean isPublicKey,
final String transformation) {
return decryptRSA(hexString2Bytes(data), key, isPublicKey, transformation);
}
/**
* Return the bytes of RSA decryption.
*
* @param data The data.
* @param key The key.
* @param isPublicKey True to use public key, false to use private key.
* @param transformation The name of the transformation, e.g., <i>RSA/CBC/PKCS1Padding</i>.
* @return the bytes of RSA decryption
*/
public static byte[] decryptRSA(final byte[] data,
final byte[] key,
final boolean isPublicKey,
final String transformation) {
return rsaTemplate(data, key, isPublicKey, transformation, false);
}
/**
* Return the bytes of RSA encryption or decryption.
*
* @param data The data.
* @param key The key.
* @param isPublicKey True to use public key, false to use private key.
* @param transformation The name of the transformation, e.g., <i>DES/CBC/PKCS1Padding</i>.
* @param isEncrypt True to encrypt, false otherwise.
* @return the bytes of RSA encryption or decryption
*/
private static byte[] rsaTemplate(final byte[] data,
final byte[] key,
final boolean isPublicKey,
final String transformation,
final boolean isEncrypt) {
if (data == null || data.length == 0 || key == null || key.length == 0) {
return null;
}
try {
Key rsaKey;
if (isPublicKey) {
X509EncodedKeySpec keySpec = new X509EncodedKeySpec(key);
rsaKey = KeyFactory.getInstance("RSA").generatePublic(keySpec);
} else {
PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(key);
rsaKey = KeyFactory.getInstance("RSA").generatePrivate(keySpec);
}
if (rsaKey == null) return null;
Cipher cipher = Cipher.getInstance(transformation);
cipher.init(isEncrypt ? Cipher.ENCRYPT_MODE : Cipher.DECRYPT_MODE, rsaKey);
int len = data.length;
int maxLen = isEncrypt ? 117 : 128;
int count = len / maxLen;
if (count > 0) {
byte[] ret = new byte[0];
byte[] buff = new byte[maxLen];
int index = 0;
for (int i = 0; i < count; i++) {
System.arraycopy(data, index, buff, 0, maxLen);
ret = joins(ret, cipher.doFinal(buff));
index += maxLen;
}
if (index != len) {
int restLen = len - index;
buff = new byte[restLen];
System.arraycopy(data, index, buff, 0, restLen);
ret = joins(ret, cipher.doFinal(buff));
}
return ret;
} else {
return cipher.doFinal(data);
}
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (NoSuchPaddingException e) {
e.printStackTrace();
} catch (InvalidKeyException e) {
e.printStackTrace();
} catch (BadPaddingException e) {
e.printStackTrace();
} catch (IllegalBlockSizeException e) {
e.printStackTrace();
} catch (InvalidKeySpecException e) {
e.printStackTrace();
}
return null;
}
private static byte[] joins(final byte[] prefix, final byte[] suffix) {
byte[] ret = new byte[prefix.length + suffix.length];
System.arraycopy(prefix, 0, ret, 0, prefix.length);
System.arraycopy(suffix, 0, ret, prefix.length, suffix.length);
return ret;
}
private static final char HEX_DIGITS[] =
{'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};
......
......@@ -3,9 +3,11 @@ package com.blankj.utilcode.util;
import android.content.ComponentName;
import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.provider.MediaStore;
import android.support.annotation.RequiresPermission;
import android.support.v4.content.FileProvider;
import java.io.File;
......@@ -74,9 +76,15 @@ public final class IntentUtils {
public static Intent getInstallAppIntent(final File file, final boolean isNewTask) {
if (file == null) return null;
Intent intent = new Intent(Intent.ACTION_VIEW);
Uri data = UriUtils.getUriForFile(file);
intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
Uri data;
String type = "application/vnd.android.package-archive";
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.N) {
data = Uri.fromFile(file);
} else {
intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
String authority = Utils.getApp().getPackageName() + ".utilcode.provider";
data = FileProvider.getUriForFile(Utils.getApp(), authority, file);
}
intent.setDataAndType(data, type);
return getIntent(intent, isNewTask);
}
......@@ -146,8 +154,14 @@ public final class IntentUtils {
final boolean isNewTask) {
if (file == null) return null;
Intent intent = new Intent(Intent.ACTION_VIEW);
Uri data = UriUtils.getUriForFile(file);
Uri data;
String type = "application/vnd.android.package-archive";
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.N) {
data = Uri.fromFile(file);
} else {
intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
data = FileProvider.getUriForFile(Utils.getApp(), authority, file);
}
intent.setDataAndType(data, type);
return getIntent(intent, isNewTask);
}
......
......@@ -11,7 +11,7 @@ import java.io.File;
* author: Blankj
* blog : http://blankj.com
* time : 2018/04/20
* desc : uri 相关
* desc : URI 相关
* </pre>
*/
public final class UriUtils {
......@@ -20,6 +20,12 @@ public final class UriUtils {
throw new UnsupportedOperationException("u can't instantiate me...");
}
/**
* Return a content URI for a given file.
*
* @param file The file.
* @return a content URI for a given file
*/
public static Uri getUriForFile(final File file) {
if (file == null) return null;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
......
......@@ -67,6 +67,15 @@ public class EncryptUtilsTest {
);
}
@Test
public void encryptMD5File() throws Exception {
String fileMd5 = "7f138a09169b250e9dcb378140907378";
assertEquals(
fileMd5.toUpperCase(),
EncryptUtils.encryptMD5File2String(new File(PATH_ENCRYPT + "MD5.txt"))
);
}
@Test
public void encryptSHA1() throws Exception {
String blankjSHA1 = "C606ACCB1FEB669E19D080ADDDDBB8E6CDA5F43C";
......@@ -447,12 +456,6 @@ public class EncryptUtilsTest {
@Test
public void encryptAES() throws Exception {
// EncryptUtils.encryptAES(
// bytesDataAES,
// bytesKeyAES,
// "AES/ECB/NoPadding",
// null
// );
assertTrue(
Arrays.equals(
bytesResAES,
......@@ -518,12 +521,25 @@ public class EncryptUtilsTest {
);
}
private String publicKey = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCWuAuSCrzUXC1l4ixXBeBfotUtkALrAjLM5UHiVfOFHrRJHM41HSeHVm56UZHgJlwk80R8juu1ykuhkgrilTv7H+3MpZdIunvndDElgdgk8aI2Ip4GUlemUDvCtWd3ychWEh4kYQ8CeInQvNM08imoLFldvbjWt/IkGK+BcGzamQIDAQAB";
private String privateKey = "MIICdgIBADANBgkqhkiG9w0BAQEFAASCAmAwggJcAgEAAoGBAJa4C5IKvNRcLWXiLFcF4F+i1S2QAusCMszlQeJV84UetEkczjUdJ4dWbnpRkeAmXCTzRHyO67XKS6GSCuKVO/sf7cyll0i6e+d0MSWB2CTxojYingZSV6ZQO8K1Z3fJyFYSHiRhDwJ4idC80zTyKagsWV29uNa38iQYr4FwbNqZAgMBAAECgYAxV1k6W1eMMg0OsKeRabQVuwoNG3tJEnQtDdSu0zKg3vdohAyh6MR7EvmiA7g86HH8CsPd/y/9WJe/8j6sBO0Ye9gt7eyQ2NiwWvlTuwNmngcSTapVvVI6NEyJFMfQt9PB1EHLNAXlz8jtJUyA7C48jReQD9p/SzAP0VxG7lwyMQJBAOjE7hAZ/6fyP3DB1fG7jr9gONZcz3TUaqx6BUn4GKZnckW08ht9Xqcqft5Hthu8BbLM9ptQ0U8QZekrJwD6ya0CQQClwstZMPu8jLhsgugVwodcG1mPEOiw9Yjnmt9+WTI07Ll2uFv//hRXBnahBBnZbucUYEbUY3kqUX9b3e9TmEodAkEAybPMbxt4VDoxCy6Mi/pxChkBZ4/pHV3sSiU6bAyWn6vIc+sGWRfca5MBePA/N+1IKtY9Y/02QwL8rH5+P/URyQJAL/hdjORGFdzLimuf6pwvPBKWKncEQCHuisghIZmClBpl2duklELddAnkztg2+tvDd/wcw14+NGb9aoKhvhl2aQJAbvcgoPU+xs0CjeexH+TS2S/jKkTRpvP2CpPK/k71m13xWdE8RtMkYY1measRmlIwOfWze7ll/PGT4dxWf31FNg==";
private String dataRSA = "BlankjBlankjBlankjBlankjBlankjBlankjBlankjBlankjBlankjBlankjBlankjBlankjBlankjBlankjBlankjBlankjBlankjBlankjBlankjBla12345678";
@Test
public void encryptMD5File() throws Exception {
String fileMd5 = "7f138a09169b250e9dcb378140907378";
public void encryptRSA() throws Exception {
assertEquals(
fileMd5.toUpperCase(),
EncryptUtils.encryptMD5File2String(new File(PATH_ENCRYPT + "MD5.txt"))
EncryptUtils.decryptRSA(
EncryptUtils.encryptRSA(
dataRSA.getBytes(),
EncodeUtils.base64Decode(publicKey.getBytes()),
true,
"RSA/ECB/PKCS1Padding"
),
EncodeUtils.base64Decode(privateKey.getBytes()),
false,
"RSA/ECB/PKCS1Padding"
),
dataRSA.getBytes()
);
}
}
\ No newline at end of file
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册