RSA decrypt Data - Java Security

Java examples for Security:RSA

Description

RSA decrypt Data

Demo Code


//package com.java2s;
import javax.crypto.Cipher;

import java.security.PrivateKey;
import java.security.PublicKey;

public class Main {
    private static String RSA = "RSA";

    public static byte[] decryptData(byte[] encryptedData,
            PrivateKey privateKey) {
        try {//  ww w.  j av a2  s  . c  o m
            Cipher cipher = Cipher.getInstance(RSA);
            cipher.init(Cipher.DECRYPT_MODE, privateKey);
            return cipher.doFinal(encryptedData);
        } catch (Exception e) {
            return null;
        }
    }

    public static byte[] decryptData(byte[] encryptedData,
            PublicKey publicKey) {
        try {
            Cipher cipher = Cipher.getInstance(RSA);
            cipher.init(Cipher.DECRYPT_MODE, publicKey);
            return cipher.doFinal(encryptedData);
        } catch (Exception e) {
            return null;
        }
    }
}

Related Tutorials