RSA decrypt Base64 Text - Android java.security

Android examples for java.security:RSA

Description

RSA decrypt Base64 Text

Demo Code


//package com.java2s;

import java.security.Key;

import javax.crypto.Cipher;

import android.util.Base64;

public class Main {
    public static String decryptBase64Text(Key key, String text) {
        try {/*from  w  ww.  j  av  a2  s.c  o m*/
            byte bytes[] = Base64.decode(text, Base64.DEFAULT);
            return decryptBytes(key, bytes);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    public static String decryptBytes(Key key, byte[] bytes) {
        try {
            Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");
            cipher.init(Cipher.DECRYPT_MODE, key);
            byte[] enBytes = cipher.doFinal(bytes);
            return new String(enBytes);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
}

Related Tutorials