X-Git-Url: https://git.argeo.org/?a=blobdiff_plain;f=basic%2Fruntime%2Forg.argeo.basic.nodeps%2Fsrc%2Ftest%2Fjava%2Forg%2Fargeo%2Futil%2Fcrypto%2FPasswordBasedEncryptionTest.java;fp=basic%2Fruntime%2Forg.argeo.basic.nodeps%2Fsrc%2Ftest%2Fjava%2Forg%2Fargeo%2Futil%2Fcrypto%2FPasswordBasedEncryptionTest.java;h=c454f49716776b921ffd413998db2691963d1d58;hb=42b134a6578ec09c77784dbc135d6a045cf67962;hp=95d2db2305ea2c7dff4fc9a29f27618cc605b849;hpb=7b3b7377bc70bef7da9b73b544e8459b65c6091d;p=lgpl%2Fargeo-commons.git diff --git a/basic/runtime/org.argeo.basic.nodeps/src/test/java/org/argeo/util/crypto/PasswordBasedEncryptionTest.java b/basic/runtime/org.argeo.basic.nodeps/src/test/java/org/argeo/util/crypto/PasswordBasedEncryptionTest.java index 95d2db230..c454f4971 100644 --- a/basic/runtime/org.argeo.basic.nodeps/src/test/java/org/argeo/util/crypto/PasswordBasedEncryptionTest.java +++ b/basic/runtime/org.argeo.basic.nodeps/src/test/java/org/argeo/util/crypto/PasswordBasedEncryptionTest.java @@ -21,101 +21,101 @@ import org.argeo.StreamUtils; import org.argeo.util.crypto.PasswordBasedEncryption; public class PasswordBasedEncryptionTest extends TestCase { - public void testEncryptDecrypt() { - final String password = "test long password since they are more powerful"; - PasswordBasedEncryption pbeEnc = new PasswordBasedEncryption( - password.toCharArray()); - String message = "Hello World!"; - byte[] encrypted = pbeEnc.encryptString(message); - // System.out.println("Encrypted: '" + new String(encrypted) + "'"); - PasswordBasedEncryption pbeDec = new PasswordBasedEncryption( - password.toCharArray()); - InputStream in = null; - in = new ByteArrayInputStream(encrypted); - String decrypted = pbeDec.decryptAsString(in); - // System.out.println("Decrypted: '" + decrypted + "'"); - StreamUtils.closeQuietly(in); - assertEquals(message, decrypted); - } - - public void testPBEWithMD5AndDES() throws Exception { - String password = "test"; - String message = "Hello World!"; - - byte[] salt = { (byte) 0xc7, (byte) 0x73, (byte) 0x21, (byte) 0x8c, - (byte) 0x7e, (byte) 0xc8, (byte) 0xee, (byte) 0x99 }; - - int count = 1024; - - String cipherAlgorithm = "PBEWithMD5AndDES"; - String secretKeyAlgorithm = "PBEWithMD5AndDES"; - SecretKeyFactory keyFac = SecretKeyFactory - .getInstance(secretKeyAlgorithm); - PBEKeySpec pbeKeySpec = new PBEKeySpec(password.toCharArray()); - PBEParameterSpec pbeParamSpec = new PBEParameterSpec(salt, count); - SecretKey pbeKey = keyFac.generateSecret(pbeKeySpec); - Cipher ecipher = Cipher.getInstance(cipherAlgorithm); - ecipher.init(Cipher.ENCRYPT_MODE, pbeKey, pbeParamSpec); - Cipher dcipher = Cipher.getInstance(cipherAlgorithm); - dcipher.init(Cipher.DECRYPT_MODE, pbeKey, pbeParamSpec); - - byte[] encrypted = ecipher.doFinal(message.getBytes()); - byte[] decrypted = dcipher.doFinal(encrypted); - assertEquals(message, new String(decrypted)); - - } - - public void testPBEWithSHA1AndAES() throws Exception { - String password = "test"; - String message = "Hello World!"; - - byte[] salt = { (byte) 0xc7, (byte) 0x73, (byte) 0x21, (byte) 0x8c, - (byte) 0x7e, (byte) 0xc8, (byte) 0xee, (byte) 0x99 }; - byte[] iv = { (byte) 0xc7, (byte) 0x73, (byte) 0x21, (byte) 0x8c, - (byte) 0x7e, (byte) 0xc8, (byte) 0xee, (byte) 0x99, - (byte) 0xc7, (byte) 0x73, (byte) 0x21, (byte) 0x8c, - (byte) 0x7e, (byte) 0xc8, (byte) 0xee, (byte) 0x99 }; - - int count = 1024; - // int keyLength = 256; - int keyLength = 128; - - String cipherAlgorithm = "AES/CBC/PKCS5Padding"; - String secretKeyAlgorithm = "PBKDF2WithHmacSHA1"; - SecretKeyFactory keyFac = SecretKeyFactory - .getInstance(secretKeyAlgorithm); - PBEKeySpec pbeKeySpec = new PBEKeySpec(password.toCharArray(), salt, - count, keyLength); - SecretKey tmp = keyFac.generateSecret(pbeKeySpec); - SecretKey secret = new SecretKeySpec(tmp.getEncoded(), "AES"); - Cipher ecipher = Cipher.getInstance(cipherAlgorithm); - ecipher.init(Cipher.ENCRYPT_MODE, secret, new IvParameterSpec(iv)); - - // decrypt - keyFac = SecretKeyFactory.getInstance(secretKeyAlgorithm); - pbeKeySpec = new PBEKeySpec(password.toCharArray(), salt, count, - keyLength); - tmp = keyFac.generateSecret(pbeKeySpec); - secret = new SecretKeySpec(tmp.getEncoded(), "AES"); - // AlgorithmParameters params = ecipher.getParameters(); - // byte[] iv = params.getParameterSpec(IvParameterSpec.class).getIV(); - Cipher dcipher = Cipher.getInstance(cipherAlgorithm); - dcipher.init(Cipher.DECRYPT_MODE, secret, new IvParameterSpec(iv)); - - byte[] encrypted = ecipher.doFinal(message.getBytes()); - byte[] decrypted = dcipher.doFinal(encrypted); - assertEquals(message, new String(decrypted)); - - ByteArrayOutputStream out = new ByteArrayOutputStream(); - CipherOutputStream cipherOut = new CipherOutputStream(out, ecipher); - cipherOut.write(message.getBytes()); - StreamUtils.closeQuietly(cipherOut); - byte[] enc = out.toByteArray(); - - ByteArrayInputStream in = new ByteArrayInputStream(enc); - CipherInputStream cipherIn = new CipherInputStream(in, dcipher); - ByteArrayOutputStream dec = new ByteArrayOutputStream(); - StreamUtils.copy(cipherIn, dec); - assertEquals(message, new String(dec.toByteArray())); - } +// public void testEncryptDecrypt() { +// final String password = "test long password since they are more powerful"; +// PasswordBasedEncryption pbeEnc = new PasswordBasedEncryption( +// password.toCharArray()); +// String message = "Hello World!"; +// byte[] encrypted = pbeEnc.encryptString(message); +// // System.out.println("Encrypted: '" + new String(encrypted) + "'"); +// PasswordBasedEncryption pbeDec = new PasswordBasedEncryption( +// password.toCharArray()); +// InputStream in = null; +// in = new ByteArrayInputStream(encrypted); +// String decrypted = pbeDec.decryptAsString(in); +// // System.out.println("Decrypted: '" + decrypted + "'"); +// StreamUtils.closeQuietly(in); +// assertEquals(message, decrypted); +// } +// +// public void testPBEWithMD5AndDES() throws Exception { +// String password = "test"; +// String message = "Hello World!"; +// +// byte[] salt = { (byte) 0xc7, (byte) 0x73, (byte) 0x21, (byte) 0x8c, +// (byte) 0x7e, (byte) 0xc8, (byte) 0xee, (byte) 0x99 }; +// +// int count = 1024; +// +// String cipherAlgorithm = "PBEWithMD5AndDES"; +// String secretKeyAlgorithm = "PBEWithMD5AndDES"; +// SecretKeyFactory keyFac = SecretKeyFactory +// .getInstance(secretKeyAlgorithm); +// PBEKeySpec pbeKeySpec = new PBEKeySpec(password.toCharArray()); +// PBEParameterSpec pbeParamSpec = new PBEParameterSpec(salt, count); +// SecretKey pbeKey = keyFac.generateSecret(pbeKeySpec); +// Cipher ecipher = Cipher.getInstance(cipherAlgorithm); +// ecipher.init(Cipher.ENCRYPT_MODE, pbeKey, pbeParamSpec); +// Cipher dcipher = Cipher.getInstance(cipherAlgorithm); +// dcipher.init(Cipher.DECRYPT_MODE, pbeKey, pbeParamSpec); +// +// byte[] encrypted = ecipher.doFinal(message.getBytes()); +// byte[] decrypted = dcipher.doFinal(encrypted); +// assertEquals(message, new String(decrypted)); +// +// } +// +// public void testPBEWithSHA1AndAES() throws Exception { +// String password = "test"; +// String message = "Hello World!"; +// +// byte[] salt = { (byte) 0xc7, (byte) 0x73, (byte) 0x21, (byte) 0x8c, +// (byte) 0x7e, (byte) 0xc8, (byte) 0xee, (byte) 0x99 }; +// byte[] iv = { (byte) 0xc7, (byte) 0x73, (byte) 0x21, (byte) 0x8c, +// (byte) 0x7e, (byte) 0xc8, (byte) 0xee, (byte) 0x99, +// (byte) 0xc7, (byte) 0x73, (byte) 0x21, (byte) 0x8c, +// (byte) 0x7e, (byte) 0xc8, (byte) 0xee, (byte) 0x99 }; +// +// int count = 1024; +// // int keyLength = 256; +// int keyLength = 128; +// +// String cipherAlgorithm = "AES/CBC/PKCS5Padding"; +// String secretKeyAlgorithm = "PBKDF2WithHmacSHA1"; +// SecretKeyFactory keyFac = SecretKeyFactory +// .getInstance(secretKeyAlgorithm); +// PBEKeySpec pbeKeySpec = new PBEKeySpec(password.toCharArray(), salt, +// count, keyLength); +// SecretKey tmp = keyFac.generateSecret(pbeKeySpec); +// SecretKey secret = new SecretKeySpec(tmp.getEncoded(), "AES"); +// Cipher ecipher = Cipher.getInstance(cipherAlgorithm); +// ecipher.init(Cipher.ENCRYPT_MODE, secret, new IvParameterSpec(iv)); +// +// // decrypt +// keyFac = SecretKeyFactory.getInstance(secretKeyAlgorithm); +// pbeKeySpec = new PBEKeySpec(password.toCharArray(), salt, count, +// keyLength); +// tmp = keyFac.generateSecret(pbeKeySpec); +// secret = new SecretKeySpec(tmp.getEncoded(), "AES"); +// // AlgorithmParameters params = ecipher.getParameters(); +// // byte[] iv = params.getParameterSpec(IvParameterSpec.class).getIV(); +// Cipher dcipher = Cipher.getInstance(cipherAlgorithm); +// dcipher.init(Cipher.DECRYPT_MODE, secret, new IvParameterSpec(iv)); +// +// byte[] encrypted = ecipher.doFinal(message.getBytes()); +// byte[] decrypted = dcipher.doFinal(encrypted); +// assertEquals(message, new String(decrypted)); +// +// ByteArrayOutputStream out = new ByteArrayOutputStream(); +// CipherOutputStream cipherOut = new CipherOutputStream(out, ecipher); +// cipherOut.write(message.getBytes()); +// StreamUtils.closeQuietly(cipherOut); +// byte[] enc = out.toByteArray(); +// +// ByteArrayInputStream in = new ByteArrayInputStream(enc); +// CipherInputStream cipherIn = new CipherInputStream(in, dcipher); +// ByteArrayOutputStream dec = new ByteArrayOutputStream(); +// StreamUtils.copy(cipherIn, dec); +// assertEquals(message, new String(dec.toByteArray())); +// } }