//判断签名是否存在
if (signatureStr == null) {
Log.w(“huangxiaoguo”, “Invalid signature.”);
Log.w(“huangxiaoguo”, “Exiting verifyData()…”);
return false;
}
try {
//Base64解码字符串
signature = Base64.decode(signatureStr, Base64.DEFAULT);
} catch (IllegalArgumentException e) {
return false;
}
KeyStore ks = KeyStore.getInstance(“AndroidKeyStore”);
ks.load(null);
// Load the key pair from the Android Key Store
if (mAlias == null) {
setAlias(SAMPLE_ALIAS);
}
//从Android加载密钥对密钥存储库中
KeyStore.Entry entry = ks.getEntry(mAlias, null);
if (entry == null) {
Log.w(“huangxiaoguo”, "No key found under alias: " + mAlias);
Log.w(“huangxiaoguo”, “Exiting verifyData()…”);
return false;
}
if (!(entry instanceof KeyStore.PrivateKeyEntry)) {
Log.w(“huangxiaoguo”, “Not an instance of a PrivateKeyEntry”);
return false;
}
Signature s = Signature.getInstance(SecurityConstants.SIGNATURE_SHA256withRSA);
// 开始校验签名
s.initVerify(((KeyStore.PrivateKeyEntry) entry).getCertificate());
s.update(data);
boolean valid = s.verify(signature);
return valid;//true 签名一致
}
/**
-
判断是否创建过秘钥
-
@return
-
@throws KeyStoreException
-
@throws CertificateException
-
@throws NoSuchAlgorithmException
-
@throws IOException
-
@throws UnrecoverableEntryException
*/
public static boolean isHaveKeyStore() {
try {
KeyStore ks = KeyStore.getInstance(“AndroidKeyStore”);
ks.load(null);
if (mAlias == null) {
setAlias(SAMPLE_ALIAS);
}
// Load the key pair from the Android Key Store
//从Android加载密钥对密钥存储库中
KeyStore.Entry entry = ks.getEntry(mAlias, null);
if (entry == null) {
return false;
}
} catch (KeyStoreException e) {
e.printStackTrace();
return false;
} catch (CertificateException e) {
e.printStackTrace();
return false;
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
return false;
} catch (IOException e) {
e.printStackTrace();
return false;
} catch (UnrecoverableEntryException e) {
e.printStackTrace();
return false;
}
return true;
}
}
package tsou.com.encryption.androidkeystoresign;
public class SecurityConstants {
public static final String KEYSTORE_PROVIDER_ANDROID_KEYSTORE = “AndroidKeyStore”;
public static final String TYPE_RSA = “RSA”;
public static final String TYPE_DSA = “DSA”;
public static final String TYPE_BKS = “BKS”;
public static final String SIGNATURE_SHA256withRSA = “SHA256withRSA”;
public static final String SIGNATURE_SHA512withRSA = “SHA512withRSA”;
}
- 执行操作
package tsou.com.encryption.activity.AndroidKeyStore;
import android.app.Activity;
import android.content.Context;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.RequiresApi;
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 java.io.IOException;
import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.security.NoSuchProviderException;
import java.security.SignatureException;
import java.security.UnrecoverableEntryException;
import java.security.cert.CertificateException;
import tsou.com.encryption.R;
import tsou.com.encryption.aescbc.Base64Encoder;
import tsou.com.encryption.androidkeystoresign.KeyStoneUtils;
/**
- Android AndroidKeyStore 对数据进行签名和验证
*/
public class AndroidKeyStoreActivity 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 Button encryptionNotFixed;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_or);
mActivity = this;
mContext = this;
encryptionContext = (EditText) findViewById(R.id.et_encryption_context);
encryption = (Button) findViewById(R.id.btn_encryption);
encryptionNotFixed = (Button) findViewById(R.id.btn_encryption_not_fixed);
tvEncryption = (TextView) findViewById(R.id.tv_encryption);
decode = (Button) findViewById(R.id.btn_decode);
tvDecode = (TextView) findViewById(R.id.tv_decode);
encryption.setText(“创建Key”);
encryptionNotFixed.setText(“签名”);
decode.setText(“验证”);
initListener();
}
private void initListener() {
encryption.setOnClickListener(this);
encryptionNotFixed.setOnClickListener(this);
decode.setOnClickListener(this);
}
private String mSignatureStr = null;
@RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR2)
@Override
public void onClick(View view) {
switch (view.getId()) {
/**
- 在应用安装后第一次运行时,生成一个随机密钥,并存入 KeyStore
当你想存储一个数据,便从 KeyStore 中取出之前生成的随机密钥,对你的数据进行加密,
加密完成后,已完成加密的数据可以随意存储在任意地方,比如 SharePreferences,
此时即使它被他人读取到,也无法解密出你的原数据,因为他人取不到你的密钥
当你需要拿到你的原数据,只需要从 SharePreferences 中读取你加密后的数据,
并从 KeyStore 取出加密密钥,使用加密密钥对 “加密后的数据” 进行解密即可
*/
case R.id.btn_encryption://创建Key,在项目中放在application或启动页中
if (KeyStoneUtils.isHaveKeyStore()) {//是否有秘钥
Toast.makeText(mContext, “已经创建过秘钥”, Toast.LENGTH_SHORT).show();
return;
}
try {
KeyStoneUtils.createKeys(mContext);
Toast.makeText(mContext, “秘钥创建成功”, Toast.LENGTH_SHORT).show();
} catch (InvalidAlgorithmParameterException e) {
e.printStackTrace();
Toast.makeText(mContext, “不支持RSA”, Toast.LENGTH_SHORT).show();
} catch (NoSuchProviderException e) {
e.printStackTrace();
Toast.makeText(mContext, “没有提供:AndroidKeyStore”, Toast.LENGTH_SHORT).show();
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
Toast.makeText(mContext, “无效的算法参数异常”, Toast.LENGTH_SHORT).show();
}
break;
case R.id.btn_encryption_not_fixed://签名
String encryptionString = encryptionContext.getText().toString().trim();
if (TextUtils.isEmpty(encryptionString)) {
Toast.makeText(mContext, “请输入签名内容”, Toast.LENGTH_SHORT).show();
return;
}
if (!KeyStoneUtils.isHaveKeyStore()) {//是否有秘钥
Toast.makeText(mContext, “没有生成秘钥对,请先创建key”, Toast.LENGTH_SHORT).show();
return;
}
try {
mSignatureStr = KeyStoneUtils.signData(encryptionString);
tvEncryption.setText(Base64Encoder.encode(mSignatureStr));
} catch (KeyStoreException e) {
e.printStackTrace();
Toast.makeText(mContext, “密钥存储库没有初始化,可能没有生成秘钥对”, Toast.LENGTH_SHORT).show();
} catch (CertificateException e) {
e.printStackTrace();
Toast.makeText(mContext, “加载证书时发生错误”, Toast.LENGTH_SHORT).show();
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
Toast.makeText(mContext, “不支持RSA”, Toast.LENGTH_SHORT).show();
} catch (IOException e) {
e.printStackTrace();
Toast.makeText(mContext, “IO Exception”, Toast.LENGTH_SHORT).show();
} catch (UnrecoverableEntryException e) {
e.printStackTrace();
Toast.makeText(mContext, “密钥对没有恢复”, Toast.LENGTH_SHORT).show();
} catch (InvalidKeyException e) {
e.printStackTrace();
Toast.makeText(mContext, “无效的key”, Toast.LENGTH_SHORT).show();
} catch (SignatureException e) {
e.printStackTrace();
Toast.makeText(mContext, “无效的签名”, Toast.LENGTH_SHORT).show();
}
break;
case R.id.btn_decode://验证
String encryptionverifyString = encryptionContext.getText().toString().trim();
if (TextUtils.isEmpty(encryptionverifyString)) {
Toast.makeText(mContext, “请输入验证内容”, Toast.LENGTH_SHORT).show();
return;
}
if (TextUtils.isEmpty(mSignatureStr)) {
Toast.makeText(mContext, “请先签名”, Toast.LENGTH_SHORT).show();
return;
}
try {
boolean b = KeyStoneUtils.verifyData(encryptionverifyString, mSignatureStr);
if (b) {
tvDecode.setText(“签名一致”);
} else {
tvDecode.setText(“签名不一致”);
}
} catch (KeyStoreException e) {
e.printStackTrace();
Toast.makeText(mContext, “密钥存储库没有初始化,可能没有生成秘钥对”, Toast.LENGTH_SHORT).show();
} catch (CertificateException e) {
e.printStackTrace();
Toast.makeText(mContext, “加载证书时发生错误”, Toast.LENGTH_SHORT).show();
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
Toast.makeText(mContext, “不支持RSA”, Toast.LENGTH_SHORT).show();
} catch (IOException e) {
e.printStackTrace();
Toast.makeText(mContext, “IO Exception”, Toast.LENGTH_SHORT).show();
} catch (UnrecoverableEntryException e) {
e.printStackTrace();
Toast.makeText(mContext, “密钥对没有恢复”, Toast.LENGTH_SHORT).show();
} catch (InvalidKeyException e) {
e.printStackTrace();
Toast.makeText(mContext, “无效的key”, Toast.LENGTH_SHORT).show();
} catch (SignatureException e) {
e.printStackTrace();
Toast.makeText(mContext, “无效的签名”, Toast.LENGTH_SHORT).show();
}
break;
}
}
自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。
深知大多数初中级Android工程师,想要提升技能,往往是自己摸索成长或者是报班学习,但对于培训机构动则近万的学费,着实压力不小。自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!
因此收集整理了一份《2024年Android移动开发全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。
既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上Android开发知识点,真正体系化!
由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且会持续更新!
如果你觉得这些内容对你有帮助,可以扫码获取!!(备注:Android)
![](https://img-blog.csdnimg.cn/img_convert/9a0dc7cd26db8417858136f74a061374.jpeg)
最后
我见过很多技术leader在面试的时候,遇到处于迷茫期的大龄程序员,比面试官年龄都大。这些人有一些共同特征:可能工作了7、8年,还是每天重复给业务部门写代码,工作内容的重复性比较高,没有什么技术含量的工作。问到这些人的职业规划时,他们也没有太多想法。
其实30岁到40岁是一个人职业发展的黄金阶段,一定要在业务范围内的扩张,技术广度和深度提升上有自己的计划,才有助于在职业发展上有持续的发展路径,而不至于停滞不前。
不断奔跑,你就知道学习的意义所在!
《Android学习笔记总结+移动架构视频+大厂面试真题+项目实战源码》,点击传送门即可获取!
0175861)]
既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上Android开发知识点,真正体系化!
由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且会持续更新!
如果你觉得这些内容对你有帮助,可以扫码获取!!(备注:Android)
![](https://img-blog.csdnimg.cn/img_convert/9a0dc7cd26db8417858136f74a061374.jpeg)
最后
我见过很多技术leader在面试的时候,遇到处于迷茫期的大龄程序员,比面试官年龄都大。这些人有一些共同特征:可能工作了7、8年,还是每天重复给业务部门写代码,工作内容的重复性比较高,没有什么技术含量的工作。问到这些人的职业规划时,他们也没有太多想法。
其实30岁到40岁是一个人职业发展的黄金阶段,一定要在业务范围内的扩张,技术广度和深度提升上有自己的计划,才有助于在职业发展上有持续的发展路径,而不至于停滞不前。
不断奔跑,你就知道学习的意义所在!