Package my.comcrazy.rsa

Source Code of my.comcrazy.rsa.rsautils

/*******************************************************************************
* Copyright (C) 2010  James ong
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.

* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
* MA  02110-1301, USA.
*
* Please contact James ong <comcrazy@gmail.com> if you need additional
* information or have any questions.
*******************************************************************************/
/*
* Author: James Ong <comcrazy@gmail.com>
*/

package my.comcrazy.rsa;

import java.io.BufferedReader;
import java.io.IOException;
import java.security.KeyFactory;
import java.security.KeyPair;
import java.security.NoSuchAlgorithmException;
import java.security.PrivateKey;
import java.security.Security;
import java.security.spec.InvalidKeySpecException;
import java.security.spec.PKCS8EncodedKeySpec;
import javax.crypto.Cipher;
import org.bouncycastle.openssl.PEMReader;
import org.codehaus.xfire.util.Base64;

public class rsautils {
   
    public static PrivateKey loadRSAPrivateKey (BufferedReader br)
        throws NoSuchAlgorithmException, InvalidKeySpecException, IOException{
       
        Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());

        String line;
       
        StringBuffer keyBuf = new StringBuffer();
        boolean pkcs8 = false;
       
        /* Made a mark */
        br.mark(4096);
        while ((line = br.readLine()) != null)
        {
          if (line.startsWith("-----BEGIN PRIVATE KEY")) {
            pkcs8 = true;
          }

          if (line.startsWith("-----END")) {
            continue;
          }
          keyBuf.append(line);
        }       

        if (pkcs8) {
          PKCS8EncodedKeySpec PKCS8KeySpec =
              new PKCS8EncodedKeySpec(Base64.decode(keyBuf.toString()));
         
          KeyFactory keyFactory = KeyFactory.getInstance("RSA");
          return keyFactory.generatePrivate(PKCS8KeySpec);
        } else {           
            br.reset();
            PEMReader pemReader = new PEMReader(br);
            KeyPair keypair = (KeyPair)pemReader.readObject();

            return ((keypair == null) ? null : keypair.getPrivate());      
        }
    }

    public static String decodeString(String encPasswd, PrivateKey privateKey)
        throws Exception {
       
        byte[] decodeText = Base64.decode(encPasswd);
       
        Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");
        cipher.init(2, privateKey);
       
       
        byte[] planTXTPasswd = cipher.doFinal(decodeText);
        return new String(planTXTPasswd);
    }   
}
TOP

Related Classes of my.comcrazy.rsa.rsautils

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.