一幅骄、集成Alipay
1球涛、打開支付寶平臺(tái)登錄賬號(hào)
https://open.alipay.com/platform/home.htm
直接支付寶掃碼登錄
2局劲、找到開發(fā)服務(wù)-沙箱
https://opendocs.alipay.com/open/200/105311/
https://openhome.alipay.com/platform/appDaily.htm
3集惋、使用工具生成公鑰和私鑰髓堪、配置沙箱環(huán)境
https://opendocs.alipay.com/open/291/105971
圖片.png
4屯仗、配置沙箱環(huán)境搞坝,將生成的公鑰填入沙箱
圖片.png
5、下載沙箱錢包
https://sandbox.alipaydev.com/user/downloadApp.htm
6魁袜、登陸沙箱錢包
圖片.png
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別填錯(cuò)
圖片.png
10融师、運(yùn)行
圖片.png
二、
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等參數(shù)
sha.update(bt);
return new BigInteger(1, sha.digest()).toString(16);
} catch (NoSuchAlgorithmException e) {
return null;
}
}
4、DES(對(duì)稱)
//DES 對(duì)稱加密
private void desDemo(String input) throws NoSuchPaddingException, NoSuchAlgorithmException, InvalidKeyException, BadPaddingException, IllegalBlockSizeException, InvalidKeySpecException {
//AES加密只需要將DES改成AES即可
//1房待,得到cipher 對(duì)象(可翻譯為密碼器或密碼系統(tǒng))
//初始化cipher 對(duì)象時(shí)可以指定更詳細(xì)的參數(shù)
//格式:”algorithm/mode/padding” 邢羔,即”算法/工作模式/填充模式” 具體看http://blog.csdn.net/axi295309066/article/details/52491077的最后面
Cipher cipher = Cipher.getInstance("DES");
//2,創(chuàng)建秘鑰
//方式0
// SecretKey key = KeyGenerator.getInstance("DES").generateKey();
//方式一
// KeySpec keySpec = new DESKeySpec("12345678".getBytes()); //12345678為自定義密鑰
// SecretKeyFactory secretKeyFactory = SecretKeyFactory.getInstance("DES");//DES處參數(shù)填加密模式
// Key key = secretKeyFactory.generateSecret(keySpec);
//方式二:
Key key = new SecretKeySpec("12345678".getBytes(), "DES");
//加密
//3吴攒,設(shè)置操作模式(加密/解密)
cipher.init(Cipher.ENCRYPT_MODE, key);
//4张抄,執(zhí)行加密
byte[] relBytes = cipher.doFinal(input.getBytes());
//注意:加密過后用Base64編碼 缺少這步會(huì)導(dǎo)致解密失敗
byte[] relBase = Base64.encode(relBytes, Base64.DEFAULT);
String encodeStr = new String(relBase);
Log.d("Simon", "des ENCRYPT_MODE: " + encodeStr);
//解密
//3,設(shè)置操作模式(加密/解密)
cipher.init(Cipher.DECRYPT_MODE, key);
//4洼怔,執(zhí)行解密
//先用Base64解密 缺少Base64編碼會(huì)導(dǎo)致解密失敗
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署惯、
//非對(duì)稱加密,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 = "加密內(nèi)容";
Log.i("Simon", "原文:" + Base64.encode(str.getBytes(), Base64.DEFAULT));
byte[] code1 = encryptByPublickey(str.getBytes(), pubKey);
Log.i("Simon", "公鑰加密后的數(shù)據(jù):" + Base64.encode(code1, Base64.DEFAULT));
byte[] code2 = decryptByPrivateKey(code1, priKey);
Log.i("Simon", "私鑰解密后的數(shù)據(jù):" + 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負(fù)責(zé)加密解密
cipher.init(Cipher.ENCRYPT_MODE, publicKey);
return cipher.doFinal(data);
}
/**
* 私鑰解密
*
* @param data 待解密數(shù)據(jù)
* @param key 密鑰
* @return byte[] 解密數(shù)據(jù)
*/
private byte[] decryptByPrivateKey(byte[] data, byte[] key) throws Exception {
//取得私鑰
PKCS8EncodedKeySpec pkcs8KeySpec = new PKCS8EncodedKeySpec(key);
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
//生成私鑰
PrivateKey privateKey = keyFactory.generatePrivate(pkcs8KeySpec);
//數(shù)據(jù)解密
Cipher cipher = Cipher.getInstance(keyFactory.getAlgorithm());
cipher.init(Cipher.DECRYPT_MODE, privateKey);
return cipher.doFinal(data);
}
}