PageRenderTime 53ms CodeModel.GetById 43ms app.highlight 6ms RepoModel.GetById 2ms app.codeStats 0ms

/v3.2/nimbits-sdk/src/com/nimbits/security/Encryptor.java

http://nimbits-server.googlecode.com/
Java | 67 lines | 42 code | 7 blank | 18 comment | 3 complexity | 5c16ce25b28579ae6c748369990cfffe MD5 | raw file
 1/*
 2 * Copyright (c) 2010 Tonic Solutions LLC.
 3 *
 4 * http://www.nimbits.com
 5 *
 6 *
 7 * Licensed under the GNU GENERAL PUBLIC LICENSE, Version 3.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at
 8 *
 9 * http://www.gnu.org/licenses/gpl.html
10 *
11 * 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.
12 */
13
14package com.nimbits.security;
15
16import sun.misc.*;
17
18import javax.crypto.*;
19import java.io.*;
20import java.security.*;
21
22/**
23 * Created by bsautner
24 * User: benjamin
25 * Date: 8/11/11
26 * Time: 11:39 AM
27 */
28public class Encryptor {
29    public static String encode(String str) {
30        BASE64Encoder encoder = new BASE64Encoder();
31        str = encoder.encodeBuffer(str.getBytes());
32        return str;
33    }
34
35    public static String decode(String str) {
36        BASE64Decoder decoder = new BASE64Decoder();
37        try {
38            str = new String(decoder.decodeBuffer(str));
39        } catch (IOException e) {
40            e.printStackTrace();
41        }
42        return str;
43    }
44    public static void writeEncryptedFile(final String fileName, final String unencryptedString) throws IOException {
45
46        final String e = encode(unencryptedString);
47        final Writer out = new OutputStreamWriter(new FileOutputStream(fileName));
48        out.write(e);
49        out.close();
50    }
51    public static String readEncryptedFile(final String fileName) throws IOException {
52
53        String retStr = null;
54        final File file = new File(fileName);
55        if (file.exists()) {
56            final StringBuilder sb = new StringBuilder();
57            final BufferedReader in = new BufferedReader(new FileReader(fileName));
58            String str;
59            while ((str = in.readLine()) != null) {
60                sb.append(str);
61            }
62            in.close();
63            retStr = decode(sb.toString());
64        }
65        return retStr;
66    }
67}