encrypt By RSA Private Key - Java Security

Java examples for Security:RSA

Description

encrypt By RSA Private 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{
    //  w  ww. j  a v a  2s. c  om
    public static String encryptByPrivateKey(String data, RSAPrivateKey key)
            throws Exception {
        Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");

        int key_len = 0;
        cipher.init(Cipher.ENCRYPT_MODE, key);
        key_len = key.getModulus().bitLength() / 8;
        byte[] bData = data.getBytes();
        return HexUtil.bytes2Hex(cipher.doFinal(bData));
    }
}

Related Tutorials