Unknown characters at starting while parsing from private key in Asymmetric Algorithm

好久不见. 提交于 2020-04-17 20:37:09

问题


I am trying to implement asymmetric algorithm in Android. I have managed to do it all. But I am facing an issue while output.

What I am getting after decrypting through private key is(Which contains actual text in the end):

�������\�C���gz�{ܳkK��c��LB(7fz���H�8�� ��ȏ�ҍ�X    ��KI��aj�B��x�D�n>�ι�&
    UL-1���E�;��s9�zB[�9B�t��B�A�s�GA6����y�f���J�����+.
    `g4�s�1��p�PW�%��7�"��fQ����G���x� �|&�ѡ�gq������:X�be�_��av��{'msg':'Laravel 7.7777777'}

Actual message is :

{'msg':'Laravel 7.7777777'}

You can clearly see it is present in the decrypted String. I know it is due to some string or some other silly mistake or something like that. But I am unable to get that. Even my encrypted data also have the same issue when it reaches to server. But when I am encrypting and decrypting in my end it works fine. It happens when key is interchanged.

Here, I am attaching my actual class which is responsible for Asymmetric encryption. With public and private key. Along with the encrypted text to be decrypted.

public class AsymmetricHandler {

    String ALGO_TYPE = "RSA";
    String CIPER_TYPE = "RSA";
//    String CIPER_TYPE = "RSA/ECB/OAEPWithSHA-1AndMGF1Padding";

    private static AsymmetricHandler instance = null;

    public static AsymmetricHandler getInstance() {
        if (instance == null) {
            Security.setProperty("crypto.policy", "unlimited");
            instance = new AsymmetricHandler();
        }
        return instance;
    }


    public PublicKey getActualPublicKey(String base64EncryptedStringKey) throws Exception {
        KeyFactory kf = KeyFactory.getInstance(ALGO_TYPE);
        X509EncodedKeySpec keySpecX509 = new X509EncodedKeySpec(Base64.decode(base64EncryptedStringKey.replace("-----BEGIN PUBLIC KEY-----", "").replace("-----END PUBLIC KEY-----", ""), Base64.DEFAULT));

        return kf.generatePublic(keySpecX509);
    }

    public PrivateKey getActualPrivateKey(String base64EncryptedStringKey) throws Exception {
        KeyFactory kf = KeyFactory.getInstance(ALGO_TYPE);
        PKCS8EncodedKeySpec keySpecPKCS8 = new PKCS8EncodedKeySpec(Base64.decode(base64EncryptedStringKey.replace("-----BEGIN PRIVATE KEY-----", "").replace("-----END PRIVATE KEY-----", ""), Base64.DEFAULT));

        return kf.generatePrivate(keySpecPKCS8);
    }

    public String decodeBase64String(String value) {
        return new String(Base64.decode(value, Base64.DEFAULT));
    }

    public String encodeToBase64String(String value) {
        return new String(Base64.encode(value.getBytes(), Base64.DEFAULT));
    }


    // Get RSA keys. Uses key size of 2048.
    public Map<String, Object> getRSAKeys() throws Exception {
        KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance(ALGO_TYPE);
        keyPairGenerator.initialize(2048);
        KeyPair keyPair = keyPairGenerator.generateKeyPair();
        PrivateKey privateKey = keyPair.getPrivate();
        PublicKey publicKey = keyPair.getPublic();

        Map<String, Object> keys = new HashMap<String, Object>();
        keys.put("private", privateKey);
        keys.put("public", publicKey);
        return keys;
    }


    public String encryptString(String msg, PublicKey key)
            throws NoSuchAlgorithmException, NoSuchPaddingException,
            IllegalBlockSizeException,
            BadPaddingException, InvalidKeyException {

//        Cipher cipher = Cipher.getInstance("RSA/ECB/OAEPWithSHA-1AndMGF1Padding");
        Cipher cipher = Cipher.getInstance(CIPER_TYPE);
        cipher.init(Cipher.ENCRYPT_MODE, key);
        return Base64.encodeToString(cipher.doFinal(msg.getBytes(StandardCharsets.UTF_8)), Base64.DEFAULT);
    }

    public String decryptString(String msg, PrivateKey key)
            throws InvalidKeyException,
            IllegalBlockSizeException, BadPaddingException {

        try {
//            Cipher cipher = Cipher.getInstance("RSA/ECB/OAEPWithSHA-1AndMGF1Padding");
            Cipher cipher = Cipher.getInstance(CIPER_TYPE);
            cipher.init(Cipher.DECRYPT_MODE, key);
            return new String(cipher.doFinal(Base64.decode(msg, Base64.DEFAULT)), StandardCharsets.UTF_8);


        } catch (NoSuchAlgorithmException | NoSuchPaddingException e) {
            e.printStackTrace();
        }

        return null;
    }

}

Here are sample key and value which I am using.

 String stringPublicKey = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEApSNOjR5QWU4H24OAhPHaJwq7/9Nqzcfii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+IOnoZ8njCIc15DD/YN9fBRIifw+zy+EFpCWLlzDeejR2wwqVyENQSmfiChC4EVnULqfHfeSWOqK/QVATYC6ljkr+G4QQQ82VQ8sRGS6x40ClKINTerqKxhSpwNoNuaDqmBkHw=="

回答1:


Your mistake is that you are using plain RSA without padding when decrypting your encrypted data.

When encrypting data with RSA the data size has to be of a certain length. If the data to be encrypted is too short it has to enlarged to the correct size by prepending padding bytes (which is obviously done by the code that encrypts the data).

The problem is not that you are decrypting the data without padding and hence you are getting the padding data plus the decrypted message.

Therefore make sure to use for encryption and decryption the same padding algorithm. Your code already specifies OAEPWithSHA-1AndMGF1Padding as padding but the relevant parts are commented out.

Use RSA/ECB/OAEPWithSHA-1AndMGF1Padding for encryption and decryption and the padding data will be automatically removed upon decryption.



来源:https://stackoverflow.com/questions/61256128/unknown-characters-at-starting-while-parsing-from-private-key-in-asymmetric-algo

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!