-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
457 additions
and
321 deletions.
There are no files selected for viewing
16 changes: 16 additions & 0 deletions
16
EncryptUtils/src/main/java/org/forevery/EncryptUtils/AES.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package org.forevery.EncryptUtils; | ||
|
||
/** | ||
* Created by Forevery on 2017/9/30. | ||
*/ | ||
|
||
public class AES { | ||
|
||
public final static int CBC = 1; | ||
public final static int CFB = 2; | ||
public final static int ECB = 3; | ||
public final static int OFB = 4; | ||
|
||
public final static String PKCS5Padding = "1"; | ||
public final static String PKCS7Padding = "2"; | ||
} |
16 changes: 16 additions & 0 deletions
16
EncryptUtils/src/main/java/org/forevery/EncryptUtils/Encrypt/AES/AES_BASE.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package org.forevery.EncryptUtils.Encrypt.AES; | ||
|
||
/** | ||
* Created by Forevery on 2017/9/30. | ||
*/ | ||
|
||
public interface AES_BASE { | ||
|
||
public String Encrypt(String data, String password); | ||
|
||
public String Decrypt(String data, String password); | ||
|
||
public String Decrypt(String data, String password, String iv); | ||
|
||
public String Encrypt(String data, String password, String iv); | ||
} |
186 changes: 186 additions & 0 deletions
186
EncryptUtils/src/main/java/org/forevery/EncryptUtils/Encrypt/AES/AES_ECB_Utils.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,186 @@ | ||
package org.forevery.EncryptUtils.Encrypt.AES; | ||
|
||
/** | ||
* Created by Forevery on 2017/5/21. | ||
*/ | ||
|
||
import java.io.UnsupportedEncodingException; | ||
|
||
import javax.crypto.Cipher; | ||
import javax.crypto.spec.SecretKeySpec; | ||
|
||
/** | ||
* AES加密解密算法 | ||
* | ||
* @author long | ||
*/ | ||
public class AES_ECB_Utils implements AES_BASE { | ||
private final String CipherMode; | ||
|
||
public AES_ECB_Utils(String complement) { | ||
StringBuilder comp = new StringBuilder(); | ||
switch (complement) { | ||
case "1": | ||
comp.append("PKCS5Padding"); | ||
break; | ||
case "2": | ||
comp.append("PKCS7Padding"); | ||
break; | ||
} | ||
this.CipherMode = "AES/ECB/" + comp.toString(); | ||
} | ||
|
||
// /** 创建密钥 **/ | ||
private SecretKeySpec createKey(String key) { | ||
byte[] data = null; | ||
if (key == null) { | ||
key = ""; | ||
} | ||
StringBuffer sb = new StringBuffer(16); | ||
sb.append(key); | ||
while (sb.length() < 16) { | ||
sb.append("0"); | ||
} | ||
if (sb.length() > 16) { | ||
sb.setLength(16); | ||
} | ||
|
||
try { | ||
data = sb.toString().getBytes("UTF-8"); | ||
} catch (UnsupportedEncodingException e) { | ||
e.printStackTrace(); | ||
} | ||
return new SecretKeySpec(data, "AES"); | ||
} | ||
|
||
|
||
// /** 加密字节数据 **/ | ||
private byte[] encrypt(byte[] content, String password) { | ||
try { | ||
SecretKeySpec key = createKey(password); | ||
Cipher cipher = Cipher.getInstance(CipherMode); | ||
cipher.init(Cipher.ENCRYPT_MODE, key); | ||
byte[] result = cipher.doFinal(content); | ||
return result; | ||
} catch (Exception e) { | ||
e.printStackTrace(); | ||
} | ||
return null; | ||
} | ||
|
||
// /** 加密(结果为16进制字符串) **/ | ||
public String Encrypt(String string, String password) { | ||
byte[] data = null; | ||
try { | ||
data = string.getBytes("UTF-8"); | ||
} catch (Exception e) { | ||
e.printStackTrace(); | ||
} | ||
data = encrypt(data, password); | ||
String result = byte2hex(data); | ||
return result; | ||
} | ||
|
||
// /** 解密字节数组 **/ | ||
private byte[] decrypt(byte[] content, String password) { | ||
try { | ||
SecretKeySpec key = createKey(password); | ||
Cipher cipher = Cipher.getInstance(CipherMode); | ||
cipher.init(Cipher.DECRYPT_MODE, key); | ||
byte[] result = cipher.doFinal(content); | ||
return result; | ||
} catch (Exception e) { | ||
e.printStackTrace(); | ||
} | ||
return null; | ||
} | ||
|
||
// /** 解密 **/ | ||
public String Decrypt(String string, String password) { | ||
byte[] data = null; | ||
try { | ||
data = hex2byte(string); | ||
} catch (Exception e) { | ||
e.printStackTrace(); | ||
} | ||
data = decrypt(data, password); | ||
if (data == null) | ||
return null; | ||
String result = null; | ||
try { | ||
result = new String(data, "UTF-8"); | ||
} catch (UnsupportedEncodingException e) { | ||
e.printStackTrace(); | ||
} | ||
return result; | ||
} | ||
|
||
@Override | ||
public String Decrypt(String string, String password, String iv) { | ||
byte[] data = null; | ||
try { | ||
data = hex2byte(string); | ||
} catch (Exception e) { | ||
e.printStackTrace(); | ||
} | ||
data = decrypt(data, password); | ||
if (data == null) | ||
return null; | ||
String result = null; | ||
try { | ||
result = new String(data, "UTF-8"); | ||
} catch (UnsupportedEncodingException e) { | ||
e.printStackTrace(); | ||
} | ||
return result; | ||
} | ||
|
||
@Override | ||
public String Encrypt(String string, String password, String iv) { | ||
byte[] data = null; | ||
try { | ||
data = string.getBytes("UTF-8"); | ||
} catch (Exception e) { | ||
e.printStackTrace(); | ||
} | ||
data = encrypt(data, password); | ||
String result = byte2hex(data); | ||
return result; | ||
} | ||
|
||
private static String byte2hex(byte[] b) { | ||
StringBuilder hs = new StringBuilder(); | ||
String stmp; | ||
for (int i = 0; i < b.length; i++) { | ||
stmp = Integer.toHexString(b[i] & 0xFF).toUpperCase(); | ||
if (stmp.length() == 1) { | ||
hs.append("0").append(stmp); | ||
} else { | ||
hs.append(stmp); | ||
} | ||
} | ||
return hs.toString(); | ||
} | ||
|
||
/** | ||
* 十六进制byte数组转二进制byte数组 | ||
* hex to byte array | ||
* | ||
* @param hex hex string | ||
* @return byte array | ||
*/ | ||
private static byte[] hex2byte(String hex) | ||
throws IllegalArgumentException { | ||
if (hex.length() % 2 != 0) { | ||
throw new IllegalArgumentException("invalid hex string"); | ||
} | ||
char[] arr = hex.toCharArray(); | ||
byte[] b = new byte[hex.length() / 2]; | ||
for (int i = 0, j = 0, l = hex.length(); i < l; i++, j++) { | ||
String swap = "" + arr[i++] + arr[i]; | ||
int byteint = Integer.parseInt(swap, 16) & 0xFF; | ||
b[j] = new Integer(byteint).byteValue(); | ||
} | ||
return b; | ||
} | ||
} |
Oops, something went wrong.