RSA Encrypt String. - Java Security

Java examples for Security:RSA

Description

RSA Encrypt String.

Demo Code


//package com.java2s;

import java.security.interfaces.RSAPublicKey;

import javax.crypto.Cipher;

public class Main {
    /**/*from www  .  j a  va  2  s.c  om*/
     * Encrypt String.
     * 
     * @return byte[]
     */
    protected static byte[] encrypt(RSAPublicKey publicKey, byte[] data) {
        if (publicKey != null) {
            try {
                Cipher cipher = Cipher.getInstance("RSA");
                cipher.init(Cipher.ENCRYPT_MODE, publicKey);
                return cipher.doFinal(data);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return null;
    }
}

Related Tutorials