Cipher cipher = Cipher.getInstance("DES");
DESKeySpec keySpec = new DESKeySpec(new byte[1,2,3,4,5,6,7,8]);
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("EDS");
SecretKey key = keyFactory.generateSecret(keySpec);
cipher.init(Cipher.ENCRYPT_MODE,key);
bytep[] ret = cipher.doFinal(data);
/**
* DES加密算法
* @param mode 模式: 加密,解密
* @param data 需要加密的内容
* @param keyData 密钥 8个字节数组
* @return 将内容加密后的结果也是byte[]格式的
*/
public static byte[] des(int mode,byte[] data,byte[] keyData)
{
byte[] ret = null;
//加密的内容存在并且密钥存在且长度为8个字节
if (data != null
&& data.length>0
&&keyData!=null
&& keyData.length==8) {
try {
Cipher cipher = Cipher.getInstance("DES");
DESKeySpec keySpec = new DESKeySpec(keyData);
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
SecretKey key = keyFactory.generateSecret(keySpec);
cipher.init(mode, key);
ret = cipher.doFinal(data);
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (NoSuchPaddingException e) {
e.printStackTrace();
} catch (IllegalBlockSizeException e) {
e.printStackTrace();
} catch (BadPaddingException e) {
e.printStackTrace();
} catch (InvalidKeySpecException e) {
e.printStackTrace();
} catch (InvalidKeyException e) {
e.printStackTrace();
}
}
return ret;
}
//DES 加密
public static byte[] desEncrypt(byte[] data,byte[] keyData){
return des(Cipher.ENCRYPT_MODE,data,keyData);
}
//DES 解密
public static byte[] desDecrypt(byte[] data,byte[] keyData){
return des(Cipher.DECRYPT_MODE,data,keyData);
}
package com.xqx.encrypsthow;
import android.app.Activity;
import android.os.Bundle;
import android.util.Base64;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;
import utils.EncryptUtil;
import javax.crypto.*;
import javax.crypto.spec.DESKeySpec;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.spec.InvalidKeySpecException;
import java.util.Arrays;
/**
* Created by Administrator on 2015/10/16.
*/
/**
* 对称加密
*/
public class SythEncryptActivity extends Activity {
private EditText txtContent;
private EditText txtPassword;
private EditText txtResult;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.sythencrypylayout);
txtContent = (EditText) findViewById(R.id.txt_content);
txtPassword = (EditText) findViewById(R.id.txt_password);
txtResult = (EditText) findViewById(R.id.txt_result);
}
/**
* DES加密,要求密码必须8个字节,64bit长度 byte[8]
* @param view
*/
public void btnDESEncrypt(View view) {
//获取需要加密的内容
String content = txtContent.getText().toString();
//获取密钥
String password = txtPassword.getText().toString();
//注意,加密,解密,秘钥都需要是字节数组
byte[] keyData = password.getBytes();
//需要加密的内容
byte[] contentData = content.getBytes();
if(keyData.length == 8) {
byte[] encryptedData = EncryptUtil.des(Cipher.ENCRYPT_MODE, contentData, keyData);
//获取加密后的数据(记住是byte[]类型的),用Base64编码 成可见的字符串形式
String s = Base64.encodeToString(encryptedData, Base64.NO_WRAP);
//显示加密后的内容
txtResult.setText(s);
}
}
/**
* DES的解密
* @param view
*/
public void btnDESDecrypt(View view) {
String encryptedStr = txtResult.getText().toString();
if(encryptedStr.length()>0){
String password = txtPassword.getText().toString();
//因为在加密方法中,使用Base64对加密的内容进行编码,要解密的时候需要Base64的解码
byte[] encryptedData = Base64.decode(encryptedStr, Base64.NO_WRAP);
byte[] keyData = password.getBytes();
//DES 要求 8个字节
if(keyData.length == 8){
//形成原始数据
byte[] decryptedData = EncryptUtil.des(Cipher.DECRYPT_MODE, encryptedData, keyData);
txtResult.setText(new String(decryptedData));
}
}
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<EditText
android:id="@+id/txt_content"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="请输入内容"
/>
<EditText
android:id="@+id/txt_password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="DES密钥"
android:text="12345678"
android:inputType="textVisiblePassword"
/>
<EditText
android:id="@+id/txt_result"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="DES加密"
android:onClick="btnDESEncrypt"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="DES解密"
android:onClick="btnDESDecrypt"
/>
</LinearLayout>
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有