Skip to content

Commit

Permalink
add AES and upgrade pom version
Browse files Browse the repository at this point in the history
  • Loading branch information
KenelLiu authored and KenelLiu committed Oct 17, 2017
1 parent e325f91 commit e16b34a
Show file tree
Hide file tree
Showing 7 changed files with 63 additions and 27 deletions.
4 changes: 2 additions & 2 deletions CDOFramework/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@
<parent>
<groupId>cdo</groupId>
<artifactId>cdo-parent</artifactId>
<version>2.0.9</version>
<version>2.1.0</version>
</parent>
<artifactId>cdoframework</artifactId>
<version>2.0.9</version>
<version>2.1.0</version>
<packaging>jar</packaging>

<name>cdoFramework</name>
Expand Down
4 changes: 2 additions & 2 deletions CDOLib/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@
<parent>
<groupId>cdo</groupId>
<artifactId>cdo-parent</artifactId>
<version>2.0.9</version>
<version>2.1.0</version>
</parent>

<artifactId>cdolib</artifactId>
<version>2.0.9</version>
<version>2.1.0</version>
<packaging>jar</packaging>

<name>cdolib</name>
Expand Down
6 changes: 3 additions & 3 deletions CDOUtil/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@
<parent>
<groupId>cdo</groupId>
<artifactId>cdo-parent</artifactId>
<version>2.0.9</version>
<version>2.1.0</version>
</parent>

<artifactId>cdoUtil</artifactId>
<version>2.0.9</version>
<version>2.1.0</version>
<packaging>jar</packaging>

<name>cdoUtil</name>
Expand Down Expand Up @@ -43,7 +43,7 @@
<groupId>org.bouncycastle</groupId>
<artifactId>bcprov-jdk16</artifactId>
<version>1.46</version>
</dependency>
</dependency>
<!-- sftp over ssh-->
<dependency>
<groupId>com.jcraft</groupId>
Expand Down
12 changes: 8 additions & 4 deletions CDOUtil/src/main/java/com/cdo/util/codec/AES.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,12 @@
import javax.crypto.KeyGenerator;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.SecretKey;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;

import com.cdo.util.exception.EncryptException;
import com.sun.crypto.provider.AESKeyGenerator;

public class AES {
/**
* 密钥算法
Expand All @@ -35,7 +39,7 @@ public static byte[] decrypt(byte[] data, byte[] key) throws GeneralSecurityExc
// 还原密钥
Key k = new SecretKeySpec(key, KEY_ALGORITHM);
Cipher cipher = Cipher.getInstance(CIPHER_ALGORITHM);
// 初始化,设置为解密模式
// 初始化,设置为解密模式
cipher.init(Cipher.DECRYPT_MODE, k);
// 执行操作
return cipher.doFinal(data);
Expand All @@ -55,13 +59,13 @@ public static byte[] encrypt(byte[] data, byte[] key) throws GeneralSecurityExce

// 还原密钥
Key k = new SecretKeySpec(key, KEY_ALGORITHM);

Cipher cipher = Cipher.getInstance(CIPHER_ALGORITHM);

// 初始化,设置为加密模式
cipher.init(Cipher.ENCRYPT_MODE, k);
// 执行操作
return cipher.doFinal(data);

}


Expand All @@ -76,7 +80,7 @@ public static byte[] initKey() throws GeneralSecurityException{
*/
public static byte[] initKey(int size) throws GeneralSecurityException {
// 实例化
KeyGenerator kg = KeyGenerator.getInstance(KEY_ALGORITHM);
KeyGenerator kg = KeyGenerator.getInstance(KEY_ALGORITHM,"BC");
/*
* AES 要求密钥长度为 128位、192位或 256位
*/
Expand Down
56 changes: 44 additions & 12 deletions CDOUtil/src/main/java/com/cdo/util/codec/TokenUtil.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
package com.cdo.util.codec;
import java.math.BigInteger;
import java.net.URLEncoder;
import java.security.AlgorithmParameterGenerator;
import java.security.interfaces.RSAPrivateKey;
import java.security.interfaces.RSAPublicKey;
import java.util.concurrent.TimeoutException;

import org.apache.commons.codec.binary.Base64;
import org.apache.commons.codec.binary.Hex;
import org.bouncycastle.util.encoders.UrlBase64;

import com.cdo.util.exception.EncryptException;

public class TokenUtil {
Expand Down Expand Up @@ -90,19 +95,46 @@ public static void main(String[] args) throws Exception {
System.out.println(" pubKey:"+pubKey);
**/
//--------RSA 算法------------------//
String strId=2013122911111300L+"ADSFA"+System.currentTimeMillis();
String s=genRSAEncrypt(strId+"");
System.out.println(s+","+s.length());
System.out.println(genRSADecrypt(s));


String apiKey = "LTMXW2OkF0XG5Qx2TlKWIA;"+System.currentTimeMillis();
System.err.println("原文:\t" + apiKey);
long start=System.currentTimeMillis();
String encode=null;
String decode=null;
for(int i=0;i<3;i++){
start=System.currentTimeMillis();
encode=genRSAEncrypt(apiKey);
System.err.println("RSA 加密后:\t" + encode+",length="+encode.length());
System.err.println("RSA 加密时间:\t" +(System.currentTimeMillis()-start));

start=System.currentTimeMillis();
decode=genRSADecrypt(encode);
System.err.println("RSA 解密后:\t" + decode);
System.err.println("RSA 加解密时间:\t" + (System.currentTimeMillis()-start));
System.err.println("\r\n");
}

//---------AES 算法----------//
byte[] AES_Key=AES.initKey();
String apiKey = "LTMXW2OkF0XG5Qx2TlKWIA";
System.err.println("原文:\t" + apiKey);
String encode=genAESEncrypt(apiKey,AES_Key);
System.err.println("加密后:\t" + encode+",length="+encode.length());
String decode=genAESDecrypt(encode,AES_Key);
System.err.println("解密后:\t" + decode);
// byte[] AES_Key=AES.initKey();
byte[] AES_Key=Base64.decodeBase64("mKG9N3J40uC0eWoEOEfqyw==");
System.err.println("ASE 密钥:\t" + Base64.encodeBase64String(AES_Key));
for(int i=0;i<3;i++){
start=System.currentTimeMillis();
encode=genAESEncrypt(apiKey,AES_Key);
String base64Str=Base64.encodeBase64URLSafeString(encode.getBytes());
System.out.println("AES BASE64URL 加密后:\t"+base64Str+",length="+base64Str.length());
System.out.println("AES 加密时间:\t" + (System.currentTimeMillis()-start));

start=System.currentTimeMillis();
base64Str=new String(Base64.decodeBase64(base64Str));
decode=genAESDecrypt(base64Str,AES_Key);
System.out.println("AES BASE64URL 解密后:\t" + decode);
System.out.println("AES 解密时间:\t" + (System.currentTimeMillis()-start));
System.out.println("\r\n");
}
// AlgorithmParameterGenerator alg=AlgorithmParameterGenerator.getInstance("AES");
// alg.init(56);
// System.out.println(new BigInteger(alg.generateParameters().getEncoded()).toString());

}
}
4 changes: 2 additions & 2 deletions CDOWeb/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@
<parent>
<groupId>cdo</groupId>
<artifactId>cdo-parent</artifactId>
<version>2.0.9</version>
<version>2.1.0</version>
</parent>

<artifactId>cdoweb</artifactId>
<version>2.0.9</version>
<version>2.1.0</version>
<packaging>jar</packaging>
<name>cdoweb</name>

Expand Down
4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
<modelVersion>4.0.0</modelVersion>
<groupId>cdo</groupId>
<artifactId>cdo-parent</artifactId>
<version>2.0.9</version>
<version>2.1.0</version>
<packaging>pom</packaging>
<properties>
<project.cdo.version>2.0.9</project.cdo.version>
<project.cdo.version>2.1.0</project.cdo.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

Expand Down

0 comments on commit e16b34a

Please sign in to comment.