RSA Decrypt Private - Android java.security

Android examples for java.security:RSA

Description

RSA Decrypt Private

Demo Code


//package com.java2s;
import android.util.Base64;

import java.security.KeyFactory;
import java.security.PrivateKey;

import java.security.spec.PKCS8EncodedKeySpec;

import javax.crypto.Cipher;

public class Main {
    public static byte[] RSADecryptPrivate(byte[] rawString, String key)
            throws Exception {
        Cipher cipher = Cipher.getInstance("RSA");
        byte[] pubKeyBytes = Base64.decode(key, Base64.CRLF);
        PrivateKey privateKey = KeyFactory.getInstance("RSA")
                .generatePrivate(new PKCS8EncodedKeySpec(pubKeyBytes));

        cipher.init(Cipher.DECRYPT_MODE, privateKey);
        byte[] encryptedBytes = cipher.doFinal(rawString);
        return encryptedBytes;
    }//from   w w w.  j a v  a 2  s .c  o m
}

Related Tutorials