栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 移动开发 > Android

Android使用RSA加密实现接口调用时的校验功能

Android 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Android使用RSA加密实现接口调用时的校验功能

RSA算法是一种非对称加密算法,那么何为非对称加密算法呢?

一般我们理解上的加密是这样子进行的:原文经过了一把钥匙(密钥)加密后变成了密文,然后将密文传递给接收方,接收方再用这把钥匙(密钥)解开密文。在这个过程中,其实加密和解密使用的是同一把钥匙,这种加密方式称为对称加密。

而非对称加密就是和对称加密相对,加密用的钥匙和解密所用的钥匙,并不是同一把钥匙。非对称加密首先会创建两把钥匙,而这两把钥匙是成对的分别称为公钥和私钥。在进行加密时我们使用公钥进行加密,而在解密的时候就必须要使用私钥才能进行解密,这就是非对称加密算法。

假如使用非对称加密,甲发送消息给乙,这时候乙会预先创建好两把钥匙,私钥乙自己保存好,然后把公钥发送给甲,甲使用公钥对信息进行加密,然后传给乙。最后乙使用自己的私钥对数据进行解密。这个过程中,公钥还是有可能被第三者所截获,但是不同的是,这个第三者纵然得到了公钥,也无法解开密文,因为解密密文所需要的私钥从始至终一直在乙的手里。因此这个过程是安全的。

在一个Android应用中录音完成后将录音文件上传到SpringBoot搭建的后台接口中。

由于Android应用中没有登录功能,所以需要对一串自定义字符串进行加密并传输,然后在SpringBoot后台进行解密验证。防止上传接口暴露。

实现

首先在SpringBoot端新建一个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 = "自己生成的公钥";
 
 private static final int MAX_ENCRYPT_BLOCK = 117;

 
 private static final int MAX_DECRYPT_BLOCK = 128;

 
 public static KeyPair getKeyPair() throws Exception {
  KeyPairGenerator generator = KeyPairGenerator.getInstance("RSA");
  generator.initialize(1024);
  return generator.generateKeyPair();
 }

 
 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);
 }

 
 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);
 }

 
 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;
  // 对数据分段加密
  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();
  // 获取加密内容使用base64进行编码,并以UTF-8为标准转化成字符串
  // 加密后的字符串
  return new String(base64.encode((encryptedData)));
 }

 
 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;
  // 对数据分段解密
  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();
  // 解密后的内容
  return new String(decryptedData, "UTF-8");
 }

 
 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());
 }

 
 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));
 }




}

然后运行此工具类的main方法中的生成密钥对的方法,获取到生成的公钥和密钥对。

然后将它们赋值到最上面的privateKey和publicKey。

然后在Android端中也新建一个工具类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端同样的公钥";
 
 private static final int MAX_ENCRYPT_BLOCK = 117;

 
 private static final int MAX_DECRYPT_BLOCK = 128;

 

 
 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);
 }

 
 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;
  // 对数据分段加密
  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();
  // 获取加密内容使用base64进行编码,并以UTF-8为标准转化成字符串
  // 加密后的字符串
  return new String(base64.encode(encryptedData, base64.DEFAULT));
 }

}

这里的公钥与上面生成的公钥一致。

注意着两个工具类的区别

在Android工具类中的base64引入的是

import android.util.base64;

而在Java中引入的base64是

import com.sun.org.apache.xerces.internal.impl.dv.util.base64;

注意这里为什么不是引用java.util.base64,因为会有换行导致的转移字符的问题。

然后在Android中对字符串进行加密

//获取加密字符串
String escode = "";
try {
  escode = RsaUtils.encrypt(key,RsaUtils.getPublicKey(RsaUtils.publicKey));
 } catch (Exception e) {
  e.printStackTrace();
}

将其作为接口调用的参数传递到Java中进行解密

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("非法访问");
  }

这样就限制了只能通过指定的移动端对文件上传接口进行访问。

在移动端调用接口进行测试

可见调用接口前加密成功

并且能用过后台接口的解密校验

这样别的第三方请求接口就没法请求

以上就是Android使用RSA加密实现接口调用时的校验功能的详细内容,更多关于Android rsa加密接口调用的资料请关注考高分网其它相关文章!

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/153607.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号