001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * Portions Copyrighted 2007 Sun Microsystems, Inc.
027: */
028: package org.netbeans.lib.uihandler;
029:
030: import java.io.IOException;
031: import java.io.InputStream;
032: import java.security.GeneralSecurityException;
033: import java.security.KeyFactory;
034: import java.security.PrivateKey;
035: import java.security.PublicKey;
036: import java.security.spec.X509EncodedKeySpec;
037: import javax.crypto.Cipher;
038:
039: /**
040: *
041: * @author Jindrich Sedek
042: */
043: public class PasswdEncryption {
044:
045: private static final String delimiter = ":"; //NOI18N
046:
047: public static String encrypt(String text) throws IOException,
048: GeneralSecurityException {
049: return encrypt(text, getPublicKey());
050: }
051:
052: public static byte[] encrypt(byte[] text) throws IOException,
053: GeneralSecurityException {
054: return encrypt(text, getPublicKey());
055: }
056:
057: public static byte[] encrypt(byte[] text, PublicKey key)
058: throws IOException, GeneralSecurityException {
059: Cipher rsaCipher = Cipher.getInstance("RSA/ECB/PKCS1Padding"); // NOI18N
060: rsaCipher.init(Cipher.ENCRYPT_MODE, key);
061: byte[] encoded = null;
062: encoded = rsaCipher.doFinal(text);
063: return encoded;
064: }
065:
066: public static byte[] decrypt(byte[] text, PrivateKey key)
067: throws IOException, GeneralSecurityException {
068: Cipher rsaCipher = Cipher.getInstance("RSA/ECB/PKCS1Padding"); // NOI18N
069: rsaCipher.init(Cipher.DECRYPT_MODE, key);
070: byte[] decoded = null;
071: decoded = rsaCipher.doFinal(text);
072: return decoded;
073: }
074:
075: public static String encrypt(String text, PublicKey key)
076: throws IOException, GeneralSecurityException {
077: byte[] encrypted = encrypt(text.getBytes(), key);
078: return arrayToString(encrypted);
079: }
080:
081: public static String decrypt(String text, PrivateKey key)
082: throws IOException, GeneralSecurityException {
083: byte[] decrypted = decrypt(stringToArray(text), key);
084: return new String(decrypted);
085: }
086:
087: private static String arrayToString(byte[] array) {
088: String result = "";
089: for (int i = 0; i < array.length; i++) {
090: byte b = array[i];
091: result = result.concat(Byte.toString(b) + delimiter);
092: }
093: return result;
094: }
095:
096: private static byte[] stringToArray(String str) {
097: String[] numbers = str.split(delimiter);
098: byte[] result = new byte[numbers.length];
099: for (int i = 0; i < numbers.length; i++) {
100: result[i] = Byte.parseByte(numbers[i]);
101: }
102: return result;
103: }
104:
105: private static PublicKey getPublicKey() throws IOException,
106: GeneralSecurityException {
107: InputStream inputStr = PasswdEncryption.class
108: .getResourceAsStream("pubKey"); // NOI18N
109: byte[] encodedKey = new byte[inputStr.available()];
110: inputStr.read(encodedKey);
111: X509EncodedKeySpec publicKeySpec = new X509EncodedKeySpec(
112: encodedKey);
113: KeyFactory kf = KeyFactory.getInstance("RSA"); // NOI18N
114: PublicKey publicKey = kf.generatePublic(publicKeySpec);
115: return publicKey;
116: }
117: }
|