RSA encrypt byte array with Key - Android java.security

Android examples for java.security:RSA

Description

RSA encrypt byte array with Key

Demo Code


//package com.java2s;

import android.util.Log;

import java.security.Key;

import javax.crypto.Cipher;

public class Main {
    public static byte[] encrypt(Key publicKey, byte[] toBeCiphred) {
        try {/*from  w ww  .  j  a va2s  .  c o m*/
            Cipher rsaCipher = Cipher.getInstance(
                    "RSA/ECB/OAEPWithSHA1AndMGF1Padding", "BC");
            rsaCipher.init(Cipher.ENCRYPT_MODE, publicKey);
            return rsaCipher.doFinal(toBeCiphred);
        } catch (Exception e) {
            Log.e("RSAHelper",
                    "Error while encrypting data: " + e.getMessage());
            throw new RuntimeException(e);
        }
    }
}

Related Tutorials