DES全称为Data Encryption
Standard,即数据加密标准,是一种使用密钥加密的块算法,1977年被美国联邦政府的国家标准局确定为联邦资料处理标准(FIPS),并授权在非密级政府通信中使用,随后该算法在国际上广泛流传开来。需要注意的是,在某些文献中,作为算法的DES称为数据加密算法(Data
Encryption Algorithm,DEA),已与作为标准的DES区分开来。
MD5加密算法:http://blog.csdn.net/huangxiaoguo1/article/details/78042596
Base64加密解密:http://blog.csdn.net/huangxiaoguo1/article/details/78042715
异或加密解密:http://blog.csdn.net/huangxiaoguo1/article/details/78042802
DES加密解密:http://blog.csdn.net/huangxiaoguo1/article/details/78042908
AES自动生成base64密钥加密解密:http://blog.csdn.net/huangxiaoguo1/article/details/78043000
AES加密解密(ECB模式):http://blog.csdn.net/huangxiaoguo1/article/details/78043098
AES加密解密(CBC模式):http://blog.csdn.net/huangxiaoguo1/article/details/78043169
非对称RSA加密解密:http://blog.csdn.net/huangxiaoguo1/article/details/78043354
DES算法的入口参数有三个:Key、Data、Mode。其中Key为7个字节共56位,是DES算法的工作密钥;Data为8个字节64位,是要被加密或被解密的数据;Mode为DES的工作方式,有两种:加密或解密。
DESActivity
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import tsou.com.encryption.R;
import tsou.com.encryption.des.DesUtils;
/**
* DES加密介绍:
*
* DES是一种对称加密算法,所谓对称加密算法即:加密和解密使用相同密钥的算法。
* DES加密算法出自IBM的研究,
* 后来被美国政府正式采用,之后开始广泛流传,但是近些年使用越来越少,
* 因为DES使用56位密钥,以现代计算能力,
* 24小时内即可被破解。
*/
public class DESActivity extends AppCompatActivity implements View.OnClickListener {
private EditText encryptionContext;
private Button encryption;
private TextView tvEncryption;
private Button decode;
private TextView tvDecode;
private Activity mActivity;
private Context mContext;
private String key;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_aes);
mActivity = this;
mContext = this;
encryptionContext = (EditText) findViewById(R.id.et_encryption_context);
encryption = (Button) findViewById(R.id.btn_encryption);
tvEncryption = (TextView) findViewById(R.id.tv_encryption);
decode = (Button) findViewById(R.id.btn_decode);
tvDecode = (TextView) findViewById(R.id.tv_decode);
key = DesUtils.generateKey();
initListener();
}
private void initListener() {
encryption.setOnClickListener(this);
decode.setOnClickListener(this);
}
@Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.btn_encryption://加密
String encryptionString = encryptionContext.getText().toString().trim();
if (TextUtils.isEmpty(encryptionString)) {
Toast.makeText(mContext, "请输入加密内容", Toast.LENGTH_SHORT).show();
return;
}
String encode = DesUtils.encode(key, encryptionString);
tvEncryption.setText(encode);
break;
case R.id.btn_decode://解密
String decodeString = tvEncryption.getText().toString().trim();
if (TextUtils.isEmpty(decodeString)) {
Toast.makeText(mContext, "请先加密", Toast.LENGTH_SHORT).show();
return;
}
String decode = DesUtils.decode(key, decodeString);
tvDecode.setText(decode);
break;
}
}
}
DesUtils
import android.util.Base64;
import java.security.Key;
import java.security.SecureRandom;
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
/**
* Created by Administrator on 2017/9/20 0020.
*/
public class DesUtils {
//至少16位
private final static String HEX = "huangxiaoguo1234";
//DES是加密方式 CBC是工作模式 PKCS5Padding是填充模式
private final static String TRANSFORMATION = "DES/CBC/PKCS5Padding";
//初始化向量参数,AES 为16bytes. DES 为8bytes,只能8位
private final static String IVPARAMETERSPEC = "xiao_guo";
//DES是加密方式
private final static String ALGORITHM = "DES";
// SHA1PRNG 强随机种子算法, 要区别4.2以上版本的调用方法
private static final String SHA1PRNG = "SHA1PRNG";
/**
* 生成随机数,可以当做动态的密钥 加密和解密的密钥必须一致,不然将不能解密
*
* @return
*/
public static String generateKey() {
try {
SecureRandom localSecureRandom = SecureRandom.getInstance(SHA1PRNG);
byte[] bytes_key = new byte[20];
localSecureRandom.nextBytes(bytes_key);
String str_key = toHex(bytes_key);
return str_key;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
/**
* 二进制转字符
*
* @param buf
* @return
*/
public static String toHex(byte[] buf) {
if (buf == null)
return "";
StringBuffer result = new StringBuffer(2 * buf.length);
for (int i = 0; i < buf.length; i++) {
appendHex(result, buf[i]);
}
return result.toString();
}
private static void appendHex(StringBuffer sb, byte b) {
sb.append(HEX.charAt((b >> 4) & 0x0f)).append(HEX.charAt(b & 0x0f));
}
/**
* 对密钥进行处理
*
* @param key
* @return
* @throws Exception
*/
private static Key getRawKey(String key) throws Exception {
KeyGenerator kgen = KeyGenerator.getInstance(ALGORITHM);
//for android
SecureRandom sr = null;
// 在4.2以上版本中,SecureRandom获取方式发生了改变
if (android.os.Build.VERSION.SDK_INT >= 17) {
sr = SecureRandom.getInstance(SHA1PRNG, "Crypto");
} else {
sr = SecureRandom.getInstance(SHA1PRNG);
}
// for Java
// secureRandom = SecureRandom.getInstance(SHA1PRNG);
sr.setSeed(key.getBytes());
kgen.init(64, sr); //DES固定格式为64bits,即8bytes。
SecretKey skey = kgen.generateKey();
byte[] raw = skey.getEncoded();
return new SecretKeySpec(raw, ALGORITHM);
}
/**
* 第二种对密钥进行处理
*/
// private static Key getRawKey(String key) throws Exception {
// DESKeySpec dks = new DESKeySpec(key.getBytes());
// SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(ALGORITHM);
// return keyFactory.generateSecret(dks);
// }
/**
* DES算法,加密
*
* @param data 待加密字符串
* @param key 加密私钥,长度不能够小于8位
* @return 加密后的字节数组,一般结合Base64编码使用
*/
public static String encode(String key, String data) {
return encode(key, data.getBytes());
}
/**
* DES算法,加密
*
* @param data 待加密字符串
* @param key 加密私钥,长度不能够小于8位
* @return 加密后的字节数组,一般结合Base64编码使用
*/
public static String encode(String key, byte[] data) {
try {
Cipher cipher = Cipher.getInstance(TRANSFORMATION);
IvParameterSpec iv = new IvParameterSpec(IVPARAMETERSPEC.getBytes());
cipher.init(Cipher.ENCRYPT_MODE, getRawKey(key), iv);
byte[] bytes = cipher.doFinal(data);
return Base64.encodeToString(bytes, Base64.DEFAULT);
} catch (Exception e) {
return null;
}
}
/**
* 获取编码后的值
*
* @param key
* @param data
* @return
*/
public static String decode(String key, String data) {
return decode(key, Base64.decode(data, Base64.DEFAULT));
}
/**
* DES算法,解密
*
* @param data 待解密字符串
* @param key 解密私钥,长度不能够小于8位
* @return 解密后的字节数组
*/
public static String decode(String key, byte[] data) {
try {
Cipher cipher = Cipher.getInstance(TRANSFORMATION);
IvParameterSpec iv = new IvParameterSpec(IVPARAMETERSPEC.getBytes());
cipher.init(Cipher.DECRYPT_MODE, getRawKey(key), iv);
byte[] original = cipher.doFinal(data);
String originalString = new String(original);
return originalString;
} catch (Exception e) {
return null;
}
}
}
Demo下载地址:java-android:AES加密,RAS加密,DES加密,MD5加密,Base64加密,异或加密