在android中使用键和指数进行RSA加密

时间:2012-03-30 09:25:26

标签: android encryption rsa

我有一个私钥和指数,我需要在android应用程序中实现字符串的RSA加密。 我怎样才能做到这一点?是否有RSA加密的默认类?

1 个答案:

答案 0 :(得分:2)

public void saveToFile(String fileName, BigInteger mod, BigInteger exp)
        throws IOException {
    ObjectOutputStream oout = new ObjectOutputStream(
            new BufferedOutputStream(new FileOutputStream(fileName)));
    try {
        oout.writeObject(mod);
        oout.writeObject(exp);
    } catch (Exception e) {
        throw new IOException("Unexpected error", e);
    } finally {
        oout.close();
    }
}

PublicKey ReadPublicKeyFromFile(String keyFileName) throws IOException {
    InputStream in = RSACrypt.class.getClassLoader().getResourceAsStream(keyFileName);
    ObjectInputStream oin = new ObjectInputStream(new BufferedInputStream(
            in));
    try {
        BigInteger m = (BigInteger) oin.readObject();
        BigInteger e = (BigInteger) oin.readObject();
        RSAPublicKeySpec keySpec = new RSAPublicKeySpec(m, e);
        KeyFactory fact = KeyFactory.getInstance("RSA");
        PublicKey pubKey = fact.generatePublic(keySpec);
        return pubKey;
    } catch (Exception e) {
        throw new RuntimeException("Spurious serialisation error", e);
    } finally {
        oin.close();
    }
}

来自http://www.developpez.net/forums/d1001867/java/developpement-web-java/besoin-daide-rsa/

相关问题