1.加密结果

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

 10-09 18:33:32.484 7617-7617/com.example.tt.downtest D/CipherUtil: init Cipher needs 18 ms
 10-09 18:33:32.484 7617-7617/com.example.tt.downtest D/CipherUtil: -----=====-----------=========-----
 10-09 18:33:32.484 7617-7617/com.example.tt.downtest D/CipherUtil: bytes data is  [ ��
                                                                     ]
 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
 10-09 18:33:32.490 7617-7617/com.example.tt.downtest D/CipherUtil: decrypt ret is [ ��
                                                                     ] needs 0 ms
 10-09 18:33:32.490 7617-7617/com.example.tt.downtest D/CipherUtil: -----=====-----------=========-----
 10-09 18:33:32.490 7617-7617/com.example.tt.downtest D/CipherUtil: int data is  [ 100 ]
 10-09 18:33:32.490 7617-7617/com.example.tt.downtest D/CipherUtil: encrypt ret is [ [��sZ"� ] needs 0 ms
 10-09 18:33:32.490 7617-7617/com.example.tt.downtest D/CipherUtil: decrypt ret is [ 100 ] needs 0 ms
 10-09 18:33:32.490 7617-7617/com.example.tt.downtest D/CipherUtil: -----=====-----------=========-----
 10-09 18:33:32.491 7617-7617/com.example.tt.downtest D/CipherUtil: string data is [ hello world ]
 10-09 18:33:32.491 7617-7617/com.example.tt.downtest D/CipherUtil: encrypt ret is [ ��́��|��@�y��y ] needs 0 ms
 10-09 18:33:32.491 7617-7617/com.example.tt.downtest D/CipherUtil: decrypt ret is [ hello world ] needs 0 ms

2.完整示例

 package com.example.tt.downtest;

 import android.util.Log;

 import java.security.InvalidKeyException;
 import java.security.NoSuchAlgorithmException;
 import java.security.spec.InvalidKeySpecException;

 import javax.crypto.BadPaddingException;
 import javax.crypto.Cipher;
 import javax.crypto.IllegalBlockSizeException;
 import javax.crypto.NoSuchPaddingException;
 import javax.crypto.SecretKey;
 import javax.crypto.SecretKeyFactory;
 import javax.crypto.spec.DESKeySpec;

 public class CipherUtil {

     final String TAG = "CipherUtil";
     long begin,end;

     String algorithm = "DES";
     Cipher cipher;
     SecretKey secretKey;
     ,};

     void init(){
         begin = System.currentTimeMillis();
         try {
             cipher = Cipher.getInstance(algorithm);
             DESKeySpec keySpec = new DESKeySpec(key);
             SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(algorithm);
             secretKey = keyFactory.generateSecret(keySpec);
         } catch (NoSuchAlgorithmException e) {
             e.printStackTrace();
         } catch (NoSuchPaddingException e) {
             e.printStackTrace();
         } catch (InvalidKeyException e) {
             e.printStackTrace();
         } catch (InvalidKeySpecException e) {
             e.printStackTrace();
         }
         end = System.currentTimeMillis();
         Log.d(TAG, "init Cipher needs " + (end - begin) + " ms");
     }

     byte[] des(int mode,byte data[]){

         byte[] ret = null;
         //加密的内容存在并且密钥存在且长度为8个字节
         ) {
             try {
                 //Cipher.DECRYPT_MODE 解密
                 //Cipher.ENCRYPT_MODE 加密
                 cipher.init(mode, secretKey);
                 ret = cipher.doFinal(data);

             } catch (IllegalBlockSizeException e) {
                 e.printStackTrace();
             } catch (BadPaddingException e) {
                 e.printStackTrace();
             } catch (InvalidKeyException e) {
                 e.printStackTrace();
             }
         }
         return ret;
     }

     //DES 解密
     public byte[] desDecrypt(byte data[]){
         return des(Cipher.DECRYPT_MODE,data);
     }
     //DES 加密
     public byte[] desEncrypt(byte data[]){
         return des(Cipher.ENCRYPT_MODE,data);
     }

     //byte 数组与 int 的相互转换
     public static int byteArrayToInt(byte[] b) {
         ] & 0xFF        |
                 (b[] &   |
                 (b[] &  |
                 (b[] &  ;
     }

     public static byte[] intToByteArray(int a) {
         return new byte[] {
                 ()   & 0xFF),
                 ()   & 0xFF),
                 ()    & 0xFF),
                 (byte) (a           & 0xFF)
         };
     }

     public CipherUtil(){
         init();
     }

     void testEncryptBytes(){
         ];
         ;i < data.length;++i){
             data[i] = (byte) i;
         }
         Log.d(TAG, "bytes data is  [ " + new String(data) + " ]");

         begin = System.currentTimeMillis();
         byte encrypt[] = desEncrypt(data);
         end = System.currentTimeMillis();
         Log.d(TAG, "encrypt ret is [ " + new String(encrypt) + " ] needs " + (end - begin) + " ms");

         begin = System.currentTimeMillis();
         byte decrypt[] = desDecrypt(encrypt);
         end = System.currentTimeMillis();

         Log.d(TAG, "decrypt ret is [ " + new String(decrypt) + " ] needs " + (end - begin) + " ms");

     }
     void testEncryptInt(){
         ;
         byte bytes[] = intToByteArray(data);
         Log.d(TAG, "int data is  [ " + data + " ]");

         begin = System.currentTimeMillis();
         byte encrypt[] = desEncrypt(bytes);
         end = System.currentTimeMillis();
         Log.d(TAG, "encrypt ret is [ " + new String(encrypt) + " ] needs " + (end - begin) + " ms");

         begin = System.currentTimeMillis();
         byte decrypt[] = desDecrypt(encrypt);
         end = System.currentTimeMillis();

         Log.d(TAG, "decrypt ret is [ " + byteArrayToInt(decrypt) + " ] needs " + (end - begin) + " ms");

     }
     void testEncryptString(){
         String str = "hello world";
         byte data[] = str.getBytes();
         Log.d(TAG, "string data is [ " + str + " ]");

         begin = System.currentTimeMillis();
         byte encrypt[] = desEncrypt(data);
         end = System.currentTimeMillis();
         Log.d(TAG, "encrypt ret is [ " + new String(encrypt) + " ] needs " + (end - begin) + " ms");

         begin = System.currentTimeMillis();
         byte decrypt[] = desDecrypt(encrypt);
         end = System.currentTimeMillis();

         Log.d(TAG, "decrypt ret is [ " + new String(decrypt) + " ] needs " + (end - begin) + " ms");

     }

     public void testMain(){

         Log.d(TAG, "-----=====-----------=========-----");
         testEncryptBytes();
         Log.d(TAG, "-----=====-----------=========-----");
         testEncryptInt();
         Log.d(TAG, "-----=====-----------=========-----");
         testEncryptString();
     }
 }

java 简单的des加密示例的更多相关文章

  1. Java与.NET DES加密解密互转

    上代码: Java代码: import javax.crypto.Cipher; import javax.crypto.SecretKey; import javax.crypto.SecretKe ...

  2. 【Java】通过DES加密和解密工具,对字符串进行加密和解密操作

    分享一个非常不错的字符串加密和解密的程序. 可以指定不同的密钥对同一字符串进行不同的加密操作,增强加密性能. Java代码如下: package com.app; import java.securi ...

  3. PHP 识别 java 8位 des 加密和 解密方式

    代码及使用说明: <?php /** *PHP 识别 java 8位密钥的加密和解密方式 *@desc 加密方式 通用 */ class DES { var $key; var $iv; //偏 ...

  4. java简单实现MD5加密

    1.话不多说,直接上代码-----传入字符串,返回加密码 import java.security.MessageDigest; import java.text.NumberFormat; publ ...

  5. 关于Objective-c和Java下DES加密保持一致的方式

    转载自:http://www.cnblogs.com/janken/archive/2012/04/05/2432930.html 最近做了一个移动项目,是有服务器和客户端类型的项目,客户端是要登录才 ...

  6. C# DES加密类,16位的加密。

    这个加密类是与java写的DES加密不同时,自己写的,最后与Java的加密相同了,解决了加密后不同的问题. 可以直接调用里面的加密和解密的方法. using System; using System. ...

  7. JAVA实现DES加密实现详解

    package util; import java.security.SecureRandom; import javax.crypto.spec.DESKeySpec; import javax.c ...

  8. JAVA实现DES加密

    DES加密介绍       DES是一种对称加密算法,所谓对称加密算法即:加密和解密使用相同密钥的算法.DES加密算法出自IBM的研究,后来被美国政府正式采用,之后开始广泛流传,但是近些年使用越来越少 ...

  9. IOS、java支持DES加密

    转载请注明博客地址:http://blog.csdn.net/mengxiangyue/article/details/40015727 近期在考虑数据加密方面的需求,所以对数据加密简单的看了一下,当 ...

随机推荐

  1. Python基础入门-实现猜数字小游戏

    今天呢,我们来通过前面学过的一些知识点来完成一个猜数字大小的游戏程序设计.那么呢,一般人写代码直接上来就干,没有分析,这样的做法是没有产出的,除非你是大牛,今天呢,我会把我学习编程的思路分享给大家,我 ...

  2. 基本滤波算法比较 (转载http://blog.sina.com.cn/s/blog_69f2aa5a01014du5.html)

    最近在做关于数据采集方面的东西,这就不免涉及到了滤波的算法,在网上找到了关于几种算法的比较. 数字滤波方法有很多种,每种方法有其不同的特点和使用范围.从大的范围可分为3类. 1.克服大脉冲干扰的数字滤 ...

  3. 软工作业1:wc.exe项目开发(java)

    Github地址:https://github.com/Zzhaomin/learngit 项目相关要求 : wc.exe 是一个常见的工具,它能统计文本文件的字符数.单词数和行数.这个项目要求写一个 ...

  4. IOC AOP 设计模式

    IOC AOP 不是什么技术而是一种设计模式  学习 IOC AOP 其实是在学习一种思想. 1.IOC IOC其实是 将对象的创建和获取提取到外部.由外部IOC容器提供需要的组件. 看下面代码: p ...

  5. android获取USB设备的名称

    1.注释内 .是三星设备可能不支持,需要更换的代码. 2.mUsbManager.是getSystemService(Context.USB_SERVICE)获的. 3. 从stackoverflow ...

  6. 新的云主机 python 创建虚拟环境

    1.为什么要搭建虚拟环境? 问题:如果在一台电脑上, 想开发多个不同的项目, 需要用到同一个包的不同版本, 如果使用上面的命令, 在同一个目录下安装或者更新, 新版本会覆盖以前的版本, 其它的项目就无 ...

  7. C#多线程编程实战1.7前台线程和后台线程

    using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threa ...

  8. Android学习笔记 ImageSwitcher图片切换组件的使用

    activity_main.xml <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android&qu ...

  9. 正确处理类的复合关系------新标准c++程序设计

    假设要编写一个小区养狗管理程序,该程序需要一个“主人”类,还需要一个“狗”类.狗是有主人的,主人也有狗.假定狗只有一个主人,但一个主人可以有最多10条狗.该如何处理“主人”类和“狗”类的关系呢?下面是 ...

  10. Intellij IDEA神器那些让人爱不释手的小技巧

      完整的IDEA使用教程,GitHub地址: https://github.com/judasn/IntelliJ-IDEA-Tutorial 概述 之前写了一篇介绍IntellIJ IDEA的文章 ...