From e2fca39777d8964c7d33e79ce7cf2dd87caf9d07 Mon Sep 17 00:00:00 2001
From: Forevery <1262198645@qq.com>
Date: Fri, 29 Sep 2017 21:12:01 +0800
Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=96=B0?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
.idea/gradle.xml | 2 +-
.idea/modules.xml | 2 +-
{encodeutils => EncryptUtils}/.gitignore | 2 +-
{encodeutils => EncryptUtils}/build.gradle | 62 ++--
.../proguard-rules.pro | 50 +--
.../src/main/AndroidManifest.xml | 20 +-
.../EncryptUtils}/Encode/AES/Aes_Util.java | 48 +--
.../EncryptUtils}/Encode/AES/CBC/CBC_Ut.java | 28 +-
.../CBC/PKCS5Padding/PKCS5Paddimg_Ulits.java | 252 +++++++--------
.../AES/CBC/PKCS5Padding/ToHexUtil.java | 90 +++---
.../EncryptUtils}/Encode/AES/ECB/ECB_Ut.java | 28 +-
.../AES/ECB/NoPadding/NoPadding_Utils.java | 180 +++++------
.../EncryptUtils}/Encode/DES/DES_Utils.java | 176 +++++------
.../Encode/HmacMD5/HmacMD5_Utils.java | 88 +++---
.../Encode/HmacSHA1/HmacSHA1_Utils.java | 90 +++---
.../Encode/HmacSHA224/HmacSHA224_Utils.java | 90 +++---
.../Encode/HmacSHA256/HmacSHA256_Utils.java | 90 +++---
.../Encode/HmacSHA384/HmacSHA384_Utils.java | 88 +++---
.../Encode/HmacSHA512/HmacSHA512_Utils.java | 88 +++---
.../EncryptUtils}/Encode/MD2/MD2_Utils.java | 80 ++---
.../EncryptUtils}/Encode/MD5/MD5_Utils.java | 296 ++++++++---------
.../EncryptUtils}/Encode/RC4/RC4_Utils.java | 282 ++++++++---------
.../EncryptUtils}/Encode/SHA1/SHA1_Utils.java | 86 ++---
.../Encode/SHA224/SHA224_Utils.java | 82 ++---
.../Encode/SHA256/SHA256_Utils.java | 86 ++---
.../Encode/SHA384/SHA384_Utils.java | 82 ++---
.../Encode/SHA512/SHA512_Utils.java | 82 ++---
.../Encode/T_3DES/T_3DES_Utils.java | 178 +++++------
.../forevery/EncryptUtils}/Encode/Utils.java | 280 ++++++++--------
.../forevery/EncryptUtils/EncryptUtils.java | 298 +++++++++---------
.../src/main/res/values/strings.xml | 6 +-
.../EncryptUtils}/ExampleUnitTest.java | 32 +-
app/build.gradle | 2 +-
settings.gradle | 2 +-
34 files changed, 1674 insertions(+), 1674 deletions(-)
rename {encodeutils => EncryptUtils}/.gitignore (87%)
rename {encodeutils => EncryptUtils}/build.gradle (96%)
rename {encodeutils => EncryptUtils}/proguard-rules.pro (97%)
rename {encodeutils => EncryptUtils}/src/main/AndroidManifest.xml (81%)
rename {encodeutils/src/main/java/org/forevery/EncodeUtils => EncryptUtils/src/main/java/org/forevery/EncryptUtils}/Encode/AES/Aes_Util.java (56%)
rename {encodeutils/src/main/java/org/forevery/EncodeUtils => EncryptUtils/src/main/java/org/forevery/EncryptUtils}/Encode/AES/CBC/CBC_Ut.java (53%)
rename {encodeutils/src/main/java/org/forevery/EncodeUtils => EncryptUtils/src/main/java/org/forevery/EncryptUtils}/Encode/AES/CBC/PKCS5Padding/PKCS5Paddimg_Ulits.java (94%)
rename {encodeutils/src/main/java/org/forevery/EncodeUtils => EncryptUtils/src/main/java/org/forevery/EncryptUtils}/Encode/AES/CBC/PKCS5Padding/ToHexUtil.java (91%)
rename {encodeutils/src/main/java/org/forevery/EncodeUtils => EncryptUtils/src/main/java/org/forevery/EncryptUtils}/Encode/AES/ECB/ECB_Ut.java (53%)
rename {encodeutils/src/main/java/org/forevery/EncodeUtils => EncryptUtils/src/main/java/org/forevery/EncryptUtils}/Encode/AES/ECB/NoPadding/NoPadding_Utils.java (82%)
rename {encodeutils/src/main/java/org/forevery/EncodeUtils => EncryptUtils/src/main/java/org/forevery/EncryptUtils}/Encode/DES/DES_Utils.java (82%)
rename {encodeutils/src/main/java/org/forevery/EncodeUtils => EncryptUtils/src/main/java/org/forevery/EncryptUtils}/Encode/HmacMD5/HmacMD5_Utils.java (79%)
rename {encodeutils/src/main/java/org/forevery/EncodeUtils => EncryptUtils/src/main/java/org/forevery/EncryptUtils}/Encode/HmacSHA1/HmacSHA1_Utils.java (79%)
rename {encodeutils/src/main/java/org/forevery/EncodeUtils => EncryptUtils/src/main/java/org/forevery/EncryptUtils}/Encode/HmacSHA224/HmacSHA224_Utils.java (79%)
rename {encodeutils/src/main/java/org/forevery/EncodeUtils => EncryptUtils/src/main/java/org/forevery/EncryptUtils}/Encode/HmacSHA256/HmacSHA256_Utils.java (79%)
rename {encodeutils/src/main/java/org/forevery/EncodeUtils => EncryptUtils/src/main/java/org/forevery/EncryptUtils}/Encode/HmacSHA384/HmacSHA384_Utils.java (79%)
rename {encodeutils/src/main/java/org/forevery/EncodeUtils => EncryptUtils/src/main/java/org/forevery/EncryptUtils}/Encode/HmacSHA512/HmacSHA512_Utils.java (79%)
rename {encodeutils/src/main/java/org/forevery/EncodeUtils => EncryptUtils/src/main/java/org/forevery/EncryptUtils}/Encode/MD2/MD2_Utils.java (76%)
rename {encodeutils/src/main/java/org/forevery/EncodeUtils => EncryptUtils/src/main/java/org/forevery/EncryptUtils}/Encode/MD5/MD5_Utils.java (92%)
rename {encodeutils/src/main/java/org/forevery/EncodeUtils => EncryptUtils/src/main/java/org/forevery/EncryptUtils}/Encode/RC4/RC4_Utils.java (95%)
rename {encodeutils/src/main/java/org/forevery/EncodeUtils => EncryptUtils/src/main/java/org/forevery/EncryptUtils}/Encode/SHA1/SHA1_Utils.java (75%)
rename {encodeutils/src/main/java/org/forevery/EncodeUtils => EncryptUtils/src/main/java/org/forevery/EncryptUtils}/Encode/SHA224/SHA224_Utils.java (76%)
rename {encodeutils/src/main/java/org/forevery/EncodeUtils => EncryptUtils/src/main/java/org/forevery/EncryptUtils}/Encode/SHA256/SHA256_Utils.java (76%)
rename {encodeutils/src/main/java/org/forevery/EncodeUtils => EncryptUtils/src/main/java/org/forevery/EncryptUtils}/Encode/SHA384/SHA384_Utils.java (76%)
rename {encodeutils/src/main/java/org/forevery/EncodeUtils => EncryptUtils/src/main/java/org/forevery/EncryptUtils}/Encode/SHA512/SHA512_Utils.java (76%)
rename {encodeutils/src/main/java/org/forevery/EncodeUtils => EncryptUtils/src/main/java/org/forevery/EncryptUtils}/Encode/T_3DES/T_3DES_Utils.java (82%)
rename {encodeutils/src/main/java/org/forevery/EncodeUtils => EncryptUtils/src/main/java/org/forevery/EncryptUtils}/Encode/Utils.java (96%)
rename encodeutils/src/main/java/org/forevery/EncodeUtils/EncodeUtils.java => EncryptUtils/src/main/java/org/forevery/EncryptUtils/EncryptUtils.java (63%)
rename {encodeutils => EncryptUtils}/src/main/res/values/strings.xml (96%)
rename {encodeutils/src/test/java/org/forevery/EncodeUtils => EncryptUtils/src/test/java/org/forevery/EncryptUtils}/ExampleUnitTest.java (96%)
diff --git a/.idea/gradle.xml b/.idea/gradle.xml
index 787bca5..fd16693 100644
--- a/.idea/gradle.xml
+++ b/.idea/gradle.xml
@@ -8,8 +8,8 @@
法算法名称/加密模式/填充方式
- *加密模式有:电子密码本模式ECB、加密块链模式CBC、加密反馈模式CFB、输出反馈模式OFB
- *填充方式有:NoPadding、ZerosPadding、PKCS5Padding
- */ - public String AES_Transformation = "AES/ECB/NoPadding"; - private final String AES_Algorithm = "AES"; - - - /** - * AES加密后转为Base64编码 - * - * @param data 明文 - * @param key 16、24、32字节秘钥 - * @return Base64密文 - */ - public byte[] Encrypt_Base64(final byte[] data, final byte[] key) { - return base64Encode(Decrypt_Byte(data, key)); - } - - /** - * AES加密后转为16进制 - * - * @param data 明文 - * @param key 16、24、32字节秘钥 - * @return 16进制密文 - */ - public String Encrypt(final byte[] data, final byte[] key) { - return bytes2HexString(Decrypt_Byte(data, key)); - } - - /** - * AES加密 - * - * @param data 明文 - * @param key 16、24、32字节秘钥 - * @return 密文 - */ - public byte[] Encrypt_Byte(final byte[] data, final byte[] key) { - return desTemplate(data, key, AES_Algorithm, AES_Transformation, true); - } - - /** - * AES解密Base64编码密文 - * - * @param data Base64编码密文 - * @param key 16、24、32字节秘钥 - * @return 明文 - */ - public byte[] Decrypt(final byte[] data, final byte[] key) { - return Decrypt_Byte(base64Decode(data), key); - } - - /** - * AES解密16进制密文 - * - * @param data 16进制密文 - * @param key 16、24、32字节秘钥 - * @return 明文 - */ - public byte[] Decrypt(final String data, final byte[] key) { - return Decrypt_Byte(hexString2Bytes(data), key); - } - - /** - * AES解密 - * - * @param data 密文 - * @param key 16、24、32字节秘钥 - * @return 明文 - */ - public byte[] Decrypt_Byte(final byte[] data, final byte[] key) { - return desTemplate(data, key, AES_Algorithm, AES_Transformation, false); - } - -} +package org.forevery.EncryptUtils.Encode.AES.ECB.NoPadding; + +import static org.forevery.EncryptUtils.Encode.Utils.base64Decode; +import static org.forevery.EncryptUtils.Encode.Utils.base64Encode; +import static org.forevery.EncryptUtils.Encode.Utils.bytes2HexString; +import static org.forevery.EncryptUtils.Encode.Utils.desTemplate; +import static org.forevery.EncryptUtils.Encode.Utils.hexString2Bytes; + +/** + * Created by Forevery on 2017/9/29. + */ + +public class NoPadding_Utils { + /** + * AES转变 + *法算法名称/加密模式/填充方式
+ *加密模式有:电子密码本模式ECB、加密块链模式CBC、加密反馈模式CFB、输出反馈模式OFB
+ *填充方式有:NoPadding、ZerosPadding、PKCS5Padding
+ */ + public String AES_Transformation = "AES/ECB/NoPadding"; + private final String AES_Algorithm = "AES"; + + + /** + * AES加密后转为Base64编码 + * + * @param data 明文 + * @param key 16、24、32字节秘钥 + * @return Base64密文 + */ + public byte[] Encrypt_Base64(final byte[] data, final byte[] key) { + return base64Encode(Decrypt_Byte(data, key)); + } + + /** + * AES加密后转为16进制 + * + * @param data 明文 + * @param key 16、24、32字节秘钥 + * @return 16进制密文 + */ + public String Encrypt(final byte[] data, final byte[] key) { + return bytes2HexString(Decrypt_Byte(data, key)); + } + + /** + * AES加密 + * + * @param data 明文 + * @param key 16、24、32字节秘钥 + * @return 密文 + */ + public byte[] Encrypt_Byte(final byte[] data, final byte[] key) { + return desTemplate(data, key, AES_Algorithm, AES_Transformation, true); + } + + /** + * AES解密Base64编码密文 + * + * @param data Base64编码密文 + * @param key 16、24、32字节秘钥 + * @return 明文 + */ + public byte[] Decrypt(final byte[] data, final byte[] key) { + return Decrypt_Byte(base64Decode(data), key); + } + + /** + * AES解密16进制密文 + * + * @param data 16进制密文 + * @param key 16、24、32字节秘钥 + * @return 明文 + */ + public byte[] Decrypt(final String data, final byte[] key) { + return Decrypt_Byte(hexString2Bytes(data), key); + } + + /** + * AES解密 + * + * @param data 密文 + * @param key 16、24、32字节秘钥 + * @return 明文 + */ + public byte[] Decrypt_Byte(final byte[] data, final byte[] key) { + return desTemplate(data, key, AES_Algorithm, AES_Transformation, false); + } + +} diff --git a/encodeutils/src/main/java/org/forevery/EncodeUtils/Encode/DES/DES_Utils.java b/EncryptUtils/src/main/java/org/forevery/EncryptUtils/Encode/DES/DES_Utils.java similarity index 82% rename from encodeutils/src/main/java/org/forevery/EncodeUtils/Encode/DES/DES_Utils.java rename to EncryptUtils/src/main/java/org/forevery/EncryptUtils/Encode/DES/DES_Utils.java index da785e2..4e48f78 100644 --- a/encodeutils/src/main/java/org/forevery/EncodeUtils/Encode/DES/DES_Utils.java +++ b/EncryptUtils/src/main/java/org/forevery/EncryptUtils/Encode/DES/DES_Utils.java @@ -1,88 +1,88 @@ -package org.forevery.EncodeUtils.Encode.DES; - -import static org.forevery.EncodeUtils.Encode.Utils.base64Decode; -import static org.forevery.EncodeUtils.Encode.Utils.base64Encode; -import static org.forevery.EncodeUtils.Encode.Utils.bytes2HexString; -import static org.forevery.EncodeUtils.Encode.Utils.desTemplate; -import static org.forevery.EncodeUtils.Encode.Utils.hexString2Bytes; - -/** - * Created by Forevery on 2017/9/29. - */ - -public class DES_Utils { - /** - * DES转变 - *法算法名称/加密模式/填充方式
- *加密模式有:电子密码本模式ECB、加密块链模式CBC、加密反馈模式CFB、输出反馈模式OFB
- *填充方式有:NoPadding、ZerosPadding、PKCS5Padding
- */ - public String DES_Transformation = "DES/ECB/NoPadding"; - private final String DES_Algorithm = "DES"; - - /** - * DES加密后转为Base64编码 - * - * @param data 明文 - * @param key 8字节秘钥 - * @return Base64密文 - */ - public byte[] Encrypt_Base64(final byte[] data, final byte[] key) { - return base64Encode(Encrypt_Byte(data, key)); - } - - /** - * DES加密后转为16进制 - * - * @param data 明文 - * @param key 8字节秘钥 - * @return 16进制密文 - */ - public String Encrypt(final byte[] data, final byte[] key) { - return bytes2HexString(Encrypt_Byte(data, key)); - } - - /** - * DES加密 - * - * @param data 明文 - * @param key 8字节秘钥 - * @return 密文 - */ - public byte[] Encrypt_Byte(final byte[] data, final byte[] key) { - return desTemplate(data, key, DES_Algorithm, DES_Transformation, true); - } - - /** - * DES解密Base64编码密文 - * - * @param data Base64编码密文 - * @param key 8字节秘钥 - * @return 明文 - */ - public byte[] Decrypt_Base64(final byte[] data, final byte[] key) { - return Decrypt(base64Decode(data), key); - } - - /** - * DES解密16进制密文 - * - * @param data 16进制密文 - * @param key 8字节秘钥 - * @return 明文 - */ - public byte[] Decrypt(final String data, final byte[] key) { - return Decrypt(hexString2Bytes(data), key); - } - - /** - * DES解密 - * - * @param data 密文 - * @param key 8字节秘钥 - * @return 明文 - */ - public byte[] Decrypt(final byte[] data, final byte[] key) { - return desTemplate(data, key, DES_Algorithm, DES_Transformation, false); - } -} +package org.forevery.EncryptUtils.Encode.DES; + +import static org.forevery.EncryptUtils.Encode.Utils.base64Decode; +import static org.forevery.EncryptUtils.Encode.Utils.base64Encode; +import static org.forevery.EncryptUtils.Encode.Utils.bytes2HexString; +import static org.forevery.EncryptUtils.Encode.Utils.desTemplate; +import static org.forevery.EncryptUtils.Encode.Utils.hexString2Bytes; + +/** + * Created by Forevery on 2017/9/29. + */ + +public class DES_Utils { + /** + * DES转变 + *法算法名称/加密模式/填充方式
+ *加密模式有:电子密码本模式ECB、加密块链模式CBC、加密反馈模式CFB、输出反馈模式OFB
+ *填充方式有:NoPadding、ZerosPadding、PKCS5Padding
+ */ + public String DES_Transformation = "DES/ECB/NoPadding"; + private final String DES_Algorithm = "DES"; + + /** + * DES加密后转为Base64编码 + * + * @param data 明文 + * @param key 8字节秘钥 + * @return Base64密文 + */ + public byte[] Encrypt_Base64(final byte[] data, final byte[] key) { + return base64Encode(Encrypt_Byte(data, key)); + } + + /** + * DES加密后转为16进制 + * + * @param data 明文 + * @param key 8字节秘钥 + * @return 16进制密文 + */ + public String Encrypt(final byte[] data, final byte[] key) { + return bytes2HexString(Encrypt_Byte(data, key)); + } + + /** + * DES加密 + * + * @param data 明文 + * @param key 8字节秘钥 + * @return 密文 + */ + public byte[] Encrypt_Byte(final byte[] data, final byte[] key) { + return desTemplate(data, key, DES_Algorithm, DES_Transformation, true); + } + + /** + * DES解密Base64编码密文 + * + * @param data Base64编码密文 + * @param key 8字节秘钥 + * @return 明文 + */ + public byte[] Decrypt_Base64(final byte[] data, final byte[] key) { + return Decrypt(base64Decode(data), key); + } + + /** + * DES解密16进制密文 + * + * @param data 16进制密文 + * @param key 8字节秘钥 + * @return 明文 + */ + public byte[] Decrypt(final String data, final byte[] key) { + return Decrypt(hexString2Bytes(data), key); + } + + /** + * DES解密 + * + * @param data 密文 + * @param key 8字节秘钥 + * @return 明文 + */ + public byte[] Decrypt(final byte[] data, final byte[] key) { + return desTemplate(data, key, DES_Algorithm, DES_Transformation, false); + } +} diff --git a/encodeutils/src/main/java/org/forevery/EncodeUtils/Encode/HmacMD5/HmacMD5_Utils.java b/EncryptUtils/src/main/java/org/forevery/EncryptUtils/Encode/HmacMD5/HmacMD5_Utils.java similarity index 79% rename from encodeutils/src/main/java/org/forevery/EncodeUtils/Encode/HmacMD5/HmacMD5_Utils.java rename to EncryptUtils/src/main/java/org/forevery/EncryptUtils/Encode/HmacMD5/HmacMD5_Utils.java index 5a85169..b64b3dc 100644 --- a/encodeutils/src/main/java/org/forevery/EncodeUtils/Encode/HmacMD5/HmacMD5_Utils.java +++ b/EncryptUtils/src/main/java/org/forevery/EncryptUtils/Encode/HmacMD5/HmacMD5_Utils.java @@ -1,44 +1,44 @@ -package org.forevery.EncodeUtils.Encode.HmacMD5; - -import static org.forevery.EncodeUtils.Encode.Utils.bytes2HexString; -import static org.forevery.EncodeUtils.Encode.Utils.hmacTemplate; - -/** - * Created by Forevery on 2017/9/29. - */ - -public class HmacMD5_Utils { - - /** - * HmacMD5加密 - * - * @param data 明文字符串 - * @param key 秘钥 - * @return 16进制密文 - */ - public String Encrypt(final String data, final String key) { - return Encrypt(data.getBytes(), key.getBytes()); - } - - /** - * HmacMD5加密 - * - * @param data 明文字节数组 - * @param key 秘钥 - * @return 16进制密文 - */ - public String Encrypt(final byte[] data, final byte[] key) { - return bytes2HexString(Encrypt_Byte(data, key)); - } - - /** - * HmacMD5加密 - * - * @param data 明文字节数组 - * @param key 秘钥 - * @return 密文字节数组 - */ - public byte[] Encrypt_Byte(final byte[] data, final byte[] key) { - return hmacTemplate(data, key, "HmacMD5"); - } -} +package org.forevery.EncryptUtils.Encode.HmacMD5; + +import static org.forevery.EncryptUtils.Encode.Utils.bytes2HexString; +import static org.forevery.EncryptUtils.Encode.Utils.hmacTemplate; + +/** + * Created by Forevery on 2017/9/29. + */ + +public class HmacMD5_Utils { + + /** + * HmacMD5加密 + * + * @param data 明文字符串 + * @param key 秘钥 + * @return 16进制密文 + */ + public String Encrypt(final String data, final String key) { + return Encrypt(data.getBytes(), key.getBytes()); + } + + /** + * HmacMD5加密 + * + * @param data 明文字节数组 + * @param key 秘钥 + * @return 16进制密文 + */ + public String Encrypt(final byte[] data, final byte[] key) { + return bytes2HexString(Encrypt_Byte(data, key)); + } + + /** + * HmacMD5加密 + * + * @param data 明文字节数组 + * @param key 秘钥 + * @return 密文字节数组 + */ + public byte[] Encrypt_Byte(final byte[] data, final byte[] key) { + return hmacTemplate(data, key, "HmacMD5"); + } +} diff --git a/encodeutils/src/main/java/org/forevery/EncodeUtils/Encode/HmacSHA1/HmacSHA1_Utils.java b/EncryptUtils/src/main/java/org/forevery/EncryptUtils/Encode/HmacSHA1/HmacSHA1_Utils.java similarity index 79% rename from encodeutils/src/main/java/org/forevery/EncodeUtils/Encode/HmacSHA1/HmacSHA1_Utils.java rename to EncryptUtils/src/main/java/org/forevery/EncryptUtils/Encode/HmacSHA1/HmacSHA1_Utils.java index fb07d09..0a9fbc9 100644 --- a/encodeutils/src/main/java/org/forevery/EncodeUtils/Encode/HmacSHA1/HmacSHA1_Utils.java +++ b/EncryptUtils/src/main/java/org/forevery/EncryptUtils/Encode/HmacSHA1/HmacSHA1_Utils.java @@ -1,45 +1,45 @@ -package org.forevery.EncodeUtils.Encode.HmacSHA1; - - -import static org.forevery.EncodeUtils.Encode.Utils.bytes2HexString; -import static org.forevery.EncodeUtils.Encode.Utils.hmacTemplate; - -/** - * Created by Forevery on 2017/9/29. - */ - -public class HmacSHA1_Utils { - - /** - * HmacSHA1加密 - * - * @param data 明文字符串 - * @param key 秘钥 - * @return 16进制密文 - */ - public String Encrypt(final String data, final String key) { - return Encrypt(data.getBytes(), key.getBytes()); - } - - /** - * HmacSHA1加密 - * - * @param data 明文字节数组 - * @param key 秘钥 - * @return 16进制密文 - */ - public String Encrypt(final byte[] data, final byte[] key) { - return bytes2HexString(Encrypt_Byte(data, key)); - } - - /** - * HmacSHA1加密 - * - * @param data 明文字节数组 - * @param key 秘钥 - * @return 密文字节数组 - */ - public byte[] Encrypt_Byte(final byte[] data, final byte[] key) { - return hmacTemplate(data, key, "HmacSHA1"); - } -} +package org.forevery.EncryptUtils.Encode.HmacSHA1; + + +import static org.forevery.EncryptUtils.Encode.Utils.bytes2HexString; +import static org.forevery.EncryptUtils.Encode.Utils.hmacTemplate; + +/** + * Created by Forevery on 2017/9/29. + */ + +public class HmacSHA1_Utils { + + /** + * HmacSHA1加密 + * + * @param data 明文字符串 + * @param key 秘钥 + * @return 16进制密文 + */ + public String Encrypt(final String data, final String key) { + return Encrypt(data.getBytes(), key.getBytes()); + } + + /** + * HmacSHA1加密 + * + * @param data 明文字节数组 + * @param key 秘钥 + * @return 16进制密文 + */ + public String Encrypt(final byte[] data, final byte[] key) { + return bytes2HexString(Encrypt_Byte(data, key)); + } + + /** + * HmacSHA1加密 + * + * @param data 明文字节数组 + * @param key 秘钥 + * @return 密文字节数组 + */ + public byte[] Encrypt_Byte(final byte[] data, final byte[] key) { + return hmacTemplate(data, key, "HmacSHA1"); + } +} diff --git a/encodeutils/src/main/java/org/forevery/EncodeUtils/Encode/HmacSHA224/HmacSHA224_Utils.java b/EncryptUtils/src/main/java/org/forevery/EncryptUtils/Encode/HmacSHA224/HmacSHA224_Utils.java similarity index 79% rename from encodeutils/src/main/java/org/forevery/EncodeUtils/Encode/HmacSHA224/HmacSHA224_Utils.java rename to EncryptUtils/src/main/java/org/forevery/EncryptUtils/Encode/HmacSHA224/HmacSHA224_Utils.java index f46dc4d..facbb68 100644 --- a/encodeutils/src/main/java/org/forevery/EncodeUtils/Encode/HmacSHA224/HmacSHA224_Utils.java +++ b/EncryptUtils/src/main/java/org/forevery/EncryptUtils/Encode/HmacSHA224/HmacSHA224_Utils.java @@ -1,45 +1,45 @@ -package org.forevery.EncodeUtils.Encode.HmacSHA224; - -import static org.forevery.EncodeUtils.Encode.Utils.bytes2HexString; -import static org.forevery.EncodeUtils.Encode.Utils.hmacTemplate; - -/** - * Created by Forevery on 2017/9/29. - */ - -public class HmacSHA224_Utils { - - /** - * HmacSHA224加密 - * - * @param data 明文字符串 - * @param key 秘钥 - * @return 16进制密文 - */ - public String Encrypt(final String data, final String key) { - return Encrypt(data.getBytes(), key.getBytes()); - } - - /** - * HmacSHA224加密 - * - * @param data 明文字节数组 - * @param key 秘钥 - * @return 16进制密文 - */ - public String Encrypt(final byte[] data, final byte[] key) { - return bytes2HexString(Encrypt_Byte(data, key)); - } - - /** - * HmacSHA224加密 - * - * @param data 明文字节数组 - * @param key 秘钥 - * @return 密文字节数组 - */ - public byte[] Encrypt_Byte(final byte[] data, final byte[] key) { - return hmacTemplate(data, key, "HmacSHA224"); - } - -} +package org.forevery.EncryptUtils.Encode.HmacSHA224; + +import static org.forevery.EncryptUtils.Encode.Utils.bytes2HexString; +import static org.forevery.EncryptUtils.Encode.Utils.hmacTemplate; + +/** + * Created by Forevery on 2017/9/29. + */ + +public class HmacSHA224_Utils { + + /** + * HmacSHA224加密 + * + * @param data 明文字符串 + * @param key 秘钥 + * @return 16进制密文 + */ + public String Encrypt(final String data, final String key) { + return Encrypt(data.getBytes(), key.getBytes()); + } + + /** + * HmacSHA224加密 + * + * @param data 明文字节数组 + * @param key 秘钥 + * @return 16进制密文 + */ + public String Encrypt(final byte[] data, final byte[] key) { + return bytes2HexString(Encrypt_Byte(data, key)); + } + + /** + * HmacSHA224加密 + * + * @param data 明文字节数组 + * @param key 秘钥 + * @return 密文字节数组 + */ + public byte[] Encrypt_Byte(final byte[] data, final byte[] key) { + return hmacTemplate(data, key, "HmacSHA224"); + } + +} diff --git a/encodeutils/src/main/java/org/forevery/EncodeUtils/Encode/HmacSHA256/HmacSHA256_Utils.java b/EncryptUtils/src/main/java/org/forevery/EncryptUtils/Encode/HmacSHA256/HmacSHA256_Utils.java similarity index 79% rename from encodeutils/src/main/java/org/forevery/EncodeUtils/Encode/HmacSHA256/HmacSHA256_Utils.java rename to EncryptUtils/src/main/java/org/forevery/EncryptUtils/Encode/HmacSHA256/HmacSHA256_Utils.java index 3088912..dc98f82 100644 --- a/encodeutils/src/main/java/org/forevery/EncodeUtils/Encode/HmacSHA256/HmacSHA256_Utils.java +++ b/EncryptUtils/src/main/java/org/forevery/EncryptUtils/Encode/HmacSHA256/HmacSHA256_Utils.java @@ -1,45 +1,45 @@ -package org.forevery.EncodeUtils.Encode.HmacSHA256; - -import static org.forevery.EncodeUtils.Encode.Utils.bytes2HexString; -import static org.forevery.EncodeUtils.Encode.Utils.hmacTemplate; - -/** - * Created by Forevery on 2017/9/29. - */ - -public class HmacSHA256_Utils { - - /** - * HmacSHA256加密 - * - * @param data 明文字符串 - * @param key 秘钥 - * @return 16进制密文 - */ - public String Encrypt(final String data, final String key) { - return Encrypt(data.getBytes(), key.getBytes()); - } - - /** - * HmacSHA256加密 - * - * @param data 明文字节数组 - * @param key 秘钥 - * @return 16进制密文 - */ - public String Encrypt(final byte[] data, final byte[] key) { - return bytes2HexString(Encrypt_Byte(data, key)); - } - - /** - * HmacSHA256加密 - * - * @param data 明文字节数组 - * @param key 秘钥 - * @return 密文字节数组 - */ - public byte[] Encrypt_Byte(final byte[] data, final byte[] key) { - return hmacTemplate(data, key, "HmacSHA256"); - } - -} +package org.forevery.EncryptUtils.Encode.HmacSHA256; + +import static org.forevery.EncryptUtils.Encode.Utils.bytes2HexString; +import static org.forevery.EncryptUtils.Encode.Utils.hmacTemplate; + +/** + * Created by Forevery on 2017/9/29. + */ + +public class HmacSHA256_Utils { + + /** + * HmacSHA256加密 + * + * @param data 明文字符串 + * @param key 秘钥 + * @return 16进制密文 + */ + public String Encrypt(final String data, final String key) { + return Encrypt(data.getBytes(), key.getBytes()); + } + + /** + * HmacSHA256加密 + * + * @param data 明文字节数组 + * @param key 秘钥 + * @return 16进制密文 + */ + public String Encrypt(final byte[] data, final byte[] key) { + return bytes2HexString(Encrypt_Byte(data, key)); + } + + /** + * HmacSHA256加密 + * + * @param data 明文字节数组 + * @param key 秘钥 + * @return 密文字节数组 + */ + public byte[] Encrypt_Byte(final byte[] data, final byte[] key) { + return hmacTemplate(data, key, "HmacSHA256"); + } + +} diff --git a/encodeutils/src/main/java/org/forevery/EncodeUtils/Encode/HmacSHA384/HmacSHA384_Utils.java b/EncryptUtils/src/main/java/org/forevery/EncryptUtils/Encode/HmacSHA384/HmacSHA384_Utils.java similarity index 79% rename from encodeutils/src/main/java/org/forevery/EncodeUtils/Encode/HmacSHA384/HmacSHA384_Utils.java rename to EncryptUtils/src/main/java/org/forevery/EncryptUtils/Encode/HmacSHA384/HmacSHA384_Utils.java index 4e56343..ab7726b 100644 --- a/encodeutils/src/main/java/org/forevery/EncodeUtils/Encode/HmacSHA384/HmacSHA384_Utils.java +++ b/EncryptUtils/src/main/java/org/forevery/EncryptUtils/Encode/HmacSHA384/HmacSHA384_Utils.java @@ -1,44 +1,44 @@ -package org.forevery.EncodeUtils.Encode.HmacSHA384; - -import static org.forevery.EncodeUtils.Encode.Utils.bytes2HexString; -import static org.forevery.EncodeUtils.Encode.Utils.hmacTemplate; - -/** - * Created by Forevery on 2017/9/29. - */ - -public class HmacSHA384_Utils { - /** - * HmacSHA384加密 - * - * @param data 明文字符串 - * @param key 秘钥 - * @return 16进制密文 - */ - public String Encrypt(final String data, final String key) { - return Encrypt(data.getBytes(), key.getBytes()); - } - - /** - * HmacSHA384加密 - * - * @param data 明文字节数组 - * @param key 秘钥 - * @return 16进制密文 - */ - public String Encrypt(final byte[] data, final byte[] key) { - return bytes2HexString(Encrypt_Byte(data, key)); - } - - /** - * HmacSHA384加密 - * - * @param data 明文字节数组 - * @param key 秘钥 - * @return 密文字节数组 - */ - public byte[] Encrypt_Byte(final byte[] data, final byte[] key) { - return hmacTemplate(data, key, "HmacSHA384"); - } - -} +package org.forevery.EncryptUtils.Encode.HmacSHA384; + +import static org.forevery.EncryptUtils.Encode.Utils.bytes2HexString; +import static org.forevery.EncryptUtils.Encode.Utils.hmacTemplate; + +/** + * Created by Forevery on 2017/9/29. + */ + +public class HmacSHA384_Utils { + /** + * HmacSHA384加密 + * + * @param data 明文字符串 + * @param key 秘钥 + * @return 16进制密文 + */ + public String Encrypt(final String data, final String key) { + return Encrypt(data.getBytes(), key.getBytes()); + } + + /** + * HmacSHA384加密 + * + * @param data 明文字节数组 + * @param key 秘钥 + * @return 16进制密文 + */ + public String Encrypt(final byte[] data, final byte[] key) { + return bytes2HexString(Encrypt_Byte(data, key)); + } + + /** + * HmacSHA384加密 + * + * @param data 明文字节数组 + * @param key 秘钥 + * @return 密文字节数组 + */ + public byte[] Encrypt_Byte(final byte[] data, final byte[] key) { + return hmacTemplate(data, key, "HmacSHA384"); + } + +} diff --git a/encodeutils/src/main/java/org/forevery/EncodeUtils/Encode/HmacSHA512/HmacSHA512_Utils.java b/EncryptUtils/src/main/java/org/forevery/EncryptUtils/Encode/HmacSHA512/HmacSHA512_Utils.java similarity index 79% rename from encodeutils/src/main/java/org/forevery/EncodeUtils/Encode/HmacSHA512/HmacSHA512_Utils.java rename to EncryptUtils/src/main/java/org/forevery/EncryptUtils/Encode/HmacSHA512/HmacSHA512_Utils.java index a4a6c7c..44b2440 100644 --- a/encodeutils/src/main/java/org/forevery/EncodeUtils/Encode/HmacSHA512/HmacSHA512_Utils.java +++ b/EncryptUtils/src/main/java/org/forevery/EncryptUtils/Encode/HmacSHA512/HmacSHA512_Utils.java @@ -1,44 +1,44 @@ -package org.forevery.EncodeUtils.Encode.HmacSHA512; - -import static org.forevery.EncodeUtils.Encode.Utils.bytes2HexString; -import static org.forevery.EncodeUtils.Encode.Utils.hmacTemplate; - -/** - * Created by Forevery on 2017/9/29. - */ - -public class HmacSHA512_Utils { - /** - * HmacSHA512加密 - * - * @param data 明文字符串 - * @param key 秘钥 - * @return 16进制密文 - */ - public String Encrypt(final String data, final String key) { - return Encrypt(data.getBytes(), key.getBytes()); - } - - /** - * HmacSHA512加密 - * - * @param data 明文字节数组 - * @param key 秘钥 - * @return 16进制密文 - */ - public String Encrypt(final byte[] data, final byte[] key) { - return bytes2HexString(Encrypt_Byte(data, key)); - } - - /** - * HmacSHA512加密 - * - * @param data 明文字节数组 - * @param key 秘钥 - * @return 密文字节数组 - */ - public byte[] Encrypt_Byte(final byte[] data, final byte[] key) { - return hmacTemplate(data, key, "HmacSHA512"); - } - -} +package org.forevery.EncryptUtils.Encode.HmacSHA512; + +import static org.forevery.EncryptUtils.Encode.Utils.bytes2HexString; +import static org.forevery.EncryptUtils.Encode.Utils.hmacTemplate; + +/** + * Created by Forevery on 2017/9/29. + */ + +public class HmacSHA512_Utils { + /** + * HmacSHA512加密 + * + * @param data 明文字符串 + * @param key 秘钥 + * @return 16进制密文 + */ + public String Encrypt(final String data, final String key) { + return Encrypt(data.getBytes(), key.getBytes()); + } + + /** + * HmacSHA512加密 + * + * @param data 明文字节数组 + * @param key 秘钥 + * @return 16进制密文 + */ + public String Encrypt(final byte[] data, final byte[] key) { + return bytes2HexString(Encrypt_Byte(data, key)); + } + + /** + * HmacSHA512加密 + * + * @param data 明文字节数组 + * @param key 秘钥 + * @return 密文字节数组 + */ + public byte[] Encrypt_Byte(final byte[] data, final byte[] key) { + return hmacTemplate(data, key, "HmacSHA512"); + } + +} diff --git a/encodeutils/src/main/java/org/forevery/EncodeUtils/Encode/MD2/MD2_Utils.java b/EncryptUtils/src/main/java/org/forevery/EncryptUtils/Encode/MD2/MD2_Utils.java similarity index 76% rename from encodeutils/src/main/java/org/forevery/EncodeUtils/Encode/MD2/MD2_Utils.java rename to EncryptUtils/src/main/java/org/forevery/EncryptUtils/Encode/MD2/MD2_Utils.java index ded5d4f..0708a5a 100644 --- a/encodeutils/src/main/java/org/forevery/EncodeUtils/Encode/MD2/MD2_Utils.java +++ b/EncryptUtils/src/main/java/org/forevery/EncryptUtils/Encode/MD2/MD2_Utils.java @@ -1,40 +1,40 @@ -package org.forevery.EncodeUtils.Encode.MD2; - -import static org.forevery.EncodeUtils.Encode.Utils.bytes2HexString; -import static org.forevery.EncodeUtils.Encode.Utils.hashTemplate; - -/** - * Created by Forevery on 2017/9/29. - */ - -public class MD2_Utils { - /** - * MD2加密 - * - * @param data 明文字符串 - * @return 16进制密文 - */ - public String Encrypt(final String data) { - return Encrypt(data.getBytes()); - } - - /** - * MD2加密 - * - * @param data 明文字节数组 - * @return 16进制密文 - */ - public String Encrypt(final byte[] data) { - return bytes2HexString(Encrypt_Byte(data)); - } - - /** - * MD2加密 - * - * @param data 明文字节数组 - * @return 密文字节数组 - */ - public byte[] Encrypt_Byte(final byte[] data) { - return hashTemplate(data, "MD2"); - } -} +package org.forevery.EncryptUtils.Encode.MD2; + +import static org.forevery.EncryptUtils.Encode.Utils.bytes2HexString; +import static org.forevery.EncryptUtils.Encode.Utils.hashTemplate; + +/** + * Created by Forevery on 2017/9/29. + */ + +public class MD2_Utils { + /** + * MD2加密 + * + * @param data 明文字符串 + * @return 16进制密文 + */ + public String Encrypt(final String data) { + return Encrypt(data.getBytes()); + } + + /** + * MD2加密 + * + * @param data 明文字节数组 + * @return 16进制密文 + */ + public String Encrypt(final byte[] data) { + return bytes2HexString(Encrypt_Byte(data)); + } + + /** + * MD2加密 + * + * @param data 明文字节数组 + * @return 密文字节数组 + */ + public byte[] Encrypt_Byte(final byte[] data) { + return hashTemplate(data, "MD2"); + } +} diff --git a/encodeutils/src/main/java/org/forevery/EncodeUtils/Encode/MD5/MD5_Utils.java b/EncryptUtils/src/main/java/org/forevery/EncryptUtils/Encode/MD5/MD5_Utils.java similarity index 92% rename from encodeutils/src/main/java/org/forevery/EncodeUtils/Encode/MD5/MD5_Utils.java rename to EncryptUtils/src/main/java/org/forevery/EncryptUtils/Encode/MD5/MD5_Utils.java index 67dc096..5fed045 100644 --- a/encodeutils/src/main/java/org/forevery/EncodeUtils/Encode/MD5/MD5_Utils.java +++ b/EncryptUtils/src/main/java/org/forevery/EncryptUtils/Encode/MD5/MD5_Utils.java @@ -1,148 +1,148 @@ -package org.forevery.EncodeUtils.Encode.MD5; - -import java.io.Closeable; -import java.io.File; -import java.io.FileInputStream; -import java.io.IOException; -import java.security.DigestInputStream; -import java.security.MessageDigest; -import java.security.NoSuchAlgorithmException; - -import static org.forevery.EncodeUtils.Encode.Utils.bytes2HexString; -import static org.forevery.EncodeUtils.Encode.Utils.hashTemplate; - -/** - * Created by Forevery on 2017/9/29. - */ - -public class MD5_Utils { - - /** - * MD5加密 - * - * @param data 明文字符串 - * @return 16进制密文 - */ - public String Encrypt(final String data) { - return Encrypt(data.getBytes()); - } - - /** - * MD5加密 - * - * @param data 明文字符串 - * @param salt 盐 - * @return 16进制加盐密文 - */ - public String Encrypt(final String data, final String salt) { - return bytes2HexString(Encrypt_Byte((data + salt).getBytes())); - } - - /** - * MD5加密 - * - * @param data 明文字节数组 - * @return 16进制密文 - */ - public String Encrypt(final byte[] data) { - return bytes2HexString(Encrypt_Byte(data)); - } - - /** - * MD5加密 - * - * @param data 明文字节数组 - * @param salt 盐字节数组 - * @return 16进制加盐密文 - */ - public String Encrypt(final byte[] data, final byte[] salt) { - if (data == null || salt == null) return null; - byte[] dataSalt = new byte[data.length + salt.length]; - System.arraycopy(data, 0, dataSalt, 0, data.length); - System.arraycopy(salt, 0, dataSalt, data.length, salt.length); - return bytes2HexString(Encrypt_Byte(dataSalt)); - } - - /** - * MD5加密 - * - * @param data 明文字节数组 - * @return 密文字节数组 - */ - public byte[] Encrypt_Byte(final byte[] data) { - return hashTemplate(data, "MD5"); - } - - /** - * MD5加密文件 - * - * @param filePath 文件路径 - * @return 文件的16进制密文 - *//* - public String Encrypt_File(final String filePath) { - File file = isSpace(filePath) ? null : new File(filePath); - return Encrypt_File2(file); - } -*/ - /** - * MD5加密文件 - * - * @param filePath 文件路径 - * @return 文件的MD5校验码 - */ - /*public byte[] Encrypt_File(final String filePath) { - File file = isSpace(filePath) ? null : new File(filePath); - return Encrypt_File(file); - }*/ - - /** - * MD5加密文件 - * - * @param file 文件 - * @return 文件的16进制密文 - */ - public String Encrypt_File2(final File file) { - return bytes2HexString(Encrypt_File(file)); - } - - /** - * MD5加密文件 - * - * @param file 文件 - * @return 文件的MD5校验码 - */ - public byte[] Encrypt_File(final File file) { - if (file == null) return null; - FileInputStream fis = null; - DigestInputStream digestInputStream; - try { - fis = new FileInputStream(file); - MessageDigest md = MessageDigest.getInstance("MD5"); - digestInputStream = new DigestInputStream(fis, md); - byte[] buffer = new byte[256 * 1024]; - while (true) { - if (!(digestInputStream.read(buffer) > 0)) break; - } - md = digestInputStream.getMessageDigest(); - return md.digest(); - } catch (NoSuchAlgorithmException | IOException e) { - e.printStackTrace(); - return null; - } finally { - closeIO(fis); - } - } - - private void closeIO(final Closeable... closeables) { - if (closeables == null) return; - for (Closeable closeable : closeables) { - if (closeable != null) { - try { - closeable.close(); - } catch (IOException e) { - e.printStackTrace(); - } - } - } - } -} +package org.forevery.EncryptUtils.Encode.MD5; + +import java.io.Closeable; +import java.io.File; +import java.io.FileInputStream; +import java.io.IOException; +import java.security.DigestInputStream; +import java.security.MessageDigest; +import java.security.NoSuchAlgorithmException; + +import static org.forevery.EncryptUtils.Encode.Utils.bytes2HexString; +import static org.forevery.EncryptUtils.Encode.Utils.hashTemplate; + +/** + * Created by Forevery on 2017/9/29. + */ + +public class MD5_Utils { + + /** + * MD5加密 + * + * @param data 明文字符串 + * @return 16进制密文 + */ + public String Encrypt(final String data) { + return Encrypt(data.getBytes()); + } + + /** + * MD5加密 + * + * @param data 明文字符串 + * @param salt 盐 + * @return 16进制加盐密文 + */ + public String Encrypt(final String data, final String salt) { + return bytes2HexString(Encrypt_Byte((data + salt).getBytes())); + } + + /** + * MD5加密 + * + * @param data 明文字节数组 + * @return 16进制密文 + */ + public String Encrypt(final byte[] data) { + return bytes2HexString(Encrypt_Byte(data)); + } + + /** + * MD5加密 + * + * @param data 明文字节数组 + * @param salt 盐字节数组 + * @return 16进制加盐密文 + */ + public String Encrypt(final byte[] data, final byte[] salt) { + if (data == null || salt == null) return null; + byte[] dataSalt = new byte[data.length + salt.length]; + System.arraycopy(data, 0, dataSalt, 0, data.length); + System.arraycopy(salt, 0, dataSalt, data.length, salt.length); + return bytes2HexString(Encrypt_Byte(dataSalt)); + } + + /** + * MD5加密 + * + * @param data 明文字节数组 + * @return 密文字节数组 + */ + public byte[] Encrypt_Byte(final byte[] data) { + return hashTemplate(data, "MD5"); + } + + /** + * MD5加密文件 + * + * @param filePath 文件路径 + * @return 文件的16进制密文 + *//* + public String Encrypt_File(final String filePath) { + File file = isSpace(filePath) ? null : new File(filePath); + return Encrypt_File2(file); + } +*/ + /** + * MD5加密文件 + * + * @param filePath 文件路径 + * @return 文件的MD5校验码 + */ + /*public byte[] Encrypt_File(final String filePath) { + File file = isSpace(filePath) ? null : new File(filePath); + return Encrypt_File(file); + }*/ + + /** + * MD5加密文件 + * + * @param file 文件 + * @return 文件的16进制密文 + */ + public String Encrypt_File2(final File file) { + return bytes2HexString(Encrypt_File(file)); + } + + /** + * MD5加密文件 + * + * @param file 文件 + * @return 文件的MD5校验码 + */ + public byte[] Encrypt_File(final File file) { + if (file == null) return null; + FileInputStream fis = null; + DigestInputStream digestInputStream; + try { + fis = new FileInputStream(file); + MessageDigest md = MessageDigest.getInstance("MD5"); + digestInputStream = new DigestInputStream(fis, md); + byte[] buffer = new byte[256 * 1024]; + while (true) { + if (!(digestInputStream.read(buffer) > 0)) break; + } + md = digestInputStream.getMessageDigest(); + return md.digest(); + } catch (NoSuchAlgorithmException | IOException e) { + e.printStackTrace(); + return null; + } finally { + closeIO(fis); + } + } + + private void closeIO(final Closeable... closeables) { + if (closeables == null) return; + for (Closeable closeable : closeables) { + if (closeable != null) { + try { + closeable.close(); + } catch (IOException e) { + e.printStackTrace(); + } + } + } + } +} diff --git a/encodeutils/src/main/java/org/forevery/EncodeUtils/Encode/RC4/RC4_Utils.java b/EncryptUtils/src/main/java/org/forevery/EncryptUtils/Encode/RC4/RC4_Utils.java similarity index 95% rename from encodeutils/src/main/java/org/forevery/EncodeUtils/Encode/RC4/RC4_Utils.java rename to EncryptUtils/src/main/java/org/forevery/EncryptUtils/Encode/RC4/RC4_Utils.java index c4cf028..b0eea1e 100644 --- a/encodeutils/src/main/java/org/forevery/EncodeUtils/Encode/RC4/RC4_Utils.java +++ b/EncryptUtils/src/main/java/org/forevery/EncryptUtils/Encode/RC4/RC4_Utils.java @@ -1,141 +1,141 @@ -package org.forevery.EncodeUtils.Encode.RC4; - -/** - * RC4 - * Created by Forevery on 2017/9/16. - */ - -public class RC4_Utils { - - /** - * @param data 数据 - * @param key 密钥 - * @return String - */ - - private String Decry(byte[] data, String key) { - if (data == null || key == null) { - return null; - } - return asString(RC4Base(data, key)); - } - - /** - * @param data 数据 - * @param key 密钥 - * @param Type 解密编码 - * @return String - */ - public String Decry(String data, String key, String Type) { - try { - if (data == null || key == null) { - return null; - } - return new String(RC4Base(HexString2Bytes(data), key), Type); - } catch (Exception e) { - return null; - } - } - - public String Decry(String data, String key) { - if (data == null || key == null) { - return null; - } - return new String(RC4Base(HexString2Bytes(data), key)); - } - - private byte[] Encry_Byte(String data, String key) { - if (data == null || key == null) { - return null; - } - byte b_data[] = data.getBytes(); - return RC4Base(b_data, key); - } - - public String Encry(String data, String key) { - if (data == null || key == null) { - return null; - } - return toHexString(asString(Encry_Byte(data, key))); - } - - private String asString(byte[] buf) { - StringBuffer strbuf = new StringBuffer(buf.length); - for (byte aBuf : buf) { - strbuf.append((char) aBuf); - } - return strbuf.toString(); - } - - private byte[] initKey(String aKey) { - byte[] b_key = aKey.getBytes(); - byte state[] = new byte[256]; - - for (int i = 0; i < 256; i++) { - state[i] = (byte) i; - } - int index1 = 0; - int index2 = 0; - if (b_key == null || b_key.length == 0) { - return null; - } - for (int i = 0; i < 256; i++) { - index2 = ((b_key[index1] & 0xff) + (state[i] & 0xff) + index2) & 0xff; - byte tmp = state[i]; - state[i] = state[index2]; - state[index2] = tmp; - index1 = (index1 + 1) % b_key.length; - } - return state; - } - - private String toHexString(String s) { - String str = ""; - for (int i = 0; i < s.length(); i++) { - int ch = (int) s.charAt(i); - String s4 = Integer.toHexString(ch & 0xFF); - if (s4.length() == 1) { - s4 = '0' + s4; - } - str = str + s4; - } - return str;// 0x表示十六进制 - } - - private byte[] HexString2Bytes(String src) { - int size = src.length(); - byte[] ret = new byte[size / 2]; - byte[] tmp = src.getBytes(); - for (int i = 0; i < size / 2; i++) { - ret[i] = uniteBytes(tmp[i * 2], tmp[i * 2 + 1]); - } - return ret; - } - - private byte uniteBytes(byte src0, byte src1) { - char _b0 = (char) Byte.decode("0x" + new String(new byte[]{src0})).byteValue(); - _b0 = (char) (_b0 << 4); - char _b1 = (char) Byte.decode("0x" + new String(new byte[]{src1})).byteValue(); - byte ret = (byte) (_b0 ^ _b1); - return ret; - } - - private byte[] RC4Base(byte[] input, String mKkey) { - int x = 0; - int y = 0; - byte key[] = initKey(mKkey); - int xorIndex; - byte[] result = new byte[input.length]; - - for (int i = 0; i < input.length; i++) { - x = (x + 1) & 0xff; - y = ((key[x] & 0xff) + y) & 0xff; - byte tmp = key[x]; - key[x] = key[y]; - key[y] = tmp; - xorIndex = ((key[x] & 0xff) + (key[y] & 0xff)) & 0xff; - result[i] = (byte) (input[i] ^ key[xorIndex]); - } - return result; - } -} +package org.forevery.EncryptUtils.Encode.RC4; + +/** + * RC4 + * Created by Forevery on 2017/9/16. + */ + +public class RC4_Utils { + + /** + * @param data 数据 + * @param key 密钥 + * @return String + */ + + private String Decry(byte[] data, String key) { + if (data == null || key == null) { + return null; + } + return asString(RC4Base(data, key)); + } + + /** + * @param data 数据 + * @param key 密钥 + * @param Type 解密编码 + * @return String + */ + public String Decry(String data, String key, String Type) { + try { + if (data == null || key == null) { + return null; + } + return new String(RC4Base(HexString2Bytes(data), key), Type); + } catch (Exception e) { + return null; + } + } + + public String Decry(String data, String key) { + if (data == null || key == null) { + return null; + } + return new String(RC4Base(HexString2Bytes(data), key)); + } + + private byte[] Encry_Byte(String data, String key) { + if (data == null || key == null) { + return null; + } + byte b_data[] = data.getBytes(); + return RC4Base(b_data, key); + } + + public String Encry(String data, String key) { + if (data == null || key == null) { + return null; + } + return toHexString(asString(Encry_Byte(data, key))); + } + + private String asString(byte[] buf) { + StringBuffer strbuf = new StringBuffer(buf.length); + for (byte aBuf : buf) { + strbuf.append((char) aBuf); + } + return strbuf.toString(); + } + + private byte[] initKey(String aKey) { + byte[] b_key = aKey.getBytes(); + byte state[] = new byte[256]; + + for (int i = 0; i < 256; i++) { + state[i] = (byte) i; + } + int index1 = 0; + int index2 = 0; + if (b_key == null || b_key.length == 0) { + return null; + } + for (int i = 0; i < 256; i++) { + index2 = ((b_key[index1] & 0xff) + (state[i] & 0xff) + index2) & 0xff; + byte tmp = state[i]; + state[i] = state[index2]; + state[index2] = tmp; + index1 = (index1 + 1) % b_key.length; + } + return state; + } + + private String toHexString(String s) { + String str = ""; + for (int i = 0; i < s.length(); i++) { + int ch = (int) s.charAt(i); + String s4 = Integer.toHexString(ch & 0xFF); + if (s4.length() == 1) { + s4 = '0' + s4; + } + str = str + s4; + } + return str;// 0x表示十六进制 + } + + private byte[] HexString2Bytes(String src) { + int size = src.length(); + byte[] ret = new byte[size / 2]; + byte[] tmp = src.getBytes(); + for (int i = 0; i < size / 2; i++) { + ret[i] = uniteBytes(tmp[i * 2], tmp[i * 2 + 1]); + } + return ret; + } + + private byte uniteBytes(byte src0, byte src1) { + char _b0 = (char) Byte.decode("0x" + new String(new byte[]{src0})).byteValue(); + _b0 = (char) (_b0 << 4); + char _b1 = (char) Byte.decode("0x" + new String(new byte[]{src1})).byteValue(); + byte ret = (byte) (_b0 ^ _b1); + return ret; + } + + private byte[] RC4Base(byte[] input, String mKkey) { + int x = 0; + int y = 0; + byte key[] = initKey(mKkey); + int xorIndex; + byte[] result = new byte[input.length]; + + for (int i = 0; i < input.length; i++) { + x = (x + 1) & 0xff; + y = ((key[x] & 0xff) + y) & 0xff; + byte tmp = key[x]; + key[x] = key[y]; + key[y] = tmp; + xorIndex = ((key[x] & 0xff) + (key[y] & 0xff)) & 0xff; + result[i] = (byte) (input[i] ^ key[xorIndex]); + } + return result; + } +} diff --git a/encodeutils/src/main/java/org/forevery/EncodeUtils/Encode/SHA1/SHA1_Utils.java b/EncryptUtils/src/main/java/org/forevery/EncryptUtils/Encode/SHA1/SHA1_Utils.java similarity index 75% rename from encodeutils/src/main/java/org/forevery/EncodeUtils/Encode/SHA1/SHA1_Utils.java rename to EncryptUtils/src/main/java/org/forevery/EncryptUtils/Encode/SHA1/SHA1_Utils.java index 07690f5..89ce980 100644 --- a/encodeutils/src/main/java/org/forevery/EncodeUtils/Encode/SHA1/SHA1_Utils.java +++ b/EncryptUtils/src/main/java/org/forevery/EncryptUtils/Encode/SHA1/SHA1_Utils.java @@ -1,43 +1,43 @@ -package org.forevery.EncodeUtils.Encode.SHA1; - - -import static org.forevery.EncodeUtils.Encode.Utils.bytes2HexString; -import static org.forevery.EncodeUtils.Encode.Utils.hashTemplate; - -/** - * Created by Forevery on 2017/9/29. - */ - -public class SHA1_Utils { - - /** - * SHA1加密 - * - * @param data 明文字符串 - * @return 16进制密文 - */ - public String Encrypt(final String data) { - return Encrypt(data.getBytes()); - } - - /** - * SHA1加密 - * - * @param data 明文字节数组 - * @return 16进制密文 - */ - public String Encrypt(final byte[] data) { - return bytes2HexString(Encrypt_Byte(data)); - } - - /** - * SHA1加密 - * - * @param data 明文字节数组 - * @return 密文字节数组 - */ - public byte[] Encrypt_Byte(final byte[] data) { - return hashTemplate(data, "SHA1"); - } - -} +package org.forevery.EncryptUtils.Encode.SHA1; + + +import static org.forevery.EncryptUtils.Encode.Utils.bytes2HexString; +import static org.forevery.EncryptUtils.Encode.Utils.hashTemplate; + +/** + * Created by Forevery on 2017/9/29. + */ + +public class SHA1_Utils { + + /** + * SHA1加密 + * + * @param data 明文字符串 + * @return 16进制密文 + */ + public String Encrypt(final String data) { + return Encrypt(data.getBytes()); + } + + /** + * SHA1加密 + * + * @param data 明文字节数组 + * @return 16进制密文 + */ + public String Encrypt(final byte[] data) { + return bytes2HexString(Encrypt_Byte(data)); + } + + /** + * SHA1加密 + * + * @param data 明文字节数组 + * @return 密文字节数组 + */ + public byte[] Encrypt_Byte(final byte[] data) { + return hashTemplate(data, "SHA1"); + } + +} diff --git a/encodeutils/src/main/java/org/forevery/EncodeUtils/Encode/SHA224/SHA224_Utils.java b/EncryptUtils/src/main/java/org/forevery/EncryptUtils/Encode/SHA224/SHA224_Utils.java similarity index 76% rename from encodeutils/src/main/java/org/forevery/EncodeUtils/Encode/SHA224/SHA224_Utils.java rename to EncryptUtils/src/main/java/org/forevery/EncryptUtils/Encode/SHA224/SHA224_Utils.java index 1bf9d44..d54ac8e 100644 --- a/encodeutils/src/main/java/org/forevery/EncodeUtils/Encode/SHA224/SHA224_Utils.java +++ b/EncryptUtils/src/main/java/org/forevery/EncryptUtils/Encode/SHA224/SHA224_Utils.java @@ -1,41 +1,41 @@ -package org.forevery.EncodeUtils.Encode.SHA224; - -import static org.forevery.EncodeUtils.Encode.Utils.bytes2HexString; -import static org.forevery.EncodeUtils.Encode.Utils.hashTemplate; - -/** - * Created by Forevery on 2017/9/29. - */ - -public class SHA224_Utils { - - /** - * SHA224加密 - * - * @param data 明文字符串 - * @return 16进制密文 - */ - public String Encrypt(final String data) { - return Encrypt(data.getBytes()); - } - - /** - * SHA224加密 - * - * @param data 明文字节数组 - * @return 16进制密文 - */ - public String Encrypt(final byte[] data) { - return bytes2HexString(Encrypt_Byte(data)); - } - - /** - * SHA224加密 - * - * @param data 明文字节数组 - * @return 密文字节数组 - */ - public byte[] Encrypt_Byte(final byte[] data) { - return hashTemplate(data, "SHA224"); - } -} +package org.forevery.EncryptUtils.Encode.SHA224; + +import static org.forevery.EncryptUtils.Encode.Utils.bytes2HexString; +import static org.forevery.EncryptUtils.Encode.Utils.hashTemplate; + +/** + * Created by Forevery on 2017/9/29. + */ + +public class SHA224_Utils { + + /** + * SHA224加密 + * + * @param data 明文字符串 + * @return 16进制密文 + */ + public String Encrypt(final String data) { + return Encrypt(data.getBytes()); + } + + /** + * SHA224加密 + * + * @param data 明文字节数组 + * @return 16进制密文 + */ + public String Encrypt(final byte[] data) { + return bytes2HexString(Encrypt_Byte(data)); + } + + /** + * SHA224加密 + * + * @param data 明文字节数组 + * @return 密文字节数组 + */ + public byte[] Encrypt_Byte(final byte[] data) { + return hashTemplate(data, "SHA224"); + } +} diff --git a/encodeutils/src/main/java/org/forevery/EncodeUtils/Encode/SHA256/SHA256_Utils.java b/EncryptUtils/src/main/java/org/forevery/EncryptUtils/Encode/SHA256/SHA256_Utils.java similarity index 76% rename from encodeutils/src/main/java/org/forevery/EncodeUtils/Encode/SHA256/SHA256_Utils.java rename to EncryptUtils/src/main/java/org/forevery/EncryptUtils/Encode/SHA256/SHA256_Utils.java index 90d8651..5d6c4a3 100644 --- a/encodeutils/src/main/java/org/forevery/EncodeUtils/Encode/SHA256/SHA256_Utils.java +++ b/EncryptUtils/src/main/java/org/forevery/EncryptUtils/Encode/SHA256/SHA256_Utils.java @@ -1,43 +1,43 @@ -package org.forevery.EncodeUtils.Encode.SHA256; - -import static org.forevery.EncodeUtils.Encode.Utils.bytes2HexString; -import static org.forevery.EncodeUtils.Encode.Utils.hashTemplate; - -/** - * Created by Forevery on 2017/9/29. - */ - -public class SHA256_Utils { - - /** - * SHA256加密 - * - * @param data 明文字符串 - * @return 16进制密文 - */ - public String Encrypt(final String data) { - return Encrypt(data.getBytes()); - } - - /** - * SHA256加密 - * - * @param data 明文字节数组 - * @return 16进制密文 - */ - public String Encrypt(final byte[] data) { - return bytes2HexString(Encrypt_Byte(data)); - } - - /** - * SHA256加密 - * - * @param data 明文字节数组 - * @return 密文字节数组 - */ - public byte[] Encrypt_Byte(final byte[] data) { - return hashTemplate(data, "SHA256"); - } - - -} +package org.forevery.EncryptUtils.Encode.SHA256; + +import static org.forevery.EncryptUtils.Encode.Utils.bytes2HexString; +import static org.forevery.EncryptUtils.Encode.Utils.hashTemplate; + +/** + * Created by Forevery on 2017/9/29. + */ + +public class SHA256_Utils { + + /** + * SHA256加密 + * + * @param data 明文字符串 + * @return 16进制密文 + */ + public String Encrypt(final String data) { + return Encrypt(data.getBytes()); + } + + /** + * SHA256加密 + * + * @param data 明文字节数组 + * @return 16进制密文 + */ + public String Encrypt(final byte[] data) { + return bytes2HexString(Encrypt_Byte(data)); + } + + /** + * SHA256加密 + * + * @param data 明文字节数组 + * @return 密文字节数组 + */ + public byte[] Encrypt_Byte(final byte[] data) { + return hashTemplate(data, "SHA256"); + } + + +} diff --git a/encodeutils/src/main/java/org/forevery/EncodeUtils/Encode/SHA384/SHA384_Utils.java b/EncryptUtils/src/main/java/org/forevery/EncryptUtils/Encode/SHA384/SHA384_Utils.java similarity index 76% rename from encodeutils/src/main/java/org/forevery/EncodeUtils/Encode/SHA384/SHA384_Utils.java rename to EncryptUtils/src/main/java/org/forevery/EncryptUtils/Encode/SHA384/SHA384_Utils.java index 294ed70..b83e99b 100644 --- a/encodeutils/src/main/java/org/forevery/EncodeUtils/Encode/SHA384/SHA384_Utils.java +++ b/EncryptUtils/src/main/java/org/forevery/EncryptUtils/Encode/SHA384/SHA384_Utils.java @@ -1,41 +1,41 @@ -package org.forevery.EncodeUtils.Encode.SHA384; - -import static org.forevery.EncodeUtils.Encode.Utils.bytes2HexString; -import static org.forevery.EncodeUtils.Encode.Utils.hashTemplate; - -/** - * Created by Forevery on 2017/9/29. - */ - -public class SHA384_Utils { - - /** - * SHA384加密 - * - * @param data 明文字符串 - * @return 16进制密文 - */ - public String Encrypt(final String data) { - return Encrypt(data.getBytes()); - } - - /** - * SHA384加密 - * - * @param data 明文字节数组 - * @return 16进制密文 - */ - public String Encrypt(final byte[] data) { - return bytes2HexString(Encrypt_Byte(data)); - } - - /** - * SHA384加密 - * - * @param data 明文字节数组 - * @return 密文字节数组 - */ - public byte[] Encrypt_Byte(final byte[] data) { - return hashTemplate(data, "SHA384"); - } -} +package org.forevery.EncryptUtils.Encode.SHA384; + +import static org.forevery.EncryptUtils.Encode.Utils.bytes2HexString; +import static org.forevery.EncryptUtils.Encode.Utils.hashTemplate; + +/** + * Created by Forevery on 2017/9/29. + */ + +public class SHA384_Utils { + + /** + * SHA384加密 + * + * @param data 明文字符串 + * @return 16进制密文 + */ + public String Encrypt(final String data) { + return Encrypt(data.getBytes()); + } + + /** + * SHA384加密 + * + * @param data 明文字节数组 + * @return 16进制密文 + */ + public String Encrypt(final byte[] data) { + return bytes2HexString(Encrypt_Byte(data)); + } + + /** + * SHA384加密 + * + * @param data 明文字节数组 + * @return 密文字节数组 + */ + public byte[] Encrypt_Byte(final byte[] data) { + return hashTemplate(data, "SHA384"); + } +} diff --git a/encodeutils/src/main/java/org/forevery/EncodeUtils/Encode/SHA512/SHA512_Utils.java b/EncryptUtils/src/main/java/org/forevery/EncryptUtils/Encode/SHA512/SHA512_Utils.java similarity index 76% rename from encodeutils/src/main/java/org/forevery/EncodeUtils/Encode/SHA512/SHA512_Utils.java rename to EncryptUtils/src/main/java/org/forevery/EncryptUtils/Encode/SHA512/SHA512_Utils.java index ddb092f..d6b4d47 100644 --- a/encodeutils/src/main/java/org/forevery/EncodeUtils/Encode/SHA512/SHA512_Utils.java +++ b/EncryptUtils/src/main/java/org/forevery/EncryptUtils/Encode/SHA512/SHA512_Utils.java @@ -1,41 +1,41 @@ -package org.forevery.EncodeUtils.Encode.SHA512; - -import static org.forevery.EncodeUtils.Encode.Utils.bytes2HexString; -import static org.forevery.EncodeUtils.Encode.Utils.hashTemplate; - -/** - * Created by Forevery on 2017/9/29. - */ - -public class SHA512_Utils { - - /** - * SHA512加密 - * - * @param data 明文字符串 - * @return 16进制密文 - */ - public String Encrypt(final String data) { - return Encrypt(data.getBytes()); - } - - /** - * SHA512加密 - * - * @param data 明文字节数组 - * @return 16进制密文 - */ - public String Encrypt(final byte[] data) { - return bytes2HexString(Encrypt_Byte(data)); - } - - /** - * SHA512加密 - * - * @param data 明文字节数组 - * @return 密文字节数组 - */ - public byte[] Encrypt_Byte(final byte[] data) { - return hashTemplate(data, "SHA512"); - } -} +package org.forevery.EncryptUtils.Encode.SHA512; + +import static org.forevery.EncryptUtils.Encode.Utils.bytes2HexString; +import static org.forevery.EncryptUtils.Encode.Utils.hashTemplate; + +/** + * Created by Forevery on 2017/9/29. + */ + +public class SHA512_Utils { + + /** + * SHA512加密 + * + * @param data 明文字符串 + * @return 16进制密文 + */ + public String Encrypt(final String data) { + return Encrypt(data.getBytes()); + } + + /** + * SHA512加密 + * + * @param data 明文字节数组 + * @return 16进制密文 + */ + public String Encrypt(final byte[] data) { + return bytes2HexString(Encrypt_Byte(data)); + } + + /** + * SHA512加密 + * + * @param data 明文字节数组 + * @return 密文字节数组 + */ + public byte[] Encrypt_Byte(final byte[] data) { + return hashTemplate(data, "SHA512"); + } +} diff --git a/encodeutils/src/main/java/org/forevery/EncodeUtils/Encode/T_3DES/T_3DES_Utils.java b/EncryptUtils/src/main/java/org/forevery/EncryptUtils/Encode/T_3DES/T_3DES_Utils.java similarity index 82% rename from encodeutils/src/main/java/org/forevery/EncodeUtils/Encode/T_3DES/T_3DES_Utils.java rename to EncryptUtils/src/main/java/org/forevery/EncryptUtils/Encode/T_3DES/T_3DES_Utils.java index 876a35a..e134f54 100644 --- a/encodeutils/src/main/java/org/forevery/EncodeUtils/Encode/T_3DES/T_3DES_Utils.java +++ b/EncryptUtils/src/main/java/org/forevery/EncryptUtils/Encode/T_3DES/T_3DES_Utils.java @@ -1,89 +1,89 @@ -package org.forevery.EncodeUtils.Encode.T_3DES; - -import static org.forevery.EncodeUtils.Encode.Utils.base64Decode; -import static org.forevery.EncodeUtils.Encode.Utils.base64Encode; -import static org.forevery.EncodeUtils.Encode.Utils.bytes2HexString; -import static org.forevery.EncodeUtils.Encode.Utils.desTemplate; -import static org.forevery.EncodeUtils.Encode.Utils.hexString2Bytes; - -/** - * Created by Forevery on 2017/9/29. - */ - -public class T_3DES_Utils { - /** - * 3DES转变 - *法算法名称/加密模式/填充方式
- *加密模式有:电子密码本模式ECB、加密块链模式CBC、加密反馈模式CFB、输出反馈模式OFB
- *填充方式有:NoPadding、ZerosPadding、PKCS5Padding
- */ - public String TripleDES_Transformation = "DESede/ECB/NoPadding"; - private final String TripleDES_Algorithm = "DESede"; - - - /** - * 3DES加密后转为Base64编码 - * - * @param data 明文 - * @param key 24字节秘钥 - * @return Base64密文 - */ - public byte[] Encrypt_Base64(final byte[] data, final byte[] key) { - return base64Encode(Decrypt_Byte(data, key)); - } - - /** - * 3DES加密后转为16进制 - * - * @param data 明文 - * @param key 24字节秘钥 - * @return 16进制密文 - */ - public String Encrypt(final byte[] data, final byte[] key) { - return bytes2HexString(Encrypt_Byte(data, key)); - } - - /** - * 3DES加密 - * - * @param data 明文 - * @param key 24字节密钥 - * @return 密文 - */ - public byte[] Encrypt_Byte(final byte[] data, final byte[] key) { - return desTemplate(data, key, TripleDES_Algorithm, TripleDES_Transformation, true); - } - - /** - * 3DES解密Base64编码密文 - * - * @param data Base64编码密文 - * @param key 24字节秘钥 - * @return 明文 - */ - public byte[] Decrypt(final byte[] data, final byte[] key) { - return Decrypt_Byte(base64Decode(data), key); - } - - /** - * 3DES解密16进制密文 - * - * @param data 16进制密文 - * @param key 24字节秘钥 - * @return 明文 - */ - public byte[] Decrypt(final String data, final byte[] key) { - return Decrypt_Byte(hexString2Bytes(data), key); - } - - /** - * 3DES解密 - * - * @param data 密文 - * @param key 24字节密钥 - * @return 明文 - */ - public byte[] Decrypt_Byte(final byte[] data, final byte[] key) { - return desTemplate(data, key, TripleDES_Algorithm, TripleDES_Transformation, false); - } -} +package org.forevery.EncryptUtils.Encode.T_3DES; + +import static org.forevery.EncryptUtils.Encode.Utils.base64Decode; +import static org.forevery.EncryptUtils.Encode.Utils.base64Encode; +import static org.forevery.EncryptUtils.Encode.Utils.bytes2HexString; +import static org.forevery.EncryptUtils.Encode.Utils.desTemplate; +import static org.forevery.EncryptUtils.Encode.Utils.hexString2Bytes; + +/** + * Created by Forevery on 2017/9/29. + */ + +public class T_3DES_Utils { + /** + * 3DES转变 + *法算法名称/加密模式/填充方式
+ *加密模式有:电子密码本模式ECB、加密块链模式CBC、加密反馈模式CFB、输出反馈模式OFB
+ *填充方式有:NoPadding、ZerosPadding、PKCS5Padding
+ */ + public String TripleDES_Transformation = "DESede/ECB/NoPadding"; + private final String TripleDES_Algorithm = "DESede"; + + + /** + * 3DES加密后转为Base64编码 + * + * @param data 明文 + * @param key 24字节秘钥 + * @return Base64密文 + */ + public byte[] Encrypt_Base64(final byte[] data, final byte[] key) { + return base64Encode(Decrypt_Byte(data, key)); + } + + /** + * 3DES加密后转为16进制 + * + * @param data 明文 + * @param key 24字节秘钥 + * @return 16进制密文 + */ + public String Encrypt(final byte[] data, final byte[] key) { + return bytes2HexString(Encrypt_Byte(data, key)); + } + + /** + * 3DES加密 + * + * @param data 明文 + * @param key 24字节密钥 + * @return 密文 + */ + public byte[] Encrypt_Byte(final byte[] data, final byte[] key) { + return desTemplate(data, key, TripleDES_Algorithm, TripleDES_Transformation, true); + } + + /** + * 3DES解密Base64编码密文 + * + * @param data Base64编码密文 + * @param key 24字节秘钥 + * @return 明文 + */ + public byte[] Decrypt(final byte[] data, final byte[] key) { + return Decrypt_Byte(base64Decode(data), key); + } + + /** + * 3DES解密16进制密文 + * + * @param data 16进制密文 + * @param key 24字节秘钥 + * @return 明文 + */ + public byte[] Decrypt(final String data, final byte[] key) { + return Decrypt_Byte(hexString2Bytes(data), key); + } + + /** + * 3DES解密 + * + * @param data 密文 + * @param key 24字节密钥 + * @return 明文 + */ + public byte[] Decrypt_Byte(final byte[] data, final byte[] key) { + return desTemplate(data, key, TripleDES_Algorithm, TripleDES_Transformation, false); + } +} diff --git a/encodeutils/src/main/java/org/forevery/EncodeUtils/Encode/Utils.java b/EncryptUtils/src/main/java/org/forevery/EncryptUtils/Encode/Utils.java similarity index 96% rename from encodeutils/src/main/java/org/forevery/EncodeUtils/Encode/Utils.java rename to EncryptUtils/src/main/java/org/forevery/EncryptUtils/Encode/Utils.java index bbe026d..dd1dc6e 100644 --- a/encodeutils/src/main/java/org/forevery/EncodeUtils/Encode/Utils.java +++ b/EncryptUtils/src/main/java/org/forevery/EncryptUtils/Encode/Utils.java @@ -1,140 +1,140 @@ -package org.forevery.EncodeUtils.Encode; - -import android.util.Base64; - -import java.security.InvalidKeyException; -import java.security.MessageDigest; -import java.security.NoSuchAlgorithmException; -import java.security.SecureRandom; - -import javax.crypto.Cipher; -import javax.crypto.Mac; -import javax.crypto.spec.SecretKeySpec; - -/** - * Created by Forevery on 2017/9/29. - */ - -public class Utils { - - private static final char hexDigits[] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'}; - - public static String bytes2HexString(final byte[] bytes) { - if (bytes == null) return null; - int len = bytes.length; - if (len <= 0) return null; - char[] ret = new char[len << 1]; - for (int i = 0, j = 0; i < len; i++) { - ret[j++] = hexDigits[bytes[i] >>> 4 & 0x0f]; - ret[j++] = hexDigits[bytes[i] & 0x0f]; - } - return new String(ret); - } - - /** - * hash加密模板 - * - * @param data 数据 - * @param algorithm 加密算法 - * @return 密文字节数组 - */ - public static byte[] hashTemplate(final byte[] data, final String algorithm) { - if (data == null || data.length <= 0) return null; - try { - MessageDigest md = MessageDigest.getInstance(algorithm); - md.update(data); - return md.digest(); - } catch (NoSuchAlgorithmException e) { - e.printStackTrace(); - return null; - } - } - - /** - * Hmac加密模板 - * - * @param data 数据 - * @param key 秘钥 - * @param algorithm 加密算法 - * @return 密文字节数组 - */ - public static byte[] hmacTemplate(final byte[] data, final byte[] key, final String algorithm) { - if (data == null || data.length == 0 || key == null || key.length == 0) return null; - try { - SecretKeySpec secretKey = new SecretKeySpec(key, algorithm); - Mac mac = Mac.getInstance(algorithm); - mac.init(secretKey); - return mac.doFinal(data); - } catch (InvalidKeyException | NoSuchAlgorithmException e) { - e.printStackTrace(); - return null; - } - } - - public static byte[] hexString2Bytes(String hexString) { - if (isSpace(hexString)) return null; - int len = hexString.length(); - if (len % 2 != 0) { - hexString = "0" + hexString; - len = len + 1; - } - char[] hexBytes = hexString.toUpperCase().toCharArray(); - byte[] ret = new byte[len >> 1]; - for (int i = 0; i < len; i += 2) { - ret[i >> 1] = (byte) (hex2Dec(hexBytes[i]) << 4 | hex2Dec(hexBytes[i + 1])); - } - return ret; - } - - public static int hex2Dec(final char hexChar) { - if (hexChar >= '0' && hexChar <= '9') { - return hexChar - '0'; - } else if (hexChar >= 'A' && hexChar <= 'F') { - return hexChar - 'A' + 10; - } else { - throw new IllegalArgumentException(); - } - } - - public static byte[] base64Encode(final byte[] input) { - return Base64.encode(input, Base64.NO_WRAP); - } - - public static byte[] base64Decode(final byte[] input) { - return Base64.decode(input, Base64.NO_WRAP); - } - - public static boolean isSpace(final String s) { - if (s == null) return true; - for (int i = 0, len = s.length(); i < len; ++i) { - if (!Character.isWhitespace(s.charAt(i))) { - return false; - } - } - return true; - } - - /** - * DES加密模板 - * - * @param data 数据 - * @param key 秘钥 - * @param algorithm 加密算法 - * @param transformation 转变 - * @param isEncrypt {@code true}: 加密 {@code false}: 解密 - * @return 密文或者明文,适用于DES,3DES,AES - */ - public static byte[] desTemplate(final byte[] data, final byte[] key, final String algorithm, final String transformation, final boolean isEncrypt) { - if (data == null || data.length == 0 || key == null || key.length == 0) return null; - try { - SecretKeySpec keySpec = new SecretKeySpec(key, algorithm); - Cipher cipher = Cipher.getInstance(transformation); - SecureRandom random = new SecureRandom(); - cipher.init(isEncrypt ? Cipher.ENCRYPT_MODE : Cipher.DECRYPT_MODE, keySpec, random); - return cipher.doFinal(data); - } catch (Throwable e) { - e.printStackTrace(); - return null; - } - } -} +package org.forevery.EncryptUtils.Encode; + +import android.util.Base64; + +import java.security.InvalidKeyException; +import java.security.MessageDigest; +import java.security.NoSuchAlgorithmException; +import java.security.SecureRandom; + +import javax.crypto.Cipher; +import javax.crypto.Mac; +import javax.crypto.spec.SecretKeySpec; + +/** + * Created by Forevery on 2017/9/29. + */ + +public class Utils { + + private static final char hexDigits[] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'}; + + public static String bytes2HexString(final byte[] bytes) { + if (bytes == null) return null; + int len = bytes.length; + if (len <= 0) return null; + char[] ret = new char[len << 1]; + for (int i = 0, j = 0; i < len; i++) { + ret[j++] = hexDigits[bytes[i] >>> 4 & 0x0f]; + ret[j++] = hexDigits[bytes[i] & 0x0f]; + } + return new String(ret); + } + + /** + * hash加密模板 + * + * @param data 数据 + * @param algorithm 加密算法 + * @return 密文字节数组 + */ + public static byte[] hashTemplate(final byte[] data, final String algorithm) { + if (data == null || data.length <= 0) return null; + try { + MessageDigest md = MessageDigest.getInstance(algorithm); + md.update(data); + return md.digest(); + } catch (NoSuchAlgorithmException e) { + e.printStackTrace(); + return null; + } + } + + /** + * Hmac加密模板 + * + * @param data 数据 + * @param key 秘钥 + * @param algorithm 加密算法 + * @return 密文字节数组 + */ + public static byte[] hmacTemplate(final byte[] data, final byte[] key, final String algorithm) { + if (data == null || data.length == 0 || key == null || key.length == 0) return null; + try { + SecretKeySpec secretKey = new SecretKeySpec(key, algorithm); + Mac mac = Mac.getInstance(algorithm); + mac.init(secretKey); + return mac.doFinal(data); + } catch (InvalidKeyException | NoSuchAlgorithmException e) { + e.printStackTrace(); + return null; + } + } + + public static byte[] hexString2Bytes(String hexString) { + if (isSpace(hexString)) return null; + int len = hexString.length(); + if (len % 2 != 0) { + hexString = "0" + hexString; + len = len + 1; + } + char[] hexBytes = hexString.toUpperCase().toCharArray(); + byte[] ret = new byte[len >> 1]; + for (int i = 0; i < len; i += 2) { + ret[i >> 1] = (byte) (hex2Dec(hexBytes[i]) << 4 | hex2Dec(hexBytes[i + 1])); + } + return ret; + } + + public static int hex2Dec(final char hexChar) { + if (hexChar >= '0' && hexChar <= '9') { + return hexChar - '0'; + } else if (hexChar >= 'A' && hexChar <= 'F') { + return hexChar - 'A' + 10; + } else { + throw new IllegalArgumentException(); + } + } + + public static byte[] base64Encode(final byte[] input) { + return Base64.encode(input, Base64.NO_WRAP); + } + + public static byte[] base64Decode(final byte[] input) { + return Base64.decode(input, Base64.NO_WRAP); + } + + public static boolean isSpace(final String s) { + if (s == null) return true; + for (int i = 0, len = s.length(); i < len; ++i) { + if (!Character.isWhitespace(s.charAt(i))) { + return false; + } + } + return true; + } + + /** + * DES加密模板 + * + * @param data 数据 + * @param key 秘钥 + * @param algorithm 加密算法 + * @param transformation 转变 + * @param isEncrypt {@code true}: 加密 {@code false}: 解密 + * @return 密文或者明文,适用于DES,3DES,AES + */ + public static byte[] desTemplate(final byte[] data, final byte[] key, final String algorithm, final String transformation, final boolean isEncrypt) { + if (data == null || data.length == 0 || key == null || key.length == 0) return null; + try { + SecretKeySpec keySpec = new SecretKeySpec(key, algorithm); + Cipher cipher = Cipher.getInstance(transformation); + SecureRandom random = new SecureRandom(); + cipher.init(isEncrypt ? Cipher.ENCRYPT_MODE : Cipher.DECRYPT_MODE, keySpec, random); + return cipher.doFinal(data); + } catch (Throwable e) { + e.printStackTrace(); + return null; + } + } +} diff --git a/encodeutils/src/main/java/org/forevery/EncodeUtils/EncodeUtils.java b/EncryptUtils/src/main/java/org/forevery/EncryptUtils/EncryptUtils.java similarity index 63% rename from encodeutils/src/main/java/org/forevery/EncodeUtils/EncodeUtils.java rename to EncryptUtils/src/main/java/org/forevery/EncryptUtils/EncryptUtils.java index a46153e..97d9b47 100644 --- a/encodeutils/src/main/java/org/forevery/EncodeUtils/EncodeUtils.java +++ b/EncryptUtils/src/main/java/org/forevery/EncryptUtils/EncryptUtils.java @@ -1,149 +1,149 @@ -package org.forevery.EncodeUtils; - -import org.forevery.EncodeUtils.Encode.AES.Aes_Util; -import org.forevery.EncodeUtils.Encode.DES.DES_Utils; -import org.forevery.EncodeUtils.Encode.HmacMD5.HmacMD5_Utils; -import org.forevery.EncodeUtils.Encode.HmacSHA1.HmacSHA1_Utils; -import org.forevery.EncodeUtils.Encode.HmacSHA224.HmacSHA224_Utils; -import org.forevery.EncodeUtils.Encode.HmacSHA256.HmacSHA256_Utils; -import org.forevery.EncodeUtils.Encode.HmacSHA384.HmacSHA384_Utils; -import org.forevery.EncodeUtils.Encode.HmacSHA512.HmacSHA512_Utils; -import org.forevery.EncodeUtils.Encode.MD2.MD2_Utils; -import org.forevery.EncodeUtils.Encode.MD5.MD5_Utils; -import org.forevery.EncodeUtils.Encode.RC4.RC4_Utils; -import org.forevery.EncodeUtils.Encode.SHA1.SHA1_Utils; -import org.forevery.EncodeUtils.Encode.SHA224.SHA224_Utils; -import org.forevery.EncodeUtils.Encode.SHA256.SHA256_Utils; -import org.forevery.EncodeUtils.Encode.SHA384.SHA384_Utils; -import org.forevery.EncodeUtils.Encode.SHA512.SHA512_Utils; -import org.forevery.EncodeUtils.Encode.T_3DES.T_3DES_Utils; - -/** - * Created by Forevery on 2017/9/29. - */ - -public class EncodeUtils { - - private EncodeUtils() { - throw new UnsupportedOperationException("u can't instantiate me..."); - } - - /** - * AES 加密 - */ - public static Aes_Util AES() { - return new Aes_Util(); - } - - /** - * RC4 加密 - */ - public static RC4_Utils RC4() { - return new RC4_Utils(); - } - - /** - * MD2 加密 - */ - public static MD2_Utils MD2() { - return new MD2_Utils(); - } - - /** - * MD5 加密 - */ - public static MD5_Utils MD5() { - return new MD5_Utils(); - } - - /** - * SHA1 加密 - */ - public static SHA1_Utils SHA1() { - return new SHA1_Utils(); - } - - /** - * SHA224 加密 - */ - public static SHA224_Utils SHA224() { - return new SHA224_Utils(); - } - - /** - * SHA256 加密 - */ - public static SHA256_Utils SHA256() { - return new SHA256_Utils(); - } - - /** - * SHA384 加密 - */ - public static SHA384_Utils SHA384() { - return new SHA384_Utils(); - } - - /** - * SHA512 加密 - */ - public static SHA512_Utils SHA512() { - return new SHA512_Utils(); - } - - /** - * HmacMD5 加密 - */ - public static HmacMD5_Utils HmacMD5() { - return new HmacMD5_Utils(); - } - - /** - * HmacSHA1 加密 - */ - public static HmacSHA1_Utils HmacSHA1() { - return new HmacSHA1_Utils(); - } - - /** - * HmacSHA224 加密 - */ - public static HmacSHA224_Utils HmacSHA224() { - return new HmacSHA224_Utils(); - } - - /** - * HmacSHA256 加密 - */ - public static HmacSHA256_Utils HmacSHA256() { - return new HmacSHA256_Utils(); - } - - /** - * HmacSHA384 加密 - */ - public static HmacSHA384_Utils HmacSHA384() { - return new HmacSHA384_Utils(); - } - - /** - * HmacSHA512 加密 - */ - public static HmacSHA512_Utils HmacSHA512() { - return new HmacSHA512_Utils(); - } - - /** - * DES 加密 - */ - public static DES_Utils DES() { - return new DES_Utils(); - } - - /** - * DES 加密 - */ - public static T_3DES_Utils T_3DES() { - return new T_3DES_Utils(); - } -} +package org.forevery.EncryptUtils; + +import org.forevery.EncryptUtils.Encode.AES.Aes_Util; +import org.forevery.EncryptUtils.Encode.DES.DES_Utils; +import org.forevery.EncryptUtils.Encode.HmacMD5.HmacMD5_Utils; +import org.forevery.EncryptUtils.Encode.HmacSHA1.HmacSHA1_Utils; +import org.forevery.EncryptUtils.Encode.HmacSHA224.HmacSHA224_Utils; +import org.forevery.EncryptUtils.Encode.HmacSHA256.HmacSHA256_Utils; +import org.forevery.EncryptUtils.Encode.HmacSHA384.HmacSHA384_Utils; +import org.forevery.EncryptUtils.Encode.HmacSHA512.HmacSHA512_Utils; +import org.forevery.EncryptUtils.Encode.MD2.MD2_Utils; +import org.forevery.EncryptUtils.Encode.MD5.MD5_Utils; +import org.forevery.EncryptUtils.Encode.RC4.RC4_Utils; +import org.forevery.EncryptUtils.Encode.SHA1.SHA1_Utils; +import org.forevery.EncryptUtils.Encode.SHA224.SHA224_Utils; +import org.forevery.EncryptUtils.Encode.SHA256.SHA256_Utils; +import org.forevery.EncryptUtils.Encode.SHA384.SHA384_Utils; +import org.forevery.EncryptUtils.Encode.SHA512.SHA512_Utils; +import org.forevery.EncryptUtils.Encode.T_3DES.T_3DES_Utils; + +/** + * Created by Forevery on 2017/9/29. + */ + +public class EncryptUtils { + + private EncryptUtils() { + throw new UnsupportedOperationException("u can't instantiate me..."); + } + + /** + * AES 加密 + */ + public static Aes_Util AES() { + return new Aes_Util(); + } + + /** + * RC4 加密 + */ + public static RC4_Utils RC4() { + return new RC4_Utils(); + } + + /** + * MD2 加密 + */ + public static MD2_Utils MD2() { + return new MD2_Utils(); + } + + /** + * MD5 加密 + */ + public static MD5_Utils MD5() { + return new MD5_Utils(); + } + + /** + * SHA1 加密 + */ + public static SHA1_Utils SHA1() { + return new SHA1_Utils(); + } + + /** + * SHA224 加密 + */ + public static SHA224_Utils SHA224() { + return new SHA224_Utils(); + } + + /** + * SHA256 加密 + */ + public static SHA256_Utils SHA256() { + return new SHA256_Utils(); + } + + /** + * SHA384 加密 + */ + public static SHA384_Utils SHA384() { + return new SHA384_Utils(); + } + + /** + * SHA512 加密 + */ + public static SHA512_Utils SHA512() { + return new SHA512_Utils(); + } + + /** + * HmacMD5 加密 + */ + public static HmacMD5_Utils HmacMD5() { + return new HmacMD5_Utils(); + } + + /** + * HmacSHA1 加密 + */ + public static HmacSHA1_Utils HmacSHA1() { + return new HmacSHA1_Utils(); + } + + /** + * HmacSHA224 加密 + */ + public static HmacSHA224_Utils HmacSHA224() { + return new HmacSHA224_Utils(); + } + + /** + * HmacSHA256 加密 + */ + public static HmacSHA256_Utils HmacSHA256() { + return new HmacSHA256_Utils(); + } + + /** + * HmacSHA384 加密 + */ + public static HmacSHA384_Utils HmacSHA384() { + return new HmacSHA384_Utils(); + } + + /** + * HmacSHA512 加密 + */ + public static HmacSHA512_Utils HmacSHA512() { + return new HmacSHA512_Utils(); + } + + /** + * DES 加密 + */ + public static DES_Utils DES() { + return new DES_Utils(); + } + + /** + * DES 加密 + */ + public static T_3DES_Utils T_3DES() { + return new T_3DES_Utils(); + } +} diff --git a/encodeutils/src/main/res/values/strings.xml b/EncryptUtils/src/main/res/values/strings.xml similarity index 96% rename from encodeutils/src/main/res/values/strings.xml rename to EncryptUtils/src/main/res/values/strings.xml index 255bbd6..67803ec 100644 --- a/encodeutils/src/main/res/values/strings.xml +++ b/EncryptUtils/src/main/res/values/strings.xml @@ -1,3 +1,3 @@ -