美文网首页
加密算法

加密算法

作者: 泛光灯 | 来源:发表于2017-07-09 15:32 被阅读0次

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();
    }
}

相关文章

网友评论

      本文标题:加密算法

      本文链接:https://www.haomeiwen.com/subject/nkgcbttx.html