Usa la chiave openssl per decodificare il file in android / java [closed]

-1

Voglio decodificare il file crittografato in openssl in java. Il file è crittografato con la chiave privata usando openssl e sul lato java voglio decifrarlo usando la chiave pubblica.

Il codice:

public void testRSA() throws InvalidKeySpecException, NoSuchAlgorithmException, IOException, IllegalBlockSizeException, InvalidKeyException, BadPaddingException, NoSuchPaddingException {
    String rawPublicKey = new String(StreamUtils.loadBytes(getContext().getAssets().open("public.key")));
    Log.d(TAG, rawPublicKey);
    rawPublicKey = rawPublicKey.replace("-----BEGIN PUBLIC KEY-----\n", "");
    rawPublicKey = rawPublicKey.replace("-----END PUBLIC KEY-----", "");
    byte[] encoded = Base64.decode(rawPublicKey, 0);
    PublicKey publicKey = SignUtils.getAsPublicKey(encoded);

    byte[] encryptedFile = StreamUtils.loadBytes(getContext().getAssets().open("encrypted_file.bin"));
    String encryptedFileString = new String(encryptedFile);

    //STACK_NOTE: This file contains 3 parts but only first is used by this code.
    // encryptedFileParts[0] is equal to test_file from OpenSSL test
    String[] encryptedFileParts = encryptedFileString.split(";");
    Log.d(TAG, new String(encryptedFileParts[0].getBytes()));

    byte[] encryptedKey = Base64.decode(encryptedFileParts[0].getBytes(), 0);

    Log.d(TAG, new String(encryptedKey));

    Cipher cipher = Cipher.getInstance("RSA");
    cipher.init(Cipher.DECRYPT_MODE, publicKey);
    byte[] bytesPassword = cipher.doFinal(encryptedKey);

    String passwd = new String(bytesPassword, "UTF8");
    Log.d(TAG, "Recieved password = " + passwd);
    assertEquals("@rm@gEdon2014", passwd);
}

L'errore:

junit.framework.ComparisonFailure: expected:<[@rm@gEdon2014]>
but was:<[�����(...a lot of these...)��������@rm@gEdon2014
]>

Test OpenSSL:

$ openssl enc -d -base64 -in test_file -out test_file_dec
$ openssl rsautl -verify -inkey public.key -in test_file_dec -pubin
@rm@gEdon2014

Sorprendi tutto è ok!

I file:

public.key

-----BEGIN PUBLIC KEY-----
MIICIjANBgkqhkiG9w0BAQEFAAOCAg8AMIICCgKCAgEAxOIG72Ri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apN4qabqZ/NXpfA+jNE5Pfi
ckoUi1A2utmnM4H6HeL8Ap+QHcAr7G5NBjahxMF5X0YVmEtGhzMt3Dt+S3jhNfPS
tk5Ic6rqmgvipc6kG5ApTgwgRhn+ferEG+KQIG0hkAtP6yEoJw5unznld75Y/t7y
djdnb71HNPRVoxVc4aI96tAJSrqXHjmYMxpEWKKb1VTWxnn77vjdGdU7sqlipGCU
jc+k2csUICaJzg4CjGykTsRF8cDkUlAcN8ZPgdVJWGWghvrUgDurYMmic2aaqGvQ
zYxl1ErE8y92z0tQKXGYWMAHQvyB7Gpga1km1oDURdM=

La domanda:

Da dove vengono questi segni and e come risolverlo?

    
posta Adam Radomski 05.01.2015 - 13:11
fonte

1 risposta

1

Difficilmente una domanda di sicurezza, ma in breve quelli sono gli '0 del tuo byte [].

E.A. devi SANITIZZARE la stringa decifrata prima di confrontare / stampare sullo schermo.

Ciò che siamo qui al lavoro chiama " PEBCAK "

    
risposta data 05.01.2015 - 14:49
fonte

Leggi altre domande sui tag