一、集成Alipay
1、打开支付宝平台登录账号
https://open.alipay.com/platform/home.htm
直接支付宝扫码登录
2、找到开发服务-沙箱
https://opendocs.alipay.com/open/200/105311/
https://openhome.alipay.com/platform/appDaily.htm
3、使用工具生成公钥和私钥、配置沙箱环境
https://opendocs.alipay.com/open/291/105971
4、配置沙箱环境,将生成的公钥填入沙箱
5、下载沙箱钱包
https://sandbox.alipaydev.com/user/downloadApp.htm
6、登陆沙箱钱包
7、下载demo
https://opendocs.alipay.com/open/54/104509
8、修改demo中的配置
需要修改APPID,PID,TARGET_ID,RSA2_PRIVATE
在onCreate中新增:EnvUtils.setEnv(EnvUtils.EnvEnum.SANDBOX);
9、PID别填错
10、运行
二、
1、Base64
private void base64Demo() {
String str = "Simon";
String strOut = Base64.encodeToString(str.getBytes(), Base64.DEFAULT);
Log.i("Simon", "" + strOut);
byte[] bytes = Base64.decode(strOut, Base64.DEFAULT);
Log.i("Simon", "" + new String(bytes));
}
2、MD5
字符串
private void md5Demo() throws NoSuchAlgorithmException {
String str = "Simon";
MessageDigest messageDigest = MessageDigest.getInstance("md5");
messageDigest.update(str.getBytes());
String ret = new BigInteger(1, messageDigest.digest()).toString(16);
Log.i("Simon", "ret: " + ret);
}
文件
public static String getFileMD5(File file) {
if (!file.isFile()) {
return null;
}
MessageDigest digest = null;
FileInputStream in = null;
byte buffer[] = new byte[1024];
int len;
try {
digest = MessageDigest.getInstance("MD5");
in = new FileInputStream(file);
while ((len = in.read(buffer, 0, 1024)) != -1) {
digest.update(buffer, 0, len);
}
in.close();
} catch (Exception e) {
e.printStackTrace();
return null;
}
return new BigInteger(1, digest.digest()).toString(16);
}
3、sha
//sha-256 摘要,加密
private String shaDemo(String strSrc) {
MessageDigest sha = null;
byte[] bt = strSrc.getBytes();
try {
sha = MessageDigest.getInstance("SHA-256");// 将此换成SHA-1、SHA-512、SHA-384等参数
sha.update(bt);
return new BigInteger(1, sha.digest()).toString(16);
} catch (NoSuchAlgorithmException e) {
return null;
}
}
4、DES(对称)
//DES 对称加密
private void desDemo(String input) throws NoSuchPaddingException, NoSuchAlgorithmException, InvalidKeyException, BadPaddingException, IllegalBlockSizeException, InvalidKeySpecException {
//AES加密只需要将DES改成AES即可
//1,得到cipher 对象(可翻译为密码器或密码系统)
//初始化cipher 对象时可以指定更详细的参数
//格式:”algorithm/mode/padding” ,即”算法/工作模式/填充模式” 具体看http://blog.csdn.net/axi295309066/article/details/52491077的最后面
Cipher cipher = Cipher.getInstance("DES");
//2,创建秘钥
//方式0
// SecretKey key = KeyGenerator.getInstance("DES").generateKey();
//方式一
// KeySpec keySpec = new DESKeySpec("12345678".getBytes()); //12345678为自定义密钥
// SecretKeyFactory secretKeyFactory = SecretKeyFactory.getInstance("DES");//DES处参数填加密模式
// Key key = secretKeyFactory.generateSecret(keySpec);
//方式二:
Key key = new SecretKeySpec("12345678".getBytes(), "DES");
//加密
//3,设置操作模式(加密/解密)
cipher.init(Cipher.ENCRYPT_MODE, key);
//4,执行加密
byte[] relBytes = cipher.doFinal(input.getBytes());
//注意:加密过后用Base64编码 缺少这步会导致解密失败
byte[] relBase = Base64.encode(relBytes, Base64.DEFAULT);
String encodeStr = new String(relBase);
Log.d("Simon", "des ENCRYPT_MODE: " + encodeStr);
//解密
//3,设置操作模式(加密/解密)
cipher.init(Cipher.DECRYPT_MODE, key);
//4,执行解密
//先用Base64解密 缺少Base64编码会导致解密失败
byte[] decode = Base64.decode(encodeStr, Base64.DEFAULT);
byte[] bytes = cipher.doFinal(decode);
String decodeStr = new String(bytes);
Log.d("Simon", "des DECRYPT_MODE: " + decodeStr);
}
5、
//非对称加密,rsa
public class RSAUtils {
public void demo() throws Exception {
HashMap<String, Object> kes = initkeys();
Key key = (Key) kes.get("public_key");
Key key1 = (Key) kes.get("private_key");
byte[] pubKey = key.getEncoded(); //公钥
byte[] priKey = key1.getEncoded(); //私钥
Log.i("Simon", "公钥:" + Base64.encode(pubKey, Base64.DEFAULT) + " 私钥:" + Base64.encode(priKey, Base64.DEFAULT));
String str = "加密内容";
Log.i("Simon", "原文:" + Base64.encode(str.getBytes(), Base64.DEFAULT));
byte[] code1 = encryptByPublickey(str.getBytes(), pubKey);
Log.i("Simon", "公钥加密后的数据:" + Base64.encode(code1, Base64.DEFAULT));
byte[] code2 = decryptByPrivateKey(code1, priKey);
Log.i("Simon", "私钥解密后的数据:" + Base64.encode(code2, Base64.DEFAULT).toString() + " " + new String(code2));
}
private HashMap<String, Object> initkeys() throws NoSuchAlgorithmException {
KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("RSA");
keyPairGenerator.initialize(512);
KeyPair pair = keyPairGenerator.generateKeyPair();
PublicKey aPublic = pair.getPublic(); //公钥
PrivateKey aPrivate = pair.getPrivate(); //私钥
HashMap<String, Object> keys = new HashMap<>();
keys.put("public_key", aPublic);
keys.put("private_key", aPrivate);
return keys;
}
/**
* 公钥加密
*
* @return
*/
private byte[] encryptByPublickey(byte[] data, byte[] publickey) throws Exception {
KeyFactory keyFactory = KeyFactory.getInstance("RSA"); //获得key工厂
X509EncodedKeySpec x509EncodedKeySpec = new X509EncodedKeySpec(publickey); //X509EncodedKeySpec公钥的解码类
PublicKey publicKey = keyFactory.generatePublic(x509EncodedKeySpec);//获得公钥
Cipher cipher = Cipher.getInstance(keyFactory.getAlgorithm());//Cipher负责加密解密
cipher.init(Cipher.ENCRYPT_MODE, publicKey);
return cipher.doFinal(data);
}
/**
* 私钥解密
*
* @param data 待解密数据
* @param key 密钥
* @return byte[] 解密数据
*/
private byte[] decryptByPrivateKey(byte[] data, byte[] key) throws Exception {
//取得私钥
PKCS8EncodedKeySpec pkcs8KeySpec = new PKCS8EncodedKeySpec(key);
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
//生成私钥
PrivateKey privateKey = keyFactory.generatePrivate(pkcs8KeySpec);
//数据解密
Cipher cipher = Cipher.getInstance(keyFactory.getAlgorithm());
cipher.init(Cipher.DECRYPT_MODE, privateKey);
return cipher.doFinal(data);
}
}