加密/解密 对称加密:只有一把密钥,如果密钥暴露,文件就会被暴露
des: Data Encryption Standard
aes: Advanced Encryption Standard
特点:
- 加密速度比较快.可以加密比较大的文件
- 密码可以自己指定
- 非对称加密:有两把钥匙(密钥对),公钥和私钥,公钥的话给别人.私钥自己保存;
RSA
- 特点:加密速度比慢一些,但是安全系数比较高
- 秘钥对的话需要程序生成.不能我们自己定义
1.加密/解密:公钥加密–>私钥解密
public static byte[] encryptByPublicKey(byte[] data, String publicKey)//公钥加密
public static byte[] decryptByPrivateKey(byte[] encryptedData, String privateKey)//私钥解密
2.加密/解密:私钥加密–>公钥解密
public static byte[] encryptByPrivateKey(byte[] data, String privateKey)//私钥加密
public static byte[] decryptByPublicKey(byte[] encryptedData, String publicKey)//公钥解密
3.数字签名:私钥签名–>公钥校验,确定数据来源的不可否认性,确定所属关系–>其实就是确定privatekey在哪里.
签名:public static String sign(byte[] data, String privateKey)//只能使用私钥进行签名
校验:public static boolean verify(byte[] data, String publicKey, String sign)
公钥互换:两个人,两个机构,就是说两个密钥对的持有者相互的置换公钥
package com.cllstudy.aesdesrsa;
import java.util.Map;
import com.billy.androidutils.utils.encrypt.aes.AesHelper;
import com.billy.androidutils.utils.encrypt.des.DesHelper;
import com.billy.androidutils.utils.encrypt.rsa.RsaHelper;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
public class MainActivity extends Activity {
private TextView textView;
private String context = "Sunshine always comes after the rain";
private String password = "11223344556677889900";
private boolean isAes = false;
private boolean isDes = false;
private boolean isRsa = false;
private String privateKey;
private String publicKey;
private String aesEncrypt;
private String encrypt;
private byte[] encryptByPrivateKey;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView = (TextView) findViewById(R.id.textView);
try {
Map<String, Object> genKeyPair = RsaHelper.genKeyPair();
privateKey = RsaHelper.getPrivateKey(genKeyPair);
publicKey = RsaHelper.getPublicKey(genKeyPair);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* aes加密解密
*
* @param view
*/
public void aes(View view) {
if (!isAes) {
// 加密
aesEncrypt = AesHelper.encrypt(context, password);
textView.setText(aesEncrypt);
} else {
String aesDecrypt = AesHelper.decrypt(aesEncrypt, password);
textView.setText(aesDecrypt);
}
isAes = !isAes;
}
/**
* des加密解密
*
* @param view
*/
public void des(View view) {
try {
if (!isDes) {
// 加密
encrypt = DesHelper.encrypt(context, password);
textView.setText(encrypt);
} else {
// 解密
String decrypt = DesHelper.decrypt(encrypt, password);
textView.setText(decrypt);
}
isDes = !isDes;
} catch (Exception e) {
// TODO: handle exception
}
}
/**
* rsa加密解密
*
* @param view
*/
public void rsa(View view) {
try {
if (!isRsa) {
// 加密
encryptByPrivateKey = RsaHelper.encryptByPrivateKey(
context.getBytes(), privateKey);
String encode = RsaHelper.encode(encryptByPrivateKey);
textView.setText(encode);
} else {
// 解密
byte[] decryptByPublicKey = RsaHelper.decryptByPublicKey(
encryptByPrivateKey, publicKey);
textView.setText(new String(decryptByPublicKey));
}
isRsa = !isRsa;
} catch (Exception e) {
// TODO: handle exception
}
}
}