1 package org
.argeo
.util
.crypto
;
3 import java
.io
.ByteArrayInputStream
;
4 import java
.io
.ByteArrayOutputStream
;
5 import java
.io
.InputStream
;
6 import java
.security
.AlgorithmParameters
;
8 import javax
.crypto
.Cipher
;
9 import javax
.crypto
.CipherInputStream
;
10 import javax
.crypto
.CipherOutputStream
;
11 import javax
.crypto
.SecretKey
;
12 import javax
.crypto
.SecretKeyFactory
;
13 import javax
.crypto
.spec
.IvParameterSpec
;
14 import javax
.crypto
.spec
.PBEKeySpec
;
15 import javax
.crypto
.spec
.PBEParameterSpec
;
16 import javax
.crypto
.spec
.SecretKeySpec
;
18 import junit
.framework
.TestCase
;
20 import org
.argeo
.StreamUtils
;
21 import org
.argeo
.util
.crypto
.PasswordBasedEncryption
;
23 public class PasswordBasedEncryptionTest
{// extends TestCase {
24 // public void testEncryptDecrypt() {
25 // final String password = "test long password since they are more powerful";
26 // PasswordBasedEncryption pbeEnc = new PasswordBasedEncryption(
27 // password.toCharArray());
28 // String message = "Hello World!";
29 // byte[] encrypted = pbeEnc.encryptString(message);
30 // // System.out.println("Encrypted: '" + new String(encrypted) + "'");
31 // PasswordBasedEncryption pbeDec = new PasswordBasedEncryption(
32 // password.toCharArray());
33 // InputStream in = null;
34 // in = new ByteArrayInputStream(encrypted);
35 // String decrypted = pbeDec.decryptAsString(in);
36 // // System.out.println("Decrypted: '" + decrypted + "'");
37 // StreamUtils.closeQuietly(in);
38 // assertEquals(message, decrypted);
41 // public void testPBEWithMD5AndDES() throws Exception {
42 // String password = "test";
43 // String message = "Hello World!";
45 // byte[] salt = { (byte) 0xc7, (byte) 0x73, (byte) 0x21, (byte) 0x8c,
46 // (byte) 0x7e, (byte) 0xc8, (byte) 0xee, (byte) 0x99 };
50 // String cipherAlgorithm = "PBEWithMD5AndDES";
51 // String secretKeyAlgorithm = "PBEWithMD5AndDES";
52 // SecretKeyFactory keyFac = SecretKeyFactory
53 // .getInstance(secretKeyAlgorithm);
54 // PBEKeySpec pbeKeySpec = new PBEKeySpec(password.toCharArray());
55 // PBEParameterSpec pbeParamSpec = new PBEParameterSpec(salt, count);
56 // SecretKey pbeKey = keyFac.generateSecret(pbeKeySpec);
57 // Cipher ecipher = Cipher.getInstance(cipherAlgorithm);
58 // ecipher.init(Cipher.ENCRYPT_MODE, pbeKey, pbeParamSpec);
59 // Cipher dcipher = Cipher.getInstance(cipherAlgorithm);
60 // dcipher.init(Cipher.DECRYPT_MODE, pbeKey, pbeParamSpec);
62 // byte[] encrypted = ecipher.doFinal(message.getBytes());
63 // byte[] decrypted = dcipher.doFinal(encrypted);
64 // assertEquals(message, new String(decrypted));
68 // public void testPBEWithSHA1AndAES() throws Exception {
69 // String password = "test";
70 // String message = "Hello World!";
72 // byte[] salt = { (byte) 0xc7, (byte) 0x73, (byte) 0x21, (byte) 0x8c,
73 // (byte) 0x7e, (byte) 0xc8, (byte) 0xee, (byte) 0x99 };
74 // byte[] iv = { (byte) 0xc7, (byte) 0x73, (byte) 0x21, (byte) 0x8c,
75 // (byte) 0x7e, (byte) 0xc8, (byte) 0xee, (byte) 0x99,
76 // (byte) 0xc7, (byte) 0x73, (byte) 0x21, (byte) 0x8c,
77 // (byte) 0x7e, (byte) 0xc8, (byte) 0xee, (byte) 0x99 };
80 // // int keyLength = 256;
81 // int keyLength = 128;
83 // String cipherAlgorithm = "AES/CBC/PKCS5Padding";
84 // String secretKeyAlgorithm = "PBKDF2WithHmacSHA1";
85 // SecretKeyFactory keyFac = SecretKeyFactory
86 // .getInstance(secretKeyAlgorithm);
87 // PBEKeySpec pbeKeySpec = new PBEKeySpec(password.toCharArray(), salt,
89 // SecretKey tmp = keyFac.generateSecret(pbeKeySpec);
90 // SecretKey secret = new SecretKeySpec(tmp.getEncoded(), "AES");
91 // Cipher ecipher = Cipher.getInstance(cipherAlgorithm);
92 // ecipher.init(Cipher.ENCRYPT_MODE, secret, new IvParameterSpec(iv));
95 // keyFac = SecretKeyFactory.getInstance(secretKeyAlgorithm);
96 // pbeKeySpec = new PBEKeySpec(password.toCharArray(), salt, count,
98 // tmp = keyFac.generateSecret(pbeKeySpec);
99 // secret = new SecretKeySpec(tmp.getEncoded(), "AES");
100 // // AlgorithmParameters params = ecipher.getParameters();
101 // // byte[] iv = params.getParameterSpec(IvParameterSpec.class).getIV();
102 // Cipher dcipher = Cipher.getInstance(cipherAlgorithm);
103 // dcipher.init(Cipher.DECRYPT_MODE, secret, new IvParameterSpec(iv));
105 // byte[] encrypted = ecipher.doFinal(message.getBytes());
106 // byte[] decrypted = dcipher.doFinal(encrypted);
107 // assertEquals(message, new String(decrypted));
109 // ByteArrayOutputStream out = new ByteArrayOutputStream();
110 // CipherOutputStream cipherOut = new CipherOutputStream(out, ecipher);
111 // cipherOut.write(message.getBytes());
112 // StreamUtils.closeQuietly(cipherOut);
113 // byte[] enc = out.toByteArray();
115 // ByteArrayInputStream in = new ByteArrayInputStream(enc);
116 // CipherInputStream cipherIn = new CipherInputStream(in, dcipher);
117 // ByteArrayOutputStream dec = new ByteArrayOutputStream();
118 // StreamUtils.copy(cipherIn, dec);
119 // assertEquals(message, new String(dec.toByteArray()));