[Schmitzm-commits] r1793 - in trunk/schmitzm-core/src: main/java/de/schmitzm/crypt main/java/de/schmitzm/lang test/java/de/schmitzm/crypt
scm-commit@wald.intevation.org
scm-commit at wald.intevation.org
Wed Nov 23 18:36:20 CET 2011
Author: mojays
Date: 2011-11-23 18:36:20 +0100 (Wed, 23 Nov 2011)
New Revision: 1793
Added:
trunk/schmitzm-core/src/main/java/de/schmitzm/crypt/CryptUtil.java
trunk/schmitzm-core/src/test/java/de/schmitzm/crypt/CryptUtilTest.java
Modified:
trunk/schmitzm-core/src/main/java/de/schmitzm/lang/LangUtil.java
Log:
new CryptUtil
Added: trunk/schmitzm-core/src/main/java/de/schmitzm/crypt/CryptUtil.java
===================================================================
--- trunk/schmitzm-core/src/main/java/de/schmitzm/crypt/CryptUtil.java 2011-11-22 12:23:00 UTC (rev 1792)
+++ trunk/schmitzm-core/src/main/java/de/schmitzm/crypt/CryptUtil.java 2011-11-23 17:36:20 UTC (rev 1793)
@@ -0,0 +1,120 @@
+/*******************************************************************************
+ * Copyright (c) 2009 Martin O. J. Schmitz.
+ *
+ * Contributors:
+ * Martin O. J. Schmitz
+ ******************************************************************************/
+
+package de.schmitzm.crypt;
+
+import java.security.GeneralSecurityException;
+import java.security.NoSuchAlgorithmException;
+import java.security.NoSuchProviderException;
+
+import javax.crypto.Cipher;
+import javax.crypto.NoSuchPaddingException;
+import javax.crypto.SecretKeyFactory;
+import javax.crypto.spec.DESKeySpec;
+import javax.crypto.spec.IvParameterSpec;
+import javax.crypto.spec.SecretKeySpec;
+import javax.management.RuntimeErrorException;
+
+/**
+ * Utility methods for string encryption and decryption.
+ * @author <a href="mailto:martin.schmitz at koeln.de">Martin Schmitz</a>
+ */
+public class CryptUtil {
+
+ /**
+ * Creates a DEC cipher.
+ * @param cryptMode {@link Cipher#ENCRYPT_MODE} or {@link Cipher#DECRYPT_MODE}
+ * @param keyBytes encryption key
+ */
+ public static Cipher getDESCipher(final int cryptMode, final byte[] keyBytes) throws GeneralSecurityException {
+ assert cryptMode == Cipher.ENCRYPT_MODE || cryptMode == Cipher.DECRYPT_MODE;
+ assert keyBytes != null;
+ Cipher cipher = Cipher.getInstance("DES/CBC/PKCS5Padding");
+ cipher.init(
+ cryptMode,
+ SecretKeyFactory.getInstance("DES").generateSecret(new DESKeySpec(keyBytes)),
+ new IvParameterSpec(new byte[] { 0x10, 0x10, 0x01, 0x04, 0x01, 0x01, 0x01, 0x02 } )
+ );
+ return cipher;
+ }
+
+ /**
+ * Encrypts or decrypts a string. Encryption or decryption depends on the cipher.
+ * @param inputBytes string to encrypt or decrypt
+ * @param cipher crypt cipher
+ */
+ public static byte[] cryptBytes(byte[] inputBytes, Cipher cipher) {
+ try {
+ // Encrypt bytes
+ byte[] outputBytes = cipher.doFinal(inputBytes);
+ return outputBytes;
+ } catch (Exception err) {
+ throw new RuntimeException(err);
+ }
+ }
+
+ /**
+ * Encrypts a string using the given cipher.
+ * @param clearText string to encrypt
+ * @param cipher crypt cipher
+ */
+ public static byte[] encryptString(String clearText, Cipher cipher) {
+ byte[] decryptBytes = clearText.getBytes();
+ byte[] encryptBytes = cryptBytes(decryptBytes,cipher);
+ return encryptBytes;
+ }
+
+ /**
+ * Decrypts a string using the given cipher.
+ * @param encryptedText string to decrypt
+ * @param cipher crypt cipher
+ */
+ public static String decryptString(byte[] encryptedText, Cipher cipher) {
+ byte[] decyptBytes = cryptBytes(encryptedText,cipher);
+ String decryptStr = new String(decyptBytes);
+ return decryptStr;
+ }
+
+ /**
+ * Encrypts or decrypts a string using the given bytes as encryption key with DES encryption.
+ * @param inputStr string to encrypt or decrypt
+ * @param keyBytes crypt key
+ * @param cryptMode {@link Cipher#ENCRYPT_MODE} or {@link Cipher#DECRYPT_MODE}
+ * @see #CIPHER_DES
+ */
+ public static byte[] cryptBytes(byte[] inputStr, byte[] keyBytes, int cryptMode) {
+ try {
+ Cipher cipher = getDESCipher(cryptMode, keyBytes);
+ return cryptBytes(inputStr, cipher);
+ } catch (Exception err) {
+ throw new RuntimeException(err);
+ }
+ }
+
+ /**
+ * Encrypts a string using the given bytes as encryption key with DES encryption.
+ * @param clearText string to encrypt
+ * @param keyBytes encryption key
+ */
+ public static byte[] encryptString(String clearText, byte[] keyBytes) {
+ byte[] decryptBytes = clearText.getBytes();
+ byte[] encryptBytes = cryptBytes(decryptBytes, keyBytes, Cipher.ENCRYPT_MODE);
+ return encryptBytes;
+ }
+
+ /**
+ * Decrypts a string using the given bytes as encryption key with DES encryption.
+ * @param encryptedText string to decrypt
+ * @param keyBytes encryption key
+ */
+ public static String decryptString(byte[] encryptedText, byte[] keyBytes) {
+ byte[] decyptBytes = cryptBytes(encryptedText,keyBytes, Cipher.DECRYPT_MODE);
+ String decryptStr = new String(decyptBytes);
+ return decryptStr;
+ }
+
+}
Modified: trunk/schmitzm-core/src/main/java/de/schmitzm/lang/LangUtil.java
===================================================================
--- trunk/schmitzm-core/src/main/java/de/schmitzm/lang/LangUtil.java 2011-11-22 12:23:00 UTC (rev 1792)
+++ trunk/schmitzm-core/src/main/java/de/schmitzm/lang/LangUtil.java 2011-11-23 17:36:20 UTC (rev 1793)
@@ -2095,5 +2095,4 @@
// else
// return String.format("%02d:%02d:%02d.%03d", days, hr, min, sec, ms);
}
-
}
Added: trunk/schmitzm-core/src/test/java/de/schmitzm/crypt/CryptUtilTest.java
===================================================================
--- trunk/schmitzm-core/src/test/java/de/schmitzm/crypt/CryptUtilTest.java 2011-11-22 12:23:00 UTC (rev 1792)
+++ trunk/schmitzm-core/src/test/java/de/schmitzm/crypt/CryptUtilTest.java 2011-11-23 17:36:20 UTC (rev 1793)
@@ -0,0 +1,36 @@
+package de.schmitzm.crypt;
+
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertTrue;
+
+import java.util.Arrays;
+
+import org.junit.Test;
+
+public class CryptUtilTest {
+ @Test
+ public void testDefaultEncryption() throws Exception {
+ final String key = "sowasvongeheim";
+ final byte[] keyBytes = key.getBytes();
+
+ final String text = "Hello World!!";
+ final byte[] textBytes = text.getBytes();
+ System.out.println("Orginal string: "+text);
+ System.out.println("Orginal string bytes: "+Arrays.toString(textBytes));
+
+ byte[] encrBytes = CryptUtil.encryptString(text, keyBytes);
+ String encrStr = new String(encrBytes);
+ System.out.println("Encrypt string: "+encrStr);
+ System.out.println("Encrypt bytes: "+Arrays.toString(encrBytes));
+ System.out.println("Encrypt string bytes: "+Arrays.toString(encrStr.getBytes()));
+
+ String decrStr = CryptUtil.decryptString(encrBytes, keyBytes);
+ System.out.println("Decrypt string: "+decrStr);
+ System.out.println("Decrypt string bytes: "+Arrays.toString(decrStr.getBytes()));
+
+ assertEquals(text,decrStr);
+ assertTrue(Arrays.equals(textBytes,decrStr.getBytes()));
+ }
+
+}
More information about the Schmitzm-commits
mailing list