Java Security Encryption with PublicKey

keytool -genkeypair -alias kulcsnev -keyalg RSA -keystore keystore.jks

import java.io.FileInputStream;
import java.io.IOException;
import java.security.*;
import java.security.cert.Certificate;
import java.security.cert.CertificateException;
import java.util.Base64;

import javax.crypto.Cipher;
import javax.crypto.NoSuchPaddingException;

public class EncryptWithPublicKey
{
    // specify mode and padding instead of relying on defaults (use OAEP if available!)
    private static Cipher encrypt = null;

    private static PublicKey getPublicKeyFromKeystore(String keystorefile,String keystorepw,String alias,String aliaspw){
    {
        Certificate cert = null;
        PublicKey publicKey = null;
        try
        {
            encrypt = Cipher.getInstance("RSA/ECB/PKCS1Padding");
            FileInputStream is = new FileInputStream("coretest.jks");

            KeyStore keystore = KeyStore.getInstance(KeyStore.getDefaultType());
            keystore.load(is, keystorepw.toCharArray());

            // Get certificate of public key
            cert = keystore.getCertificate(alias);
            // Get public key
            publicKey = cert.getPublicKey();
        }
        catch (NoSuchAlgorithmException | IOException | CertificateException | KeyStoreException | NoSuchPaddingException e)
        {
            e.printStackTrace();
        }
        return publicKey;
    }

    public static void main(String[] args) throws Exception
    {
        PublicKey publicKey = getPublicKeyFromKeystore("KEYSTORE.jks","KeyStorePWD","KeyNAME","KeyPWD");

        // encrypt with known character encoding, you should probably use hybrid cryptography instead
        String msg = "Almafa123";
        String encryptedMsg = encrypt(msg, publicKey);
        System.out.println(encryptedMsg);

    }

    private static String encrypt(String plainText, PublicKey secretKey)
            throws Exception
    {
        byte[] plainTextByte = plainText.getBytes();
        encrypt.init(Cipher.ENCRYPT_MODE, secretKey);
        byte[] encryptedByte = encrypt.doFinal(plainTextByte);
        Base64.Encoder encoder = Base64.getEncoder();
        return encoder.encodeToString(encryptedByte);
    }
}

import java.io.FileInputStream;
import java.io.IOException;
import java.security.*;
import java.security.cert.CertificateException;
import java.util.Base64;

import javax.crypto.Cipher;
import javax.crypto.NoSuchPaddingException;

public class DencryptWithPrivateKey
{
    // specify mode and padding instead of relying on defaults (use OAEP if available!)
    private static Cipher encrypt = null;

    private static Key getPrivateKeyFromKeystore(String keystorefile,String keystorepw,String alias,String aliaspw){
        Key privateKey = null;
        try
        {
            encrypt = Cipher.getInstance("RSA/ECB/PKCS1Padding");
            FileInputStream is = new FileInputStream(keystorefile);

            KeyStore keystore = KeyStore.getInstance(KeyStore.getDefaultType());
            keystore.load(is, keystorepw.toCharArray());

            privateKey = keystore.getKey(alias, aliaspw.toCharArray());

        }
        catch (NoSuchAlgorithmException | IOException | CertificateException | UnrecoverableKeyException | KeyStoreException | NoSuchPaddingException e)
        {
            e.printStackTrace();
        }
        return privateKey;
    }

    public static void main(String[] args) throws Exception
    {
        Key privateKey=getPrivateKeyFromKeystore("");
        String encrytedMsg="NOFbqvspgLdCR8jYYBHXXUWZSPffYL6uTvm61V1oou4u8LX4CxLzcTCtzcUpQRNKTxHJUEMyC4l+YW0EGJAU5GKifeLCZR6tT0K2Ms4ojOOMlnEodiW/3P4zU68ULjomnf+2N0f7Ss6d5ZYsKyMgQL9nAVgkWuSzeUTZnxDwc2E3zBUs93WB99TRokYBY1PdOAIxd4McqFxrKFv3VHEvYaf4aiYl1SPfC/lKgbS1KEbMN83XxocV5VFe0HJS0jtwZpvlc9eyUL90r225wp/EplFJHxlk8/mN1PERV550+22mbgmE/lM71I0kB2mW5zq66DMOzlZykCT4uJ1gf5sP7w==";
        System.out.println(deN_crypt(encrytedMsg,privateKey));

    }
    private static String dencrypt(String encryptedText, Key secretKey)
            throws Exception {
        Base64.Decoder decoder = Base64.getDecoder();
        byte[] encryptedTextByte = decoder.decode(encryptedText);
        encrypt.init(Cipher.DEN_CRYPT_MODE, secretKey);
        byte[] den_cryptedByte = dencrypt.doFinal(encryptedTextByte);
        String den_cryptedText = new String(den_cryptedByte);
        return dencryptedText;
    }

}

Vélemény, hozzászólás?

Az e-mail címet nem tesszük közzé. A kötelező mezőket * karakterrel jelöltük