package com.hasar.util;
import java.io.FileInputStream;
import
java.io.IOException;
import java.security.KeyStore;
import
java.security.NoSuchAlgorithmException;
import
java.security.PrivateKey;
import java.security.PublicKey;
import
java.security.Security;
import java.security.cert.X509Certificate;
import
java.util.Enumeration;
import javax.crypto.Cipher;
import
javax.crypto.NoSuchPaddingException;
import
org.bouncycastle.jce.provider.BouncyCastleProvider;
import
com.hasar.distribuidor.TareaPrincipal;
import sun.misc.BASE64Decoder;
import
sun.misc.BASE64Encoder;
public class RSAEncryptUtil {
protected static final String ALGORITHM = "RSA";
protected
static final String ENCODING = "UTF8";
protected static
final String PADDING = "RSA/ECB/PKCS1Padding";
protected
static final String CERT_FILENAME = "ARCHIVO.PFX";
protected static final String PKCS = "PKCS12";
protected
static final String PASSWORD = "Clave de acceso al pfx";
public static PrivateKey _privateKey;
public static PublicKey
_publicKey;
public static X509Certificate
_certificate;
public static Cipher
cipher;
private RSAEncryptUtil()
{
}
public static void init()
{
try
{
Security.addProvider(new
BouncyCastleProvider());
TareaPrincipal._debug.print("EncrypUtil","Provider en memoria\nObteniendo la
instancia PKCS1Padding - Puede tardar
..");
cipher =
Cipher.getInstance(PADDING);
}
catch (NoSuchAlgorithmException e)
{
e.printStackTrace();
} catch
(NoSuchPaddingException e)
{
e.printStackTrace();
}
}
public static byte[]
encrypt(byte[] text, PublicKey key) throws Exception
{
byte[] cipherText =
null;
try
{
cipher.init(Cipher.ENCRYPT_MODE,
key);
cipherText = cipher.doFinal(text);
} catch (Exception e)
{
throw
e;
}
return
cipherText;
}
public static String
encrypt(String text, PublicKey key) throws Exception
{
String
encryptedText;
try
{
byte[]
cipherText = encrypt(text.getBytes(ENCODING),
key);
encryptedText =
encodeBASE64(cipherText);
} catch
(Exception e)
{
throw
e;
}
return
encryptedText;
}
private static String
encodeBASE64(byte[] bytes) {
BASE64Encoder b64 = new
BASE64Encoder();
return
b64.encode(bytes);
}
public static void
cargoCertificadoX509() throws Exception
{
KeyStore keyStore =
KeyStore.getInstance(PKCS);
FileInputStream inputStream = new
FileInputStream(CERT_FILENAME);
try {
char[]
passwordChars = new
char[PASSWORD.length()];
PASSWORD.getChars(0, PASSWORD.length(), passwordChars,
0);
TareaPrincipal._debug.print("EncrypUtil","Espere Por
Favor...");
keyStore.load(inputStream,
passwordChars);
Enumeration enumeration =
keyStore.aliases();
String alias = (String)
enumeration.nextElement();
PrivateKey privKey = (PrivateKey)
keyStore.getKey(alias,
passwordChars);
X509Certificate cert = (X509Certificate)
keyStore
getCertificate(alias);
_privateKey =
privKey;
_certificate =
cert;
_publicKey = cert.getPublicKey();
} finally {
inputStream.close();
}
}
public
static byte[] decrypt(byte[] text, PrivateKey key) throws
Exception
{
byte[] dectyptedText = null;
try
{
cipher.init(Cipher.DECRYPT_MODE,
key);
dectyptedText =
cipher.doFinal(text);
}
catch (Exception
e)
{
throw
e;
}
return
dectyptedText;
}
public static String
decrypt(String text, PrivateKey key) throws Exception
{
String
result;
try
{
byte[]
dectyptedText =
decrypt(decodeBASE64(text),key);
result = new String(dectyptedText,
ENCODING);
}
catch (Exception
e)
{
throw
e;
}
return
result;
}
private static byte[] decodeBASE64(String text) throws IOException
{
BASE64Decoder b64 = new
BASE64Decoder();
return
b64.decodeBuffer(text);
}
}