001: /*
002: * SRADecoder.java
003: *
004: * Created on January 27, 2003, 12:50 PM
005: */
006:
007: /**
008: *
009: * @author ss133690
010: * @version
011: */package com.sun.portal.cli.cert;
012:
013: import java.security.*;
014: import com.sun.portal.log.common.PortalLogger;
015: import javax.crypto.spec.*;
016: import javax.crypto.*;
017:
018: public abstract class SRADecoder {
019: protected Cipher cipher;
020: protected MessageDigest digest;
021: protected PBEParameterSpec paramSpec;
022: protected SecretKey secretKey;
023: protected SRADecoderContext decoderCntx;
024:
025: public abstract void init() throws SRADecoderException;
026:
027: protected abstract SRADecoderContext getDecoderContext();
028:
029: public String encrypt(String data) throws SRADecoderException {
030: try {
031: return getEncodedStr(encrypt(data
032: .getBytes(CertAdminConstants.CHAR_ENC)));
033: } catch (Exception ex) {
034: return getEncodedStr(encrypt(data.getBytes()));
035: }
036: }
037:
038: public String decrypt(String data) throws SRADecoderException {
039: try {
040: return new String(decrypt(decode(data
041: .getBytes(CertAdminConstants.CHAR_ENC))),
042: CertAdminConstants.CHAR_ENC);
043: } catch (Exception ex) {
044: return new String(decrypt(decode(data.getBytes())));
045: }
046: }
047:
048: public byte[] encrypt(byte[] data) throws SRADecoderException {
049: try {
050: cipher.init(Cipher.ENCRYPT_MODE, secretKey, paramSpec);
051: return cipher.doFinal(data);
052: } catch (Exception ex) {
053: throw new SRADecoderException(ex.toString());
054: }
055: }
056:
057: public byte[] decrypt(byte[] data) throws SRADecoderException {
058: try {
059: cipher.init(Cipher.DECRYPT_MODE, secretKey, paramSpec);
060: return cipher.doFinal(data);
061: } catch (Exception ex) {
062: throw new SRADecoderException(ex.toString());
063: }
064: }
065:
066: public String digest(String data) throws SRADecoderException {
067: try {
068: return getEncodedStr(digest(data
069: .getBytes(CertAdminConstants.CHAR_ENC)));
070: } catch (Exception ex) {
071: return getEncodedStr(digest(data.getBytes()));
072: }
073: }
074:
075: public byte[] digest(byte[] data) throws SRADecoderException {
076: try {
077: return digest.digest(data);
078: } catch (Exception ex) {
079: throw new SRADecoderException(ex.toString());
080: }
081: }
082:
083: //Default is base64 encoding
084: protected byte[] encode(byte[] data) {
085: return Base64.encode(data);
086: }
087:
088: protected byte[] decode(byte[] data) {
089: return Base64.decode(data);
090: }
091:
092: public String getEncodedStr(byte[] data) {
093: try {
094: return new String(encode(data), CertAdminConstants.CHAR_ENC);
095: } catch (Exception ex) {
096: return new String(encode(data));
097: }
098: }
099:
100: public String getDecodeStr(byte[] data) {
101: try {
102: return new String(decode(data), CertAdminConstants.CHAR_ENC);
103: } catch (Exception ex) {
104: return new String(decode(data));
105: }
106: }
107:
108: }
|