MD5:登錄注冊,
SHA1
對稱加密
- 1.DES:Data Encryption Standard,數據加密標準
- 2.AES:Advanced Encryption Standard ,更高級的方式
對稱加密特點:加密速度快,只有一把鑰匙,鑰匙泄露文件就暴露
非對稱加密
- 加密算法:RSA
- 特點:
- 秘鑰對:私鑰和公鑰,秘鑰對不是指定的,系統生成的
- 私鑰自己保留,公鑰可以給別人
- 公鑰加密、使用解密
- 私鑰加密、公鑰解密
- 公鑰互換:兩個組織或者兩個人互換公鑰
- 數字簽名:驗證所屬關系(驗證私鑰在哪里,舉例:比如使用支付寶支付,支付寶它會有我們的公鑰)
我們來看看本地keystore涉及的算法
搜狗截圖20170709152342.png
搜狗截圖20170709152529.png
下面我們通過簡單的Demo來了解對稱加密
public class MainActivity extends AppCompatActivity {
private TextView mTv_result;
private String data;
private String key;
private boolean isDes;
private String mDesEncrypt;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mTv_result = (TextView) findViewById(R.id.tv_result);
data = "我的微信密碼: 123456789";//要加密的內容
key = "123456789";//解密/加密的key
}
public void des(View view) {
try {
if (!isDes) {
//加密
mDesEncrypt = Des.encrypt(data, key);
mTv_result.setText("DES加密:" + mDesEncrypt);
} else {
String desDecrypt = Des.decrypt(mDesEncrypt, key);//解密
mTv_result.setText("DES解密:" + desDecrypt);
}
isDes = !isDes;
} catch (Exception e) {
e.printStackTrace();
}
}
}
GIF.gif
AES 加密解密
public void aes(View view) {
if (!isAes) {
//加密
mAesEncrypt = Aes.encrypt(data, key);
mTv_result.setText("AES加密:" + mAesEncrypt);
} else {
//解密
String aesEecrypt = Aes.decrypt(mAesEncrypt, key);
mTv_result.setText("AES解密:" + aesEecrypt);
}
isAes = !isAes;
}
非對稱加密(RSA)
/**
* 初始化秘鑰對
*/
private void initKeyPair() {
// 初始化秘鑰對: 公鑰和私鑰
try {
Map<String, Object> keyPair = RSACrypt.genKeyPair();
privateKey = RSACrypt.getPrivateKey(keyPair);
publicKey = RSACrypt.getPublicKey(keyPair);
Log.e("result", "privateKey=" + privateKey);
Log.e("result", "publicKey=" + publicKey);
} catch (Exception e) {
e.printStackTrace();
}
}
public void rsa(View view) {
try {
if (!isRsa) {
//私鑰加密
encryptByPrivateKey = RSACrypt.encryptByPrivateKey(data.getBytes(), privateKey);
mTv_result.setText("RSA加密:"+RSACrypt.encode(encryptByPrivateKey));
}else {
//公鑰解密
byte[] decryptByPublicKey = RSACrypt.decryptByPublicKey(encryptByPrivateKey, publicKey);
mTv_result.setText("RSA解密:"+new String(decryptByPublicKey));
}
isRsa=!isRsa;
} catch (Exception e) {
e.printStackTrace();
}
}