decrypt By RSA Public Key - Java Security

Java examples for Security:RSA

Description

decrypt By RSA Public Key

Demo Code


import javax.crypto.Cipher;
import java.math.BigInteger;
import java.security.*;
import java.security.interfaces.RSAPrivateKey;
import java.security.interfaces.RSAPublicKey;
import java.security.spec.RSAPrivateKeySpec;
import java.security.spec.RSAPublicKeySpec;
import java.util.HashMap;

public class Main{
    //from  w ww .  jav a2s  .co  m
    public static String decryptByPublicKey(String data, RSAPublicKey key)
            throws Exception {
        Cipher cipher = Cipher.getInstance("RSA/ECB/NoPadding");
        //??
        int key_len = 0;
        cipher.init(Cipher.DECRYPT_MODE, key);
        key_len = key.getModulus().bitLength() / 8;
        byte[] x = data.getBytes();
        byte[] x1 = HexUtil.Hex2Bytes(data);
        return HexUtil.bytes2Hex(cipher.doFinal(x1));
    }
}

Related Tutorials