1 package com.example.tt.downtest;2
3
4 import android.util.Log;5
6 import java.security.InvalidKeyException;7 import java.security.NoSuchAlgorithmException;8 import java.security.spec.InvalidKeySpecException;9
10 import javax.crypto.BadPaddingException;11 import javax.crypto.Cipher;12 import javax.crypto.IllegalBlockSizeException;13 import javax.crypto.NoSuchPaddingException;14 import javax.crypto.SecretKey;15 import javax.crypto.SecretKeyFactory;16 import javax.crypto.spec.DESKeySpec;17
18
19 public classCipherUtil {20
21 final String TAG = "CipherUtil";22 longbegin,end;23
24
25 String algorithm = "DES";26 Cipher cipher;27 SecretKey secretKey;28 byte key[] = new byte[]{'c','B','z',2,'b','y','d',8};29
30 voidinit(){31 begin =System.currentTimeMillis();32 try{33 cipher =Cipher.getInstance(algorithm);34 DESKeySpec keySpec = newDESKeySpec(key);35 SecretKeyFactory keyFactory =SecretKeyFactory.getInstance(algorithm);36 secretKey =keyFactory.generateSecret(keySpec);37 } catch(NoSuchAlgorithmException e) {38 e.printStackTrace();39 } catch(NoSuchPaddingException e) {40 e.printStackTrace();41 } catch(InvalidKeyException e) {42 e.printStackTrace();43 } catch(InvalidKeySpecException e) {44 e.printStackTrace();45 }46 end =System.currentTimeMillis();47 Log.d(TAG, "init Cipher needs" + (end - begin) + "ms");48 }49
50 byte[] des(int mode,bytedata[]){51
52 byte[] ret = null;53 //加密的内容存在并且密钥存在且长度为8个字节
54 if (data != null && data.length > 0) {55 try{56 //Cipher.DECRYPT_MODE 解密57 //Cipher.ENCRYPT_MODE 加密
58 cipher.init(mode, secretKey);59 ret =cipher.doFinal(data);60
61 } catch(IllegalBlockSizeException e) {62 e.printStackTrace();63 } catch(BadPaddingException e) {64 e.printStackTrace();65 } catch(InvalidKeyException e) {66 e.printStackTrace();67 }68 }69 returnret;70 }71
72 //DES 解密
73 public byte[] desDecrypt(bytedata[]){74 returndes(Cipher.DECRYPT_MODE,data);75 }76 //DES 加密
77 public byte[] desEncrypt(bytedata[]){78 returndes(Cipher.ENCRYPT_MODE,data);79 }80
81 //byte 数组与 int 的相互转换
82 public static int byteArrayToInt(byte[] b) {83 return b[3] & 0xFF |
84 (b[2] & 0xFF) << 8 |
85 (b[1] & 0xFF) << 16 |
86 (b[0] & 0xFF) << 24;87 }88
89 public static byte[] intToByteArray(inta) {90 return new byte[] {91 (byte) ((a >> 24) & 0xFF),92 (byte) ((a >> 16) & 0xFF),93 (byte) ((a >> 8) & 0xFF),94 (byte) (a & 0xFF)95 };96 }97
98 publicCipherUtil(){99 init();100 }101
102 voidtestEncryptBytes(){103 byte data[] = new byte[16];104 for (int i = 0;i < data.length;++i){105 data[i] = (byte) i;106 }107 Log.d(TAG, "bytes data is [" + new String(data) + "]");108
109 begin =System.currentTimeMillis();110 byte encrypt[] =desEncrypt(data);111 end =System.currentTimeMillis();112 Log.d(TAG, "encrypt ret is [" + new String(encrypt) + "] needs" + (end - begin) + "ms");113
114 begin =System.currentTimeMillis();115 byte decrypt[] =desDecrypt(encrypt);116 end =System.currentTimeMillis();117
118 Log.d(TAG, "decrypt ret is [" + new String(decrypt) + "] needs" + (end - begin) + "ms");119
120 }121 voidtestEncryptInt(){122 int data = 100;123 byte bytes[] =intToByteArray(data);124 Log.d(TAG, "int data is [" + data + "]");125
126 begin =System.currentTimeMillis();127 byte encrypt[] =desEncrypt(bytes);128 end =System.currentTimeMillis();129 Log.d(TAG, "encrypt ret is [" + new String(encrypt) + "] needs" + (end - begin) + "ms");130
131 begin =System.currentTimeMillis();132 byte decrypt[] =desDecrypt(encrypt);133 end =System.currentTimeMillis();134
135 Log.d(TAG, "decrypt ret is [" + byteArrayToInt(decrypt) + "] needs" + (end - begin) + "ms");136
137 }138 voidtestEncryptString(){139 String str = "hello world";140 byte data[] =str.getBytes();141 Log.d(TAG, "string data is [" + str + "]");142
143 begin =System.currentTimeMillis();144 byte encrypt[] =desEncrypt(data);145 end =System.currentTimeMillis();146 Log.d(TAG, "encrypt ret is [" + new String(encrypt) + "] needs" + (end - begin) + "ms");147
148
149 begin =System.currentTimeMillis();150 byte decrypt[] =desDecrypt(encrypt);151 end =System.currentTimeMillis();152
153 Log.d(TAG, "decrypt ret is [" + new String(decrypt) + "] needs" + (end - begin) + "ms");154
155 }156
157 public voidtestMain(){158
159 Log.d(TAG, "-----=====-----------=========-----");160 testEncryptBytes();161 Log.d(TAG, "-----=====-----------=========-----");162 testEncryptInt();163 Log.d(TAG, "-----=====-----------=========-----");164 testEncryptString();165 }166 }