Java examples for Security:RSA
encrypt By RSA Public Key
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 w w . j a v a2 s .co m public static String encryptByPublicKey(String data, RSAPublicKey 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)); } }