Google支付Java服務端訂單校驗 原文鏈接:https://blog.csdn.net/qq_40193787/article/details/123507578 一、Google Play 結算系統概覽 Google Play 結算系統是一項可讓您在 Android 應用中銷售數字產品和內容 ...
生成秘鑰簡單粗暴
import java.io.UnsupportedEncodingException; import java.security.KeyPair; import java.security.KeyPairGenerator; import java.security.NoSuchAlgorithmException; import java.security.SecureRandom; import java.security.interfaces.RSAPrivateKey; import java.security.interfaces.RSAPublicKey; import java.util.Base64; import java.util.HashMap; import java.util.Map; import java.util.Random; /** * @ClassName: SecretKeyUtils * 各秘鑰生成 * @author zhupig3028 * @date 2022年5月10日 */ public class SecretKeyUtils { /** AES秘鑰 */ private static final String AES_ALGORITHM = "AES"; /** 3DES秘鑰 */ private static final String DES_ALGORITHM = "DESede"; /** 秘鑰生成來源 */ public static final String ALLCHAR = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"; /** * 生成AES密鑰對象 * @throws NoSuchAlgorithmException */ public static String generateAESKey() throws NoSuchAlgorithmException, UnsupportedEncodingException { StringBuffer sb = new StringBuffer(); Random random = new Random(); for (int i = 0; i < 16; i++) { sb.append(ALLCHAR.charAt(random.nextInt(ALLCHAR.length()))); } return sb.toString(); } /** * 生成3DES密鑰對象 */ public static String generate3DESKey() { StringBuffer sb = new StringBuffer(); Random random = new Random(); for (int i = 0; i < 24; i++) { sb.append(ALLCHAR.charAt(random.nextInt(ALLCHAR.length()))); } return sb.toString(); } /** * 隨機生成RSA秘鑰 * @throws NoSuchAlgorithmException */ public static Map<String,String > genKeyPair() throws NoSuchAlgorithmException { HashMap<String, String> stringStringHashMap = new HashMap<>(); // KeyPairGenerator類用於生成公鑰和私鑰對,基於RSA演算法生成對象 KeyPairGenerator keyPairGen = KeyPairGenerator.getInstance("RSA"); // 初始化密鑰對生成器,密鑰大小為96-1024位 keyPairGen.initialize(1024,new SecureRandom()); // 生成一個密鑰對,保存在keyPair中 KeyPair keyPair = keyPairGen.generateKeyPair(); // 得到私鑰 RSAPrivateKey privateKey = (RSAPrivateKey) keyPair.getPrivate(); // 得到公鑰 RSAPublicKey publicKey = (RSAPublicKey) keyPair.getPublic(); String publicKeyString = new String(Base64.getEncoder().encodeToString(publicKey.getEncoded())); // 得到私鑰字元串 String privateKeyString = new String(Base64.getEncoder().encodeToString((privateKey.getEncoded()))); // 將公鑰和私鑰保存到Map //0表示公鑰 stringStringHashMap.put("0",publicKeyString); //1表示私鑰 stringStringHashMap.put("1",privateKeyString); return stringStringHashMap; } }
Java實現3DES加解密(這裡採用了ECB模式,填充模式為pkcs5padding)ECB模式沒有偏移量
import javax.crypto.Cipher; import javax.crypto.SecretKey; import javax.crypto.spec.SecretKeySpec; import java.util.Base64; /** * @author zhupig3028 * @date 2022/5/10 14:25 * 3DES加密工具類 */ public class ThreeDESUtils { /** * 加解密統一編碼方式 */ private final static String ENCODING = "utf-8"; /** * 加解密方式 */ private final static String ALGORITHM = "DESede"; /** *加密模式及填充方式 */ private final static String PATTERN = "DESede/ECB/pkcs5padding"; /** * 3DES加密 * * @param plainText 普通文本 * @param sK 秘鑰 * @return * @throws Exception */ public static String encode(String plainText,String sK) throws Exception { SecretKey secretKey = new SecretKeySpec(sK.getBytes(ENCODING), ALGORITHM); // 3DES加密採用pkcs5padding填充 Cipher cipher = Cipher.getInstance(PATTERN); // 用密匙初始化Cipher對象 cipher.init(Cipher.ENCRYPT_MODE, secretKey); // 執行加密操作 byte[] encryptData = cipher.doFinal(plainText.getBytes(ENCODING)); return Base64.getEncoder().encodeToString(encryptData); } /** * 3DES解密 * * @param encryptText 加密文本 * @return * @throws Exception */ public static String decode(String encryptText, String sK) throws Exception { SecretKey secretKey = new SecretKeySpec(sK.getBytes(ENCODING), ALGORITHM); // 3DES加密採用pkcs5padding填充 Cipher cipher = Cipher.getInstance(PATTERN); // 用密匙初始化Cipher對象 cipher.init(Cipher.DECRYPT_MODE, secretKey); // 正式執行解密操作 byte[] decryptData = cipher.doFinal(Base64.getDecoder().decode(encryptText)); return new String(decryptData, ENCODING); } }