Android RSA非对称加密/解密

您所在的位置:网站首页 rsa加密解密公式 Android RSA非对称加密/解密

Android RSA非对称加密/解密

2023-03-14 22:03| 来源: 网络整理| 查看: 265

/** * Created by Xinghai.Zhao on 17/08/28. */public class RSAUtils { private static final String RSA = "RSA"; //公钥 private static final String PUBLIC_KEY = "填写你的公钥"; //私钥 private static final String PRIVATE_KEY = "填写你的私钥";

/** 非对称加密 */ public static String getEncodeData(String s) { try { byte[] buffer = Base64.decode(PUBLIC_KEY, Base64.DEFAULT); KeyFactory keyFactory = KeyFactory.getInstance(RSA); X509EncodedKeySpec keySpec = new X509EncodedKeySpec(buffer); PublicKey publicKey = keyFactory.generatePublic(keySpec); Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding"); cipher.init(Cipher.ENCRYPT_MODE, publicKey); byte[] bytes = cipher.doFinal(s.getBytes()); return Base64.encodeToString(bytes, Base64.DEFAULT); } catch (Exception e) { e.printStackTrace(); return ""; } } /** 非对称解密 */ public static String getDecodeData(String s) { try { byte[] decode = Base64.decode(PRIVATE_KEY, Base64.DEFAULT); PrivateKey privateKey = KeyFactory.getInstance(RSA).generatePrivate(new PKCS8EncodedKeySpec(decode)); Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding"); cipher.init(Cipher.DECRYPT_MODE, privateKey); byte[] result = cipher.doFinal(Base64.decode(s,Base64.DEFAULT)); return new String(result); } catch (Exception e) { e.printStackTrace(); return ""; } }

}



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3