@Override public void setEopAppSecurityBean(EopAppSecurityBean bean) { try { KeyFactory keyFactory = KeyFactory.getInstance(ALGORITHM); String prikey = bean.getPrikey(); EncodedKeySpec privateKeySpec = new PKCS8EncodedKeySpec(Base64.decode(prikey)); privateKey = keyFactory.generatePrivate(privateKeySpec); String pubkey = bean.getPubkey(); EncodedKeySpec publicKeySpec = new X509EncodedKeySpec(Base64.decode(pubkey)); publicKey = keyFactory.generatePublic(publicKeySpec); } catch (NoSuchAlgorithmException e) { throw new RuntimeException(e); } catch (InvalidKeySpecException e) { throw new RuntimeException(e); } }
@Override public String encrypt(String value) { try { Cipher cipher = Cipher.getInstance(RSA_ECB_PKCS1_PADDING); cipher.init(Cipher.ENCRYPT_MODE, publicKey); return Base64.encodeToString(cipher.doFinal(ByteUtils.toBytes(value)), false); } catch (Exception e) { throw new RuntimeException(e); } }
@Override public String decrypt(String value) { try { Cipher cipher = Cipher.getInstance(RSA_ECB_PKCS1_PADDING); cipher.init(Cipher.DECRYPT_MODE, privateKey); return ByteUtils.toString(cipher.doFinal(Base64.decode(value))); } catch (Exception e) { throw new RuntimeException(e); } }