RSA encrypt plain text with charset - Android java.security

Android examples for java.security:RSA

Description

RSA encrypt plain text with charset

Demo Code

/*//from   w  ww .  j a v  a2s.  c o m
 * Copyright (C) 2014 Wei Chou (weichou2010@gmail.com)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
//package com.java2s;
import android.util.Base64;

import java.io.UnsupportedEncodingException;
import java.security.InvalidKeyException;
import java.security.KeyFactory;

import java.security.NoSuchAlgorithmException;
import java.security.PrivateKey;
import java.security.PublicKey;

import java.security.spec.InvalidKeySpecException;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;
import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;

public class Main {
    private static final String RSA_ARITHEMTIC_NAME = "RSA";
    private static final String RSA_TRANSFORMATION = "RSA/ECB/PKCS1Padding";

    public static String encrypt(boolean isByPublic, String base64Key,
            String plaintext, String charset)
            throws NoSuchAlgorithmException, NoSuchPaddingException,
            InvalidKeyException, InvalidKeySpecException,
            IllegalBlockSizeException, BadPaddingException,
            UnsupportedEncodingException {
        Cipher cipher = Cipher.getInstance(RSA_TRANSFORMATION);
        cipher.init(Cipher.ENCRYPT_MODE,
                isByPublic ? getPublicKey(RSA_ARITHEMTIC_NAME, base64Key)
                        : getPrivateKey(RSA_ARITHEMTIC_NAME, base64Key));
        byte[] output = cipher.doFinal(plaintext.getBytes(charset));
        return Base64.encodeToString(output, Base64.NO_WRAP); 
    }

    private static PublicKey getPublicKey(String arithmetic,
            String base64EncodedKey) throws NoSuchAlgorithmException,
            InvalidKeySpecException {
        byte[] keyBytes = Base64.decode(base64EncodedKey, Base64.DEFAULT);
        X509EncodedKeySpec x509 = new X509EncodedKeySpec(keyBytes);
        KeyFactory keyFactory = KeyFactory.getInstance(arithmetic);
        return keyFactory.generatePublic(x509);
    }

    private static PrivateKey getPrivateKey(String arithmetic,
            String base64EncodedKey) throws NoSuchAlgorithmException,
            InvalidKeySpecException {
        byte[] keyBytes = Base64.decode(base64EncodedKey, Base64.DEFAULT);
        PKCS8EncodedKeySpec pkcs8 = new PKCS8EncodedKeySpec(keyBytes);
        KeyFactory keyFactory = KeyFactory.getInstance(arithmetic);
        return keyFactory.generatePrivate(pkcs8);
    }
}

Related Tutorials