Android examples for java.security:Key
Decrypts a message encrypted with the user's public key by using their private key
//package com.java2s; import android.util.Log; import java.security.InvalidKeyException; import java.security.NoSuchAlgorithmException; import java.security.NoSuchProviderException; import java.security.PrivateKey; import javax.crypto.BadPaddingException; import javax.crypto.Cipher; import javax.crypto.IllegalBlockSizeException; import javax.crypto.NoSuchPaddingException; public class Main { /**/*from w w w .j a v a2 s. c om*/ * * Decrypts a message encrypted with the user's public key by using their private key * @see java.security.interfaces.RSAPrivateKey * * @param encBarr * @param privateKey * @return * @throws NoSuchAlgorithmException * @throws NoSuchPaddingException * @throws InvalidKeyException * @throws IllegalBlockSizeException * @throws NoSuchProviderException */ public static String RSADecrypt(final byte[] encBarr, PrivateKey privateKey) throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, IllegalBlockSizeException, NoSuchProviderException { String res = null; Cipher cipher1 = Cipher.getInstance("RSA/ECB/PKCS1Padding", "AndroidOpenSSL"); cipher1.init(Cipher.DECRYPT_MODE, privateKey); byte[] decBarr = new byte[0]; try { decBarr = cipher1.doFinal(encBarr); } catch (BadPaddingException e) { Log.i("myApp", "BadPadding"); e.printStackTrace(); } if (decBarr != null) res = new String(decBarr); return res; } }