public boolean generateKeys() { PublicKey keyPub; PrivateKey keyPri; SecureRandom rand; Security.addProvider(new ABAProvider()); rand = new SecureRandom(); rand.setSeed(System.currentTimeMillis()); try { KeyPairGenerator fact; KeyPair keyPair; fact = KeyPairGenerator.getInstance("RSA", "ABA"); fact.initialize(1024, rand); keyPair = fact.generateKeyPair(); keyPub = keyPair.getPublic(); keyPri = keyPair.getPrivate(); pubKey = bytesToHexStr(keyPub.getEncoded()); priKey = bytesToHexStr(keyPri.getEncoded()); } catch (Exception e) { return false; } return true; }
public String generateSigature(String src) { try { Security.addProvider(new ABAProvider()); Signature sigEng = Signature.getInstance("MD5withRSA", "ABA"); byte[] pribyte = hexStrToBytes(priKey.trim()); sigEng.initSign(new RSAPrivKeyCrt(pribyte)); sigEng.update(src.getBytes()); byte[] signature = sigEng.sign(); return bytesToHexStr(signature); } catch (Exception e) { return null; } }
public boolean verifySigature(String sign, String src) { try { Security.addProvider(new ABAProvider()); Signature sigEng = Signature.getInstance("MD5withRSA", "ABA"); byte[] pubbyte = hexStrToBytes(pubKey.trim()); sigEng.initVerify(new RSAPubKey(pubbyte)); sigEng.update(src.getBytes()); byte[] sign1 = hexStrToBytes(sign); if (sigEng.verify(sign1)) { return true; } else { return false; } } catch (Exception e) { return false; } }