rsa Encrypt byte array - Android java.security

Android examples for java.security:RSA

Description

rsa Encrypt byte array

Demo Code


//package com.java2s;

import javax.crypto.*;

import java.security.*;

public class Main {

    public static byte[] rsaEncrypt(byte[] data, Key key) {
        byte[] ret = null;
        if (data != null && data.length > 0 && key != null) {
            try {

                Cipher cipher = Cipher.getInstance("RSA");
                cipher.init(Cipher.ENCRYPT_MODE, key);

                ret = cipher.doFinal(data);

            } catch (NoSuchAlgorithmException e) {
                e.printStackTrace();/*w w  w  . j a va2  s .com*/
            } catch (NoSuchPaddingException e) {
                e.printStackTrace();
            } catch (InvalidKeyException e) {
                e.printStackTrace();
            } catch (BadPaddingException e) {
                e.printStackTrace();
            } catch (IllegalBlockSizeException e) {
                e.printStackTrace();
            }
        }
        return ret;
    }
}

Related Tutorials