RSA шифрование с ключом и показателем в андроиде - PullRequest
1 голос
/ 30 марта 2012

У меня есть закрытый ключ и показатель степени, мне нужно реализовать шифрование строки RSA в приложении для Android. Как я могу это сделать? Существует ли какой-либо класс по умолчанию для шифрования RSA?

1 Ответ

2 голосов
/ 30 марта 2012
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/

...