Android使用RSA加密實(shí)現(xiàn)接口調(diào)用時(shí)的校驗(yàn)功能
RSA算法是一種非對(duì)稱加密算法,那么何為非對(duì)稱加密算法呢?
一般我們理解上的加密是這樣子進(jìn)行的:原文經(jīng)過(guò)了一把鑰匙(密鑰)加密后變成了密文,然后將密文傳遞給接收方,接收方再用這把鑰匙(密鑰)解開(kāi)密文。在這個(gè)過(guò)程中,其實(shí)加密和解密使用的是同一把鑰匙,這種加密方式稱為對(duì)稱加密。
而非對(duì)稱加密就是和對(duì)稱加密相對(duì),加密用的鑰匙和解密所用的鑰匙,并不是同一把鑰匙。非對(duì)稱加密首先會(huì)創(chuàng)建兩把鑰匙,而這兩把鑰匙是成對(duì)的分別稱為公鑰和私鑰。在進(jìn)行加密時(shí)我們使用公鑰進(jìn)行加密,而在解密的時(shí)候就必須要使用私鑰才能進(jìn)行解密,這就是非對(duì)稱加密算法。
假如使用非對(duì)稱加密,甲發(fā)送消息給乙,這時(shí)候乙會(huì)預(yù)先創(chuàng)建好兩把鑰匙,私鑰乙自己保存好,然后把公鑰發(fā)送給甲,甲使用公鑰對(duì)信息進(jìn)行加密,然后傳給乙。最后乙使用自己的私鑰對(duì)數(shù)據(jù)進(jìn)行解密。這個(gè)過(guò)程中,公鑰還是有可能被第三者所截獲,但是不同的是,這個(gè)第三者縱然得到了公鑰,也無(wú)法解開(kāi)密文,因?yàn)榻饷苊芪乃枰乃借€從始至終一直在乙的手里。因此這個(gè)過(guò)程是安全的。
在一個(gè)Android應(yīng)用中錄音完成后將錄音文件上傳到SpringBoot搭建的后臺(tái)接口中。
由于Android應(yīng)用中沒(méi)有登錄功能,所以需要對(duì)一串自定義字符串進(jìn)行加密并傳輸,然后在SpringBoot后臺(tái)進(jìn)行解密驗(yàn)證。防止上傳接口暴露。
實(shí)現(xiàn)首先在SpringBoot端新建一個(gè)RsaUtils工具類
import com.sun.org.apache.xerces.internal.impl.dv.util.Base64;import java.io.ByteArrayOutputStream;import java.security.KeyFactory;import java.security.KeyPair;import java.security.KeyPairGenerator;import java.security.PrivateKey;import java.security.PublicKey;import java.security.Signature;import java.security.spec.PKCS8EncodedKeySpec;import java.security.spec.X509EncodedKeySpec;import java.util.Base64;import javax.crypto.Cipher;//java 后端public class RsaUtils { //私鑰 public static String privateKey = '自己生成的私鑰'; //公鑰 private static String publicKey = '自己生成的公鑰'; /** * RSA最大加密明文大小 */ private static final int MAX_ENCRYPT_BLOCK = 117; /** * RSA最大解密密文大小 */ private static final int MAX_DECRYPT_BLOCK = 128; /** * 獲取密鑰對(duì) * * @return 密鑰對(duì) */ public static KeyPair getKeyPair() throws Exception { KeyPairGenerator generator = KeyPairGenerator.getInstance('RSA'); generator.initialize(1024); return generator.generateKeyPair(); } /** * 獲取私鑰 * * @param privateKey 私鑰字符串 * @return */ public static PrivateKey getPrivateKey(String privateKey) throws Exception { KeyFactory keyFactory = KeyFactory.getInstance('RSA'); byte[] decodedKey = com.sun.org.apache.xerces.internal.impl.dv.util.Base64.decode(new String(privateKey.getBytes())); PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(decodedKey); return keyFactory.generatePrivate(keySpec); } /** * 獲取公鑰 * * @param publicKey 公鑰字符串 * @return */ public static PublicKey getPublicKey(String publicKey) throws Exception { KeyFactory keyFactory = KeyFactory.getInstance('RSA'); byte[] decodedKey = Base64.decode(publicKey); X509EncodedKeySpec keySpec = new X509EncodedKeySpec(decodedKey); return keyFactory.generatePublic(keySpec); } /** * RSA加密 * * @param data 待加密數(shù)據(jù) * @param publicKey 公鑰 * @return */ public static String encrypt(String data, PublicKey publicKey) throws Exception { Cipher cipher = Cipher.getInstance('RSA'); cipher.init(Cipher.ENCRYPT_MODE, publicKey); int inputLen = data.getBytes().length; ByteArrayOutputStream out = new ByteArrayOutputStream(); int offset = 0; byte[] cache; int i = 0; // 對(duì)數(shù)據(jù)分段加密 while (inputLen - offset > 0) { if (inputLen - offset > MAX_ENCRYPT_BLOCK) { cache = cipher.doFinal(data.getBytes(), offset, MAX_ENCRYPT_BLOCK); } else { cache = cipher.doFinal(data.getBytes(), offset, inputLen - offset); } out.write(cache, 0, cache.length); i++; offset = i * MAX_ENCRYPT_BLOCK; } byte[] encryptedData = out.toByteArray(); out.close(); // 獲取加密內(nèi)容使用base64進(jìn)行編碼,并以UTF-8為標(biāo)準(zhǔn)轉(zhuǎn)化成字符串 // 加密后的字符串 return new String(Base64.encode((encryptedData))); } /** * RSA解密 * * @param data 待解密數(shù)據(jù) * @param privateKey 私鑰 * @return */ public static String decrypt(String data, PrivateKey privateKey) throws Exception { Cipher cipher = Cipher.getInstance('RSA'); cipher.init(Cipher.DECRYPT_MODE, privateKey); byte[] dataBytes = Base64.decode(data); int inputLen = dataBytes.length; ByteArrayOutputStream out = new ByteArrayOutputStream(); int offset = 0; byte[] cache; int i = 0; // 對(duì)數(shù)據(jù)分段解密 while (inputLen - offset > 0) { if (inputLen - offset > MAX_DECRYPT_BLOCK) { cache = cipher.doFinal(dataBytes, offset, MAX_DECRYPT_BLOCK); } else { cache = cipher.doFinal(dataBytes, offset, inputLen - offset); } out.write(cache, 0, cache.length); i++; offset = i * MAX_DECRYPT_BLOCK; } byte[] decryptedData = out.toByteArray(); out.close(); // 解密后的內(nèi)容 return new String(decryptedData, 'UTF-8'); } /** * 簽名 * * @param data 待簽名數(shù)據(jù) * @param privateKey 私鑰 * @return 簽名 */ public static String sign(String data, PrivateKey privateKey) throws Exception { byte[] keyBytes = privateKey.getEncoded(); PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(keyBytes); KeyFactory keyFactory = KeyFactory.getInstance('RSA'); PrivateKey key = keyFactory.generatePrivate(keySpec); Signature signature = Signature.getInstance('MD5withRSA'); signature.initSign(key); signature.update(data.getBytes()); return Base64.encode(signature.sign()); } /** * 驗(yàn)簽 * * @param srcData 原始字符串 * @param publicKey 公鑰 * @param sign 簽名 * @return 是否驗(yàn)簽通過(guò) */ public static boolean verify(String srcData, PublicKey publicKey, String sign) throws Exception { byte[] keyBytes = publicKey.getEncoded(); X509EncodedKeySpec keySpec = new X509EncodedKeySpec(keyBytes); KeyFactory keyFactory = KeyFactory.getInstance('RSA'); PublicKey key = keyFactory.generatePublic(keySpec); Signature signature = Signature.getInstance('MD5withRSA'); signature.initVerify(key); signature.update(srcData.getBytes()); return signature.verify(Base64.decode(sign)); }/* public static void main(String[] args) { try { // 生成密鑰對(duì) KeyPair keyPair = getKeyPair(); String privateKey = new String(Base64.getEncoder().encode(keyPair.getPrivate().getEncoded())); String publicKey = new String(Base64.getEncoder().encode(keyPair.getPublic().getEncoded())); System.out.println('私鑰:' + privateKey); System.out.println('公鑰:' + publicKey); // RSA加密*//* String data = '待加密的文字內(nèi)容'; String encryptData = encrypt(data, getPublicKey(publicKey)); System.out.println('加密后內(nèi)容:' + encryptData); // RSA解密 String decryptData = decrypt('encryptData ', getPrivateKey(privateKey)); System.out.println('解密后內(nèi)容:' + decryptData); // RSA簽名 String sign = sign(data, getPrivateKey(privateKey)); // RSA驗(yàn)簽 boolean result = verify(data, getPublicKey(publicKey), sign); System.out.print('驗(yàn)簽結(jié)果:' + result);*//* } catch (Exception e) { e.printStackTrace(); System.out.print('加解密異常'); } }*/}
然后運(yùn)行此工具類的main方法中的生成密鑰對(duì)的方法,獲取到生成的公鑰和密鑰對(duì)。
然后將它們賦值到最上面的privateKey和publicKey。
然后在Android端中也新建一個(gè)工具類RsaUtils
package com.badao.badaoimclient.common;import android.util.Base64;import java.io.ByteArrayOutputStream;import java.security.KeyFactory;import java.security.PublicKey;import java.security.spec.X509EncodedKeySpec;import javax.crypto.Cipher;public class RsaUtils{ //公鑰 public static String publicKey='跟Java端同樣的公鑰'; /** * RSA最大加密明文大小 */ private static final int MAX_ENCRYPT_BLOCK = 117; /** * RSA最大解密密文大小 */ private static final int MAX_DECRYPT_BLOCK = 128; /** * 獲取公鑰 * * @param publicKey 公鑰字符串 * @return */ public static PublicKey getPublicKey(String publicKey) throws Exception { KeyFactory keyFactory = KeyFactory.getInstance('RSA'); byte[] decodedKey =Base64.decode(publicKey.getBytes(), Base64.DEFAULT); X509EncodedKeySpec keySpec = new X509EncodedKeySpec(decodedKey); return keyFactory.generatePublic(keySpec); } /** * RSA加密 * * @param data 待加密數(shù)據(jù) * @param publicKey 公鑰 * @return */ public static String encrypt(String data, PublicKey publicKey) throws Exception { Cipher cipher ; cipher= Cipher.getInstance('RSA/ECB/PKCS1Padding'); cipher.init(Cipher.ENCRYPT_MODE, publicKey); int inputLen = data.getBytes().length; ByteArrayOutputStream out = new ByteArrayOutputStream(); int offset = 0; byte[] cache; int i = 0; // 對(duì)數(shù)據(jù)分段加密 while (inputLen - offset > 0) { if (inputLen - offset > MAX_ENCRYPT_BLOCK) { cache = cipher.doFinal(data.getBytes(), offset, MAX_ENCRYPT_BLOCK); } else { cache = cipher.doFinal(data.getBytes(), offset, inputLen - offset); } out.write(cache, 0, cache.length); i++; offset = i * MAX_ENCRYPT_BLOCK; } byte[] encryptedData = out.toByteArray(); out.close(); // 獲取加密內(nèi)容使用base64進(jìn)行編碼,并以UTF-8為標(biāo)準(zhǔn)轉(zhuǎn)化成字符串 // 加密后的字符串 return new String(Base64.encode(encryptedData, Base64.DEFAULT)); }}
這里的公鑰與上面生成的公鑰一致。
注意著兩個(gè)工具類的區(qū)別
在Android工具類中的Base64引入的是
import android.util.Base64;
而在Java中引入的Base64是
import com.sun.org.apache.xerces.internal.impl.dv.util.Base64;
注意這里為什么不是引用java.util.Base64,因?yàn)闀?huì)有換行導(dǎo)致的轉(zhuǎn)移字符的問(wèn)題。
然后在Android中對(duì)字符串進(jìn)行加密
//獲取加密字符串String escode = '';try { escode = RsaUtils.encrypt(key,RsaUtils.getPublicKey(RsaUtils.publicKey)); } catch (Exception e) { e.printStackTrace();}
將其作為接口調(diào)用的參數(shù)傳遞到Java中進(jìn)行解密
if(decode.equals(RsaUtils.decrypt(key,RsaUtils.getPrivateKey(RsaUtils.privateKey)))) { try { // 上傳文件路徑 String filePath = RuoYiConfig.getUploadPath(); // 上傳并返回新文件名稱 String fileName = FileUploadUtils.upload(filePath, file); String url = serverConfig.getUrl() + fileName; AjaxResult ajax = AjaxResult.success(); ajax.put('fileName', fileName); ajax.put('url', url); return ajax; } catch (Exception e) { return AjaxResult.error(e.getMessage()); } }else { return AjaxResult.error('非法訪問(wèn)'); }
這樣就限制了只能通過(guò)指定的移動(dòng)端對(duì)文件上傳接口進(jìn)行訪問(wèn)。
在移動(dòng)端調(diào)用接口進(jìn)行測(cè)試
可見(jiàn)調(diào)用接口前加密成功
并且能用過(guò)后臺(tái)接口的解密校驗(yàn)
這樣別的第三方請(qǐng)求接口就沒(méi)法請(qǐng)求
以上就是Android使用RSA加密實(shí)現(xiàn)接口調(diào)用時(shí)的校驗(yàn)功能的詳細(xì)內(nèi)容,更多關(guān)于Android rsa加密接口調(diào)用的資料請(qǐng)關(guān)注好吧啦網(wǎng)其它相關(guān)文章!
相關(guān)文章:
1. asp讀取xml文件和記數(shù)2. vue實(shí)現(xiàn)移動(dòng)端返回頂部3. PHP實(shí)現(xiàn)基本留言板功能原理與步驟詳解4. CSS自定義滾動(dòng)條樣式案例詳解5. xml中的空格之完全解說(shuō)6. Python如何實(shí)現(xiàn)感知器的邏輯電路7. 多個(gè)SpringBoot項(xiàng)目采用redis實(shí)現(xiàn)Session共享功能8. python利用opencv實(shí)現(xiàn)顏色檢測(cè)9. vue 驗(yàn)證兩次輸入的密碼是否一致的方法示例10. python基于scrapy爬取京東筆記本電腦數(shù)據(jù)并進(jìn)行簡(jiǎn)單處理和分析
