java 简单的des加密示例

1.加密结果

  包含 : 对int加密 、对string加密、对byte[]加密。

 1 10-09 18:33:32.484 7617-7617/com.example.tt.downtest D/CipherUtil: init Cipher needs 18 ms
 2 10-09 18:33:32.484 7617-7617/com.example.tt.downtest D/CipherUtil: -----=====-----------=========-----
 3 10-09 18:33:32.484 7617-7617/com.example.tt.downtest D/CipherUtil: bytes data is  [ ��    
 4                                                                     ]
 5 10-09 18:33:32.490 7617-7617/com.example.tt.downtest D/CipherUtil: encrypt ret is [ ����h�1 !m�7M��J�˝��� ] needs 5 ms
 6 10-09 18:33:32.490 7617-7617/com.example.tt.downtest D/CipherUtil: decrypt ret is [ ��    
 7                                                                     ] needs 0 ms
 8 10-09 18:33:32.490 7617-7617/com.example.tt.downtest D/CipherUtil: -----=====-----------=========-----
 9 10-09 18:33:32.490 7617-7617/com.example.tt.downtest D/CipherUtil: int data is  [ 100 ]
10 10-09 18:33:32.490 7617-7617/com.example.tt.downtest D/CipherUtil: encrypt ret is [ [��sZ"� ] needs 0 ms
11 10-09 18:33:32.490 7617-7617/com.example.tt.downtest D/CipherUtil: decrypt ret is [ 100 ] needs 0 ms
12 10-09 18:33:32.490 7617-7617/com.example.tt.downtest D/CipherUtil: -----=====-----------=========-----
13 10-09 18:33:32.491 7617-7617/com.example.tt.downtest D/CipherUtil: string data is [ hello world ]
14 10-09 18:33:32.491 7617-7617/com.example.tt.downtest D/CipherUtil: encrypt ret is [ ��́��|��@�y��y ] needs 0 ms
15 10-09 18:33:32.491 7617-7617/com.example.tt.downtest D/CipherUtil: decrypt ret is [ hello world ] needs 0 ms

2.完整示例

  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 class CipherUtil {
 20 
 21     final String TAG = "CipherUtil";
 22     long begin,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     void init(){
 31         begin = System.currentTimeMillis();
 32         try {
 33             cipher = Cipher.getInstance(algorithm);
 34             DESKeySpec keySpec = new DESKeySpec(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,byte data[]){
 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         return ret;
 70     }
 71 
 72     //DES 解密
 73     public byte[] desDecrypt(byte data[]){
 74         return des(Cipher.DECRYPT_MODE,data);
 75     }
 76     //DES 加密
 77     public byte[] desEncrypt(byte data[]){
 78         return des(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(int a) {
 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     public CipherUtil(){
 99         init();
100     }
101 
102     void testEncryptBytes(){
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     void testEncryptInt(){
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     void testEncryptString(){
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 void testMain(){
158 
159         Log.d(TAG, "-----=====-----------=========-----");
160         testEncryptBytes();
161         Log.d(TAG, "-----=====-----------=========-----");
162         testEncryptInt();
163         Log.d(TAG, "-----=====-----------=========-----");
164         testEncryptString();
165     }
166 }

 

posted @ 2017-10-09 18:33  f9q  阅读(433)  评论(0编辑  收藏  举报