Skip to content

Commit 84668af

Browse files
author
xqs
committed
aes
1 parent a1ef9e5 commit 84668af

File tree

1 file changed

+32
-25
lines changed

1 file changed

+32
-25
lines changed

src/main/java/com/devil/des/DESCoder.java

Lines changed: 32 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,14 @@
2020
import javax.crypto.SecretKeyFactory;
2121
import javax.crypto.spec.DESKeySpec;
2222
import javax.crypto.spec.IvParameterSpec;
23-
import javax.crypto.spec.SecretKeySpec;
2423

2524
import org.dom4j.Document;
2625
import org.dom4j.Element;
2726
import org.dom4j.io.OutputFormat;
2827
import org.dom4j.io.SAXReader;
2928
import org.dom4j.io.XMLWriter;
3029

31-
import com.devil.utils.Base64Coder;
30+
import com.devil.utils.CommUtil;
3231

3332
/*
3433
* @Author xingqisheng
@@ -43,43 +42,53 @@ public class DESCoder {
4342
// private byte[] paramvector = { 1, 2, 3, 4, 5, 6, 7, 8 };
4443
private byte[] paramvector = { 1, 2, 3, 4, 5, 6, 7, 8 };
4544

46-
public DESCoder(byte[] key) {
47-
this.key = key;
48-
paramvector = Arrays.copyOfRange(key, 0, 8);
45+
private static DESCoder defaultInstance;
46+
47+
public static DESCoder getDefault() {
48+
if (defaultInstance == null) {
49+
defaultInstance = new DESCoder();
50+
}
51+
return defaultInstance;
4952
}
5053

51-
public DESCoder() {
54+
private DESCoder() {
5255
this.key = this.getPrivateKey();
5356
paramvector = Arrays.copyOfRange(key, 0, 8);
5457
}
5558

59+
public DESCoder(byte[] key) {
60+
this.key = key;
61+
paramvector = Arrays.copyOfRange(key, 0, 8);
62+
}
63+
5664
public static void main(String[] args) {
57-
// String src = "guessyourheart";
58-
// System.out.println("原文是:" + src);
59-
// byte[] key = "guessyourheart".getBytes(Charset.forName("UTF-8"));
60-
String src ="https://raw.github.com/configs/configs/master/guessyourheart";
65+
// String src = "guessyourheart";
66+
// System.out.println("原文是:" + src);
67+
// byte[] key = "guessyourheart".getBytes(Charset.forName("UTF-8"));
68+
String src = "https://raw.github.com/configs/configs/master/guessyourheart";
6169
System.out.println("原文是:" + src);
6270
byte[] key = "12345678".getBytes(Charset.forName("UTF-8"));
63-
DESCoder coder = new DESCoder(key);
71+
DESCoder coder = new DESCoder();
6472
String cypher = coder.encrypt(src.getBytes());
6573
System.out.println("加密后:" + new String(cypher));
6674
byte[] plain = coder.decrypt(cypher);
6775
System.out.println("解密后:" + new String(plain));
6876

6977
// System.out.println("======测试xml文件加密=======");
7078
// String testElName="username";
71-
// coder.encryptXml("C:\\Documents and Settings\\Administrator\\v3\\conf\\v3-taobao-config.xml");
79+
// coder.encryptXml("C:\\Documents and
80+
// Settings\\Administrator\\v3\\conf\\v3-taobao-config.xml");
7281
// System.out.println("xml原文是:"+XmlUtil.getDocument("d:\\xx.xml").getRootElement().elementText(testElName));
7382
// Document doc = coder.decryptXml("d:\\xx_new.xml");
7483
// System.out.println("xml解密后:"+doc.getRootElement().elementText(testElName));
7584
}
7685

7786
public String encrypt(byte[] src) {
78-
return Base64Coder.encode(this.desEncrypt(src)).replace("\n", "");
87+
return CommUtil.encBase64Url(this.desEncrypt(src));
7988
}
8089

8190
public byte[] decrypt(String src) {
82-
return this.desDecrypt(Base64Coder.decode(src));
91+
return this.desDecrypt(CommUtil.decBase64Url(src));
8392
}
8493

8594
// 作为一个工具使用
@@ -108,13 +117,17 @@ private byte[] getPrivateKey() {
108117
byte[] result = null;
109118
try {
110119
byte[] bArr = { 100, 101, 118, 105, 108, 105, 118, 101, 100 };
111-
SecureRandom secureRandom = new SecureRandom(bArr);
120+
SecureRandom secureRandom = SecureRandom.getInstance("SHA1PRNG");
121+
secureRandom.setSeed(bArr);
122+
123+
// SecureRandom secureRandom = new SecureRandom(bArr);
112124
KeyGenerator kg = null;
113125
kg = KeyGenerator.getInstance(ALGORITHM);
114126
kg.init(secureRandom);
115127

116128
SecretKey secretKey = kg.generateKey();
117129
result = secretKey.getEncoded();
130+
System.out.println(Arrays.toString(result));
118131
} catch (NoSuchAlgorithmException e) {
119132
e.printStackTrace();
120133
}
@@ -174,8 +187,7 @@ protected static Document getDocument(String path) {
174187
if (f.exists()) {
175188
SAXReader reader = new SAXReader();
176189
try {
177-
doc = reader.read(new InputStreamReader(new FileInputStream(f),
178-
"UTF-8"));
190+
doc = reader.read(new InputStreamReader(new FileInputStream(f), "UTF-8"));
179191
} catch (Exception e) {
180192
System.out.println("读入Xml文件" + path + "有误");
181193
e.printStackTrace();
@@ -188,8 +200,7 @@ protected static void writeDocument(Document doc, String destPah) {
188200
OutputFormat format = OutputFormat.createPrettyPrint();
189201
XMLWriter writer = null;
190202
try {
191-
writer = new XMLWriter(new OutputStreamWriter(new FileOutputStream(
192-
destPah), "UTF-8"), format);
203+
writer = new XMLWriter(new OutputStreamWriter(new FileOutputStream(destPah), "UTF-8"), format);
193204
writer.write(doc);
194205
} catch (Exception e) {
195206
} finally {
@@ -207,9 +218,7 @@ protected static void writeDocument(Document doc, String destPah) {
207218
protected static void encryptElement(Element e, DESCoder coder) {
208219
String text = e.getTextTrim();
209220
if (text != null && text.length() > 0) {
210-
if (e.attribute("encrypt") != null
211-
&& "true"
212-
.equalsIgnoreCase(e.attribute("encrypt").getText())) {
221+
if (e.attribute("encrypt") != null && "true".equalsIgnoreCase(e.attribute("encrypt").getText())) {
213222
try {
214223
text = coder.encrypt(text.getBytes("UTF-8"));
215224
e.setText(text);
@@ -231,9 +240,7 @@ protected static void encryptElement(Element e, DESCoder coder) {
231240
protected static void decryptElement(Element e, DESCoder coder) {
232241
String text = e.getTextTrim();
233242
if (text != null && text.length() > 0) {
234-
if (e.attribute("encrypt") != null
235-
&& "true"
236-
.equalsIgnoreCase(e.attribute("encrypt").getText())) {
243+
if (e.attribute("encrypt") != null && "true".equalsIgnoreCase(e.attribute("encrypt").getText())) {
237244
try {
238245
text = new String(coder.decrypt(text), "UTF-8");
239246
e.setText(text);

0 commit comments

Comments
 (0)