Basic RSA decrypt method - Java Security

Java examples for Security:RSA

Description

Basic RSA decrypt method

Demo Code


//package com.java2s;

import java.security.interfaces.RSAPrivateKey;

import javax.crypto.Cipher;

public class Main {
    /**//from w  w w. ja  v a  2  s  .c  o  m
     * Basic decrypt method
     * 
     * @return byte[]
     */
    protected static byte[] decrypt(RSAPrivateKey privateKey, byte[] raw) {
        if (privateKey != null) {
            try {
                Cipher cipher = Cipher.getInstance("RSA");
                cipher.init(Cipher.DECRYPT_MODE, privateKey);
                return cipher.doFinal(raw);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        return null;
    }
}

Related Tutorials