非对称加密算法RSA介绍
RSA:到2008年为止,世界上还没有任何可靠的攻击RSA算法的方式
秘钥对:公钥和私钥,秘钥对不能手动指定,必须有系统生成
加密速度慢:必须分段加密,不能加密大文件
公钥加密私钥解密;私钥加密公钥解密
公钥互换:连个商家合作需要交互公钥,但是私钥不能别人
非对称加密RSA生成秘钥对
不能手动指定,必须由系统生成:公钥和私钥
public void click13(View view) { try { KeyPairGenerator generator = KeyPairGenerator.getInstance("RSA"); //generator.initialize(1024); KeyPair keyPair = generator.generateKeyPair(); PrivateKey aPrivate = keyPair.getPrivate(); PublicKey aPublic = keyPair.getPublic(); aprivateKey = Base64.encode(aPrivate.getEncoded());//私钥字符串 apublicKey = Base64.encode(aPublic.getEncoded());//公钥字符串 Log.e(TAG, "click13: " + aprivateKey); Log.e(TAG, "click13: " + apublicKey); } catch (Exception e) { e.printStackTrace(); } }
非对称加密RSA加密
公钥加密和私钥加密
/** * 私钥加密 * * @param str * @param privateKey * @return */ public static String encryptByPrivateKey(String str, PrivateKey privateKey) { try { byte[] bytes = str.getBytes(); Cipher cipher = Cipher.getInstance(TRANSFORMATION); cipher.init(Cipher.ENCRYPT_MODE, privateKey); int offset = 0;//偏移量 byte[] buffer = new byte[1024]; ByteArrayOutputStream bao = new ByteArrayOutputStream(); while (bytes.length - offset > 0) { if (bytes.length - offset >= MAX_ENCRYPT_SIZE) { buffer = cipher.doFinal(bytes, offset, MAX_ENCRYPT_SIZE); offset += MAX_ENCRYPT_SIZE; } else { buffer = cipher.doFinal(bytes, offset, bytes.length - offset); offset = bytes.length; } bao.write(bytes); } String encode = Base64.encode(bao.toByteArray()); return encode; } catch (Exception e) { e.printStackTrace(); } return null; } /** * 公钥加密 * * @param str * @param publicKey * @return */ public static String encryptByPublicKey(String str, PublicKey publicKey) { try { byte[] bytes = str.getBytes(); Cipher cipher = Cipher.getInstance(TRANSFORMATION); cipher.init(Cipher.ENCRYPT_MODE, publicKey); int offset = 0;//偏移量 byte[] buffer = new byte[1024]; ByteArrayOutputStream bao = new ByteArrayOutputStream(); while (bytes.length - offset > 0) { if (bytes.length - offset >= MAX_ENCRYPT_SIZE) { buffer = cipher.doFinal(bytes, offset, MAX_ENCRYPT_SIZE); offset += MAX_ENCRYPT_SIZE; } else { buffer = cipher.doFinal(bytes, offset, bytes.length - offset); offset = bytes.length; } bao.write(bytes); } String encode = Base64.encode(bao.toByteArray()); return encode; } catch (Exception e) { e.printStackTrace(); } return null; }
非对称加密RSA分段加密
RSA每次最大只能加密117个字节
超过117字节,分段加密
非对称加密RSA分段解密
/** * 公钥解密 * * @param str * @param publicKey * @return */ public static String decrypByPublicKey(String str, PublicKey publicKey) { try { byte[] bytes = Base64.decode(str); Cipher cipher = Cipher.getInstance(TAD); cipher.init(Cipher.DECRYPT_MODE, publicKey); int offset = 0; byte[] buffer = new byte[1024]; ByteArrayOutputStream bao = new ByteArrayOutputStream(); while (bytes.length - offset > 0) { if (bytes.length - offset >= MAX_DECRYP_SIZE) { buffer = cipher.doFinal(bytes, offset, MAX_DECRYP_SIZE); offset += MAX_DECRYP_SIZE; } else { buffer = cipher.doFinal(bytes, offset, bytes.length - offset); offset = bytes.length; } bao.write(bytes); } String encode = bao.toString(); return encode; } catch (Exception e) { e.printStackTrace(); } return null; } /** * 私钥解密 * * @param str * @param privateKey * @return */ public static String decrypByPrivateKey(String str, PrivateKey privateKey) { try { byte[] bytes = Base64.decode(str); Cipher cipher = Cipher.getInstance(TAD); cipher.init(Cipher.DECRYPT_MODE, privateKey); int offset = 0; byte[] buffer = new byte[1024]; ByteArrayOutputStream bao = new ByteArrayOutputStream(); while (bytes.length - offset > 0) { if (bytes.length - offset >= MAX_DECRYP_SIZE) { buffer = cipher.doFinal(bytes, offset, MAX_DECRYP_SIZE); offset += MAX_DECRYP_SIZE; } else { buffer = cipher.doFinal(bytes, offset, bytes.length - offset); offset = bytes.length; } bao.write(bytes); } String encode = bao.toString(); return encode; } catch (Exception e) { e.printStackTrace(); } return null; }
非对称加密保存秘钥对
每次都生成秘钥对:安卓加密有肯能IOS不能解密
第一次生成存储起来
public static PrivateKey getPrivateKey(String aprivateKey) { if (aprivateKey != null) { PRIVATE_KEY = aprivateKey; } PrivateKey privateKey = null; try { KeyFactory keyFactory = KeyFactory.getInstance(TAE); privateKey = keyFactory.generatePrivate(new PKCS8EncodedKeySpec(Base64.decode(PRIVATE_KEY))); } catch (Exception e) { e.printStackTrace(); } return privateKey; } public static PublicKey getPublicKey(String apublicKey) { if (apublicKey != null) { PUBLIC_KEY = apublicKey; } PublicKey publicKey = null; try { KeyFactory keyFactory = KeyFactory.getInstance(TAE); publicKey = keyFactory.generatePublic(new X509EncodedKeySpec(Base64.decode(PUBLIC_KEY))); } catch (Exception e) { e.printStackTrace(); } return publicKey; }