在Android中的RSA 512位实现没有PADDING

时间:2018-02-12 15:39:36

标签: java android rsa public-key-encryption private-key

我的问题是如何在Android中使用RSA 512位;我能够通过在互联网上查看来生成公钥和私钥,但不确定它是否没有填充,因为我找不到任何解决方案。公钥也将被发送到服务器,并使用加密的整数私钥。 我是新手,所以有一些帮助会很棒。谢谢!

genreateKeys(" RSA" 512)

以下代码:

    private static void generateKeys(String keyAlgorithm, int numBits) {

    try {
        // Get the public/private key pair
        KeyPairGenerator keyGen = KeyPairGenerator.getInstance(keyAlgorithm);
        keyGen.initialize(numBits);
        KeyPair keyPair = keyGen.genKeyPair();
        PrivateKey privateKey = keyPair.getPrivate();
        PublicKey publicKey = keyPair.getPublic();

        System.out.println("\n" + "Generating key/value pair using " + privateKey.getAlgorithm() + " algorithm");

        // Get the bytes of the public and private keys
        byte[] privateKeyBytes = privateKey.getEncoded();
        byte[] publicKeyBytes = publicKey.getEncoded();

        // Get the formats of the encoded bytes
        String formatPrivate = privateKey.getFormat(); // PKCS#8
        String formatPublic = publicKey.getFormat(); // X.509

        System.out.println("Private Key : " + HttpRequest.Base64.encode(String.valueOf(privateKeyBytes)));
        System.out.println("Public Key : " + HttpRequest.Base64.encode(String.valueOf(publicKeyBytes)));

        // The bytes can be converted back to public and private key objects
        KeyFactory keyFactory = KeyFactory.getInstance(keyAlgorithm);
        EncodedKeySpec privateKeySpec = new PKCS8EncodedKeySpec(privateKeyBytes);
        PrivateKey privateKey2 = keyFactory.generatePrivate(privateKeySpec);

        EncodedKeySpec publicKeySpec = new X509EncodedKeySpec(publicKeyBytes);
        PublicKey publicKey2 = keyFactory.generatePublic(publicKeySpec);

        // The original and new keys are the same
        System.out.println("  Are both private keys equal? " + privateKey.equals(privateKey2));
        System.out.println("  Are both public keys equal? " + publicKey.equals(publicKey2));
    } catch (InvalidKeySpecException specException) {
        System.out.println("Exception");
        System.out.println("Invalid Key Spec Exception");
    } catch (NoSuchAlgorithmException e) {
        System.out.println("Exception");
        System.out.println("No such algorithm: " + keyAlgorithm);
    }

}

它为我提供了公钥和私钥的输出,但它没有没有PADDING我怎样才能使用私钥加密我的整数值?

0 个答案:

没有答案