]> git.argeo.org Git - lgpl/argeo-commons.git/blob - PasswordBasedEncryptionTest.java
c5600884ffd7c2431677d2465e7f9a7e02c09151
[lgpl/argeo-commons.git] / PasswordBasedEncryptionTest.java
1 /*
2 * Copyright (C) 2007-2012 Mathieu Baudier
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16 package org.argeo.util.crypto;
17
18 import java.io.ByteArrayInputStream;
19 import java.io.ByteArrayOutputStream;
20 import java.io.InputStream;
21 import java.security.AlgorithmParameters;
22
23 import javax.crypto.Cipher;
24 import javax.crypto.CipherInputStream;
25 import javax.crypto.CipherOutputStream;
26 import javax.crypto.SecretKey;
27 import javax.crypto.SecretKeyFactory;
28 import javax.crypto.spec.IvParameterSpec;
29 import javax.crypto.spec.PBEKeySpec;
30 import javax.crypto.spec.PBEParameterSpec;
31 import javax.crypto.spec.SecretKeySpec;
32
33 import junit.framework.TestCase;
34
35 import org.argeo.StreamUtils;
36 import org.argeo.util.crypto.PasswordBasedEncryption;
37
38 public class PasswordBasedEncryptionTest{// extends TestCase {
39 // public void testEncryptDecrypt() {
40 // final String password = "test long password since they are more powerful";
41 // PasswordBasedEncryption pbeEnc = new PasswordBasedEncryption(
42 // password.toCharArray());
43 // String message = "Hello World!";
44 // byte[] encrypted = pbeEnc.encryptString(message);
45 // // System.out.println("Encrypted: '" + new String(encrypted) + "'");
46 // PasswordBasedEncryption pbeDec = new PasswordBasedEncryption(
47 // password.toCharArray());
48 // InputStream in = null;
49 // in = new ByteArrayInputStream(encrypted);
50 // String decrypted = pbeDec.decryptAsString(in);
51 // // System.out.println("Decrypted: '" + decrypted + "'");
52 // StreamUtils.closeQuietly(in);
53 // assertEquals(message, decrypted);
54 // }
55 //
56 // public void testPBEWithMD5AndDES() throws Exception {
57 // String password = "test";
58 // String message = "Hello World!";
59 //
60 // byte[] salt = { (byte) 0xc7, (byte) 0x73, (byte) 0x21, (byte) 0x8c,
61 // (byte) 0x7e, (byte) 0xc8, (byte) 0xee, (byte) 0x99 };
62 //
63 // int count = 1024;
64 //
65 // String cipherAlgorithm = "PBEWithMD5AndDES";
66 // String secretKeyAlgorithm = "PBEWithMD5AndDES";
67 // SecretKeyFactory keyFac = SecretKeyFactory
68 // .getInstance(secretKeyAlgorithm);
69 // PBEKeySpec pbeKeySpec = new PBEKeySpec(password.toCharArray());
70 // PBEParameterSpec pbeParamSpec = new PBEParameterSpec(salt, count);
71 // SecretKey pbeKey = keyFac.generateSecret(pbeKeySpec);
72 // Cipher ecipher = Cipher.getInstance(cipherAlgorithm);
73 // ecipher.init(Cipher.ENCRYPT_MODE, pbeKey, pbeParamSpec);
74 // Cipher dcipher = Cipher.getInstance(cipherAlgorithm);
75 // dcipher.init(Cipher.DECRYPT_MODE, pbeKey, pbeParamSpec);
76 //
77 // byte[] encrypted = ecipher.doFinal(message.getBytes());
78 // byte[] decrypted = dcipher.doFinal(encrypted);
79 // assertEquals(message, new String(decrypted));
80 //
81 // }
82 //
83 // public void testPBEWithSHA1AndAES() throws Exception {
84 // String password = "test";
85 // String message = "Hello World!";
86 //
87 // byte[] salt = { (byte) 0xc7, (byte) 0x73, (byte) 0x21, (byte) 0x8c,
88 // (byte) 0x7e, (byte) 0xc8, (byte) 0xee, (byte) 0x99 };
89 // byte[] iv = { (byte) 0xc7, (byte) 0x73, (byte) 0x21, (byte) 0x8c,
90 // (byte) 0x7e, (byte) 0xc8, (byte) 0xee, (byte) 0x99,
91 // (byte) 0xc7, (byte) 0x73, (byte) 0x21, (byte) 0x8c,
92 // (byte) 0x7e, (byte) 0xc8, (byte) 0xee, (byte) 0x99 };
93 //
94 // int count = 1024;
95 // // int keyLength = 256;
96 // int keyLength = 128;
97 //
98 // String cipherAlgorithm = "AES/CBC/PKCS5Padding";
99 // String secretKeyAlgorithm = "PBKDF2WithHmacSHA1";
100 // SecretKeyFactory keyFac = SecretKeyFactory
101 // .getInstance(secretKeyAlgorithm);
102 // PBEKeySpec pbeKeySpec = new PBEKeySpec(password.toCharArray(), salt,
103 // count, keyLength);
104 // SecretKey tmp = keyFac.generateSecret(pbeKeySpec);
105 // SecretKey secret = new SecretKeySpec(tmp.getEncoded(), "AES");
106 // Cipher ecipher = Cipher.getInstance(cipherAlgorithm);
107 // ecipher.init(Cipher.ENCRYPT_MODE, secret, new IvParameterSpec(iv));
108 //
109 // // decrypt
110 // keyFac = SecretKeyFactory.getInstance(secretKeyAlgorithm);
111 // pbeKeySpec = new PBEKeySpec(password.toCharArray(), salt, count,
112 // keyLength);
113 // tmp = keyFac.generateSecret(pbeKeySpec);
114 // secret = new SecretKeySpec(tmp.getEncoded(), "AES");
115 // // AlgorithmParameters params = ecipher.getParameters();
116 // // byte[] iv = params.getParameterSpec(IvParameterSpec.class).getIV();
117 // Cipher dcipher = Cipher.getInstance(cipherAlgorithm);
118 // dcipher.init(Cipher.DECRYPT_MODE, secret, new IvParameterSpec(iv));
119 //
120 // byte[] encrypted = ecipher.doFinal(message.getBytes());
121 // byte[] decrypted = dcipher.doFinal(encrypted);
122 // assertEquals(message, new String(decrypted));
123 //
124 // ByteArrayOutputStream out = new ByteArrayOutputStream();
125 // CipherOutputStream cipherOut = new CipherOutputStream(out, ecipher);
126 // cipherOut.write(message.getBytes());
127 // StreamUtils.closeQuietly(cipherOut);
128 // byte[] enc = out.toByteArray();
129 //
130 // ByteArrayInputStream in = new ByteArrayInputStream(enc);
131 // CipherInputStream cipherIn = new CipherInputStream(in, dcipher);
132 // ByteArrayOutputStream dec = new ByteArrayOutputStream();
133 // StreamUtils.copy(cipherIn, dec);
134 // assertEquals(message, new String(dec.toByteArray()));
135 // }
136 }