MD5加密算法:http://blog.csdn.net/huangxiaoguo1/article/details/
Base64加密解密:http://blog.csdn.net/huangxiaoguo1/article/details/
异或加密解密:http://blog.csdn.net/huangxiaoguo1/article/details/
DES加密解密:http://blog.csdn.net/huangxiaoguo1/article/details/
AES自动生成base64密钥加密解密:http://blog.csdn.net/huangxiaoguo1/article/details/
AES加密解密(ECB模式):http://blog.csdn.net/huangxiaoguo1/article/details/
AES加密解密(CBC模式):http://blog.csdn.net/huangxiaoguo1/article/details/
非对称RSA加密解密:http://blog.csdn.net/huangxiaoguo1/article/details/
密码说明
加密标准
效果

代码
AESECBActivity
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.aesecb.AESUtils; / * ECB模式自设定秘钥 */ public class AESECBActivity 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 = "huangxiaoguo1234";//必须16位 private byte[] encrypt; @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); 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; } encrypt = AESUtils.encrypt(encryptionString.getBytes(), key.getBytes()); tvEncryption.setText(new String(encrypt)); break; case R.id.btn_decode://解密 String decodeString = tvEncryption.getText().toString().trim(); if (TextUtils.isEmpty(decodeString)) { Toast.makeText(mContext, "请先加密", Toast.LENGTH_SHORT).show(); return; } byte[] decrypt = AESUtils.decrypt(encrypt, key.getBytes()); tvDecode.setText(new String(decrypt)); break; } } }
AESUtils
package tsou.com.encryption.aesecb; import javax.crypto.Cipher; import javax.crypto.spec.SecretKeySpec; / * AES加密解密工具 * * @author huangxiaoguo */ public class AESUtils {
/ * AES加密 * * @param data * 将要加密的内容 * @param key * 密钥 * @return 已经加密的内容 */ public static byte[] encrypt(byte[] data, byte[] key) { //不足16字节,补齐内容为差值 int len = 16 - data.length % 16; for (int i = 0; i < len; i++) { byte[] bytes = { (byte) len }; data = ArrayUtils.concat(data, bytes); } try { SecretKeySpec skeySpec = new SecretKeySpec(key, "AES"); Cipher cipher = Cipher.getInstance("AES/ECB/NoPadding"); cipher.init(Cipher.ENCRYPT_MODE, skeySpec); return cipher.doFinal(data); } catch (Exception e) { e.printStackTrace(); } return new byte[] {}; } / * AES解密 * * @param data * 将要解密的内容 * @param key * 密钥 * @return 已经解密的内容 */ public static byte[] decrypt(byte[] data, byte[] key) { data = ArrayUtils.noPadding(data, -1); try { SecretKeySpec skeySpec = new SecretKeySpec(key, "AES"); Cipher cipher = Cipher.getInstance("AES/ECB/NoPadding"); cipher.init(Cipher.DECRYPT_MODE, skeySpec); byte[] decryptData = cipher.doFinal(data); int len = 2 + ByteUtils.byteToInt(decryptData[4]) + 3; return ArrayUtils.noPadding(decryptData, len); } catch (Exception e) { e.printStackTrace(); } return new byte[] {}; } }
ArrayUtils
package tsou.com.encryption.aesecb; / * 数组工具 * * @author huangxiaoguo */ public class ArrayUtils {
/ * 合并数组 * * @param firstArray * 第一个数组 * @param secondArray * 第二个数组 * @return 合并后的数组 */ public static byte[] concat(byte[] firstArray, byte[] secondArray) { if (firstArray == null || secondArray == null) { return null; } byte[] bytes = new byte[firstArray.length + secondArray.length]; System.arraycopy(firstArray, 0, bytes, 0, firstArray.length); System.arraycopy(secondArray, 0, bytes, firstArray.length, secondArray.length); return bytes; } / * 去除数组中的补齐 * * @param paddingBytes * 源数组 * @param dataLength * 去除补齐后的数据长度 * @return 去除补齐后的数组 */ public static byte[] noPadding(byte[] paddingBytes, int dataLength) { if (paddingBytes == null) { return null; } byte[] noPaddingBytes = null; if (dataLength > 0) { if (paddingBytes.length > dataLength) { noPaddingBytes = new byte[dataLength]; System.arraycopy(paddingBytes, 0, noPaddingBytes, 0, dataLength); } else { noPaddingBytes = paddingBytes; } } else { int index = paddingIndex(paddingBytes); if (index > 0) { noPaddingBytes = new byte[index]; System.arraycopy(paddingBytes, 0, noPaddingBytes, 0, index); } } return noPaddingBytes; } / * 获取补齐的位置 * * @param paddingBytes * 源数组 * @return 补齐的位置 */ private static int paddingIndex(byte[] paddingBytes) { for (int i = paddingBytes.length - 1; i >= 0; i--) { if (paddingBytes[i] != 0) { return i + 1; } } return -1; } }
ByteUtils
package tsou.com.encryption.aesecb; public class ByteUtils { public static int byteToInt(byte b) { return (b) & 0xff; } }
Demo下载地址:java-android:AES加密,RAS加密,DES加密,MD5加密,Base64加密,异或加密
发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/209347.html原文链接:https://javaforall.net
