java 简单的des加密示例
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加密示例的更多相关文章
- Java与.NET DES加密解密互转
上代码: Java代码: import javax.crypto.Cipher; import javax.crypto.SecretKey; import javax.crypto.SecretKe ...
- 【Java】通过DES加密和解密工具,对字符串进行加密和解密操作
分享一个非常不错的字符串加密和解密的程序. 可以指定不同的密钥对同一字符串进行不同的加密操作,增强加密性能. Java代码如下: package com.app; import java.securi ...
- PHP 识别 java 8位 des 加密和 解密方式
代码及使用说明: <?php /** *PHP 识别 java 8位密钥的加密和解密方式 *@desc 加密方式 通用 */ class DES { var $key; var $iv; //偏 ...
- java简单实现MD5加密
1.话不多说,直接上代码-----传入字符串,返回加密码 import java.security.MessageDigest; import java.text.NumberFormat; publ ...
- 关于Objective-c和Java下DES加密保持一致的方式
转载自:http://www.cnblogs.com/janken/archive/2012/04/05/2432930.html 最近做了一个移动项目,是有服务器和客户端类型的项目,客户端是要登录才 ...
- C# DES加密类,16位的加密。
这个加密类是与java写的DES加密不同时,自己写的,最后与Java的加密相同了,解决了加密后不同的问题. 可以直接调用里面的加密和解密的方法. using System; using System. ...
- JAVA实现DES加密实现详解
package util; import java.security.SecureRandom; import javax.crypto.spec.DESKeySpec; import javax.c ...
- JAVA实现DES加密
DES加密介绍 DES是一种对称加密算法,所谓对称加密算法即:加密和解密使用相同密钥的算法.DES加密算法出自IBM的研究,后来被美国政府正式采用,之后开始广泛流传,但是近些年使用越来越少 ...
- IOS、java支持DES加密
转载请注明博客地址:http://blog.csdn.net/mengxiangyue/article/details/40015727 近期在考虑数据加密方面的需求,所以对数据加密简单的看了一下,当 ...
随机推荐
- Linq学习<二>
http://www.cnblogs.com/wyqlijin/archive/2011/02/25/1964934.html 这位仁兄写的比较高深,建议大家看看 一: 这一篇以一个数据类为例,操作数 ...
- 编写高质量代码改善C#程序的157个建议——建议34:为泛型参数设定约束
建议34:为泛型参数设定约束 “约束”这个词可能会引起歧义,有些人肯能认为对泛型参数设定约束是限制参数的使用,实际情况正好相反.没有“约束”的泛型参数作用很有限,倒是“约束”让泛型参数具有了更多的行为 ...
- wpf path语法
http://www.cnblogs.com/HQFZ/p/4452548.html WPF系列 Path表示语法详解(Path之Data属性语法)
- LibreOJ 6281 数列分块入门 5(分块区间开方区间求和)
题解:区间开方emmm,这马上让我想起了当时写线段树的时候,很显然,对于一个在2^31次方以内的数,开方7-8次就差不多变成一了,所以我们对于每次开方,如果块中的所有数都为一了,那么开方也没有必要了. ...
- jQuery bind() live()
<script type="text/javascript"> $(document).ready(function () { /*$('.clickme').live ...
- 在sql server数据库可以插入在回车的数据
insert into t_FamilyClass (id,ParentId,Name) values(111,111,'111')可以在编辑模式下copy到editplus中,设置 显示 空白字符: ...
- window中启动vs后鼠标无法移动
你停止wisptis.exe这个进程,在c:\Windows\System32下删除wispitis.exe就可以了!
- Castle Windsor
让我们从Web API的集成点开始,它们是IDependencyResolver和IDependencyScope接口.IDependencyResolver和其他接口的名称可能与MVC中的接口相同, ...
- 【bzoj4836】二元运算 分治FFT
Description 定义二元运算 opt 满足 现在给定一个长为 n 的数列 a 和一个长为 m 的数列 b ,接下来有 q 次询问.每次询问给定一个数字 c 你需要求出有多少对 (i, j) 使 ...
- 带权并查集 - How Many Answers Are Wrong
思路: 带权并查集+向量偏移 #include <iostream> using namespace std; int n, m; ]; ]; // 到根节点的距离 ; void init ...