[Schmitzm-commits] r1795 - in trunk/schmitzm-core/src: main/java/de/schmitzm/crypt test/java/de/schmitzm/crypt

scm-commit@wald.intevation.org scm-commit at wald.intevation.org
Thu Nov 24 12:19:04 CET 2011


Author: mojays
Date: 2011-11-24 12:19:04 +0100 (Thu, 24 Nov 2011)
New Revision: 1795

Modified:
   trunk/schmitzm-core/src/main/java/de/schmitzm/crypt/CryptUtil.java
   trunk/schmitzm-core/src/test/java/de/schmitzm/crypt/CryptUtilTest.java
Log:
CryptUtil: bytes/string conversion modified

Modified: trunk/schmitzm-core/src/main/java/de/schmitzm/crypt/CryptUtil.java
===================================================================
--- trunk/schmitzm-core/src/main/java/de/schmitzm/crypt/CryptUtil.java	2011-11-23 19:19:26 UTC (rev 1794)
+++ trunk/schmitzm-core/src/main/java/de/schmitzm/crypt/CryptUtil.java	2011-11-24 11:19:04 UTC (rev 1795)
@@ -26,16 +26,70 @@
  */
 public class CryptUtil {
 
+//  /**
+//   * An encrypted byte array can not be converted to string by
+//   * {@code new String(byte[])} and {@code String.getBytes()} (e.g.
+//   * to store the encrypted value in a {@link Properties} file)!
+//   * This string creation destroys the encrypted value!<br>
+//   * This method simply converts the byte array (the encrypted string)
+//   * to a comma-separated list of the numeric byte values. This string
+//   * can riskless be stored in {@link Properties} file.<br>
+//   * The other way around: {@link #convertWritableStringToEncryptedBytes(String)}
+//   * method can be used to convert the comma-separated byte list back to the
+//   * byte array (encrypted value).
+//   * @param encryptedText encrypted text
+//   * @see #convertWritableStringToEncryptedBytes(String)
+//   */
+//  public static String convertEncryptedBytesToWritableString(byte[] encryptedText) {
+//    if ( encryptedText == null )
+//      return null;
+//    StringBuffer str = new StringBuffer();
+//    for (int i=0; i<encryptedText.length; i++) {
+//      if (i>0)
+//        str.append(',');
+//      str.append(encryptedText[i]);
+//    }
+//    return str.toString();
+//  }
+//
+//  /**
+//   * An encrypted byte array can not be converted to string by
+//   * {@code new String(byte[])} and {@code String.getBytes()} (e.g.
+//   * to store the encrypted value in a {@link Properties} file)!
+//   * This string creation destroys the encrypted value!<br>
+//   * {@link #convertEncryptedBytesToWritableString(byte[])} converts
+//   * the byte array (the encrypted string) to a comma-separated list
+//   * of the numeric byte values. This string can stored without risk
+//   * in {@link Properties} file.<br>
+//   * This method can be used to convert the string (comma-separated byte list)
+//   * back to the byte array (encrypted value).
+//   * @param byteList comma-separated list of numeric byte values
+//   * @see #convertEncryptedBytesToWritableString(byte[])
+//   */
+//  public static byte[] convertWritableStringToEncryptedBytes(String byteList) {
+//    if ( byteList == null )
+//      return null;
+//    try {
+//      String[] strParts = byteList.split(",");
+//      byte[]   bytes = new byte[strParts.length];
+//      for (int i=0; i<bytes.length; i++)
+//        bytes[i] = Byte.parseByte(strParts[i]);
+//      return bytes;
+//    } catch (Exception err) {
+//      throw new IllegalArgumentException("Byte list does not represent an encrypted string generated by convertEncryptedBytesToWritableString(.): "+byteList);
+//    }
+//  }
+
   /**
    * An encrypted byte array can not be converted to string by
-   * {@code new String(byte[])} (e.g. to store the encrypted value
-   * in a {@link Properties} file)! This string creation destroys the
-   * encrypted value!<br> 
+   * {@code new String(byte[])} and {@code String.getBytes()} (e.g.
+   * to store the encrypted value in a {@link Properties} file)!
+   * This string creation destroys the encrypted value!<br>
    * This method simply converts the byte array (the encrypted string)
-   * to a comma-separated list of the numeric byte values. This string
-   * can riskless stored in {@link Properties} file.<br>
+   * manually to chars, which are concatenated to a string. This string
+   * can riskless be stored in {@link Properties} file.<br>
    * The other way around: {@link #convertWritableStringToEncryptedBytes(String)}
-   * method can be used to convert the comma-separated byte list back to the
+   * method can be used to re-convert this string back to the
    * byte array (encrypted value).
    * @param encryptedText encrypted text
    * @see #convertWritableStringToEncryptedBytes(String)
@@ -43,43 +97,40 @@
   public static String convertEncryptedBytesToWritableString(byte[] encryptedText) {
     if ( encryptedText == null )
       return null;
-    StringBuffer str = new StringBuffer();
+    //  Statt "new String(byte[])" die als char interpretierten
+    //  bytes manuell aneinander haengen.
+    String encryptedTextStr = "";
     for (int i=0; i<encryptedText.length; i++) {
-      if (i>0)
-        str.append(',');
-      str.append(encryptedText[i]);
+      encryptedTextStr += (char)encryptedText[i];
     }
-    return str.toString();
+    return encryptedTextStr;
   }
 
   /**
    * An encrypted byte array can not be converted to string by
-   * {@code new String(byte[])} (e.g. to store the encrypted value
-   * in a {@link Properties} file)! This string creation destroys the
-   * encrypted value!<br>
+   * {@code new String(byte[])} and {@code String.getBytes()} (e.g.
+   * to store the encrypted value in a {@link Properties} file)!
+   * This string creation destroys the encrypted value!<br>
    * {@link #convertEncryptedBytesToWritableString(byte[])} converts
-   * the byte array (the encrypted string) to a comma-separated list
-   * of the numeric byte values. This string can stored without risk
-   * in {@link Properties} file.<br>
-   * This method can be used to convert the string (comma-separated byte list)
+   * the byte array (the encrypted string) to a char-concatenated 
+   * string. This string can stored without risk in {@link Properties}
+   * file.<br>
+   * This method can be used to re-convert this kind of string
    * back to the byte array (encrypted value).
-   * @param byteList comma-separated list of numeric byte values
+   * @param byteList char-concatenation of bytes
    * @see #convertEncryptedBytesToWritableString(byte[])
    */
   public static byte[] convertWritableStringToEncryptedBytes(String byteList) {
     if ( byteList == null )
       return null;
-    try {
-      String[] strParts = byteList.split(",");
-      byte[]   bytes = new byte[strParts.length];
-      for (int i=0; i<bytes.length; i++)
-        bytes[i] = Byte.parseByte(strParts[i]);
-      return bytes;
-    } catch (Exception err) {
-      throw new IllegalArgumentException("Byte list does not represent an encrypted string generated by convertEncryptedBytesToWritableString(.): "+byteList);
-    }
+    //  Statt "String.getBytes()" die einzelnen Zeichen des Strings
+    //  bytes manuell als bytes interpretieren
+    byte[] bytes = new byte[byteList.length()];
+    for (int i=0; i<bytes.length; i++)
+      bytes[i] = (byte)byteList.charAt(i);
+    return bytes;
   }
-
+  
     /**
      * Creates a DEC cipher.
      * @param cryptMode  {@link Cipher#ENCRYPT_MODE} or {@link Cipher#DECRYPT_MODE}

Modified: trunk/schmitzm-core/src/test/java/de/schmitzm/crypt/CryptUtilTest.java
===================================================================
--- trunk/schmitzm-core/src/test/java/de/schmitzm/crypt/CryptUtilTest.java	2011-11-23 19:19:26 UTC (rev 1794)
+++ trunk/schmitzm-core/src/test/java/de/schmitzm/crypt/CryptUtilTest.java	2011-11-24 11:19:04 UTC (rev 1795)
@@ -16,21 +16,46 @@
 
       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));
+      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()));
+      System.out.println("Encrypted string:       "+encrStr);
+      System.out.println("Encrypted bytes:        "+Arrays.toString(encrBytes));
+//      System.out.println("Encrypted 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()));
+      System.out.println("Decrypted string:       "+decrStr);
+      System.out.println("Decrypted string bytes: "+Arrays.toString(decrStr.getBytes()));
       
       assertEquals(text,decrStr);
       assertTrue(Arrays.equals(textBytes,decrStr.getBytes()));
 	}
+	
+	@Test
+	public void testByteStringConversion() throws Exception {
+	  final byte[] keyBytes = "MeinKeya".getBytes();
+	  
+	  String text = "abc.-\"ABC!!---!\\";
+      byte[] textBytes = text.getBytes();
+	  byte[] encrBytes = CryptUtil.encryptString(text, keyBytes);
+	  System.out.println("Original string:                  "+text+" ("+text.length()+")");
+      System.out.println("Original string bytes:            "+Arrays.toString(textBytes));
+      System.out.println("Encrypted bytes:                  "+Arrays.toString(encrBytes)+" ("+encrBytes.length+")");
 
+      String encrStr = CryptUtil.convertEncryptedBytesToWritableString(encrBytes);
+      byte[] encrStrBytes  = encrStr.getBytes();
+      byte[] encrStrBytes2 = CryptUtil.convertWritableStringToEncryptedBytes(encrStr);
+      System.out.println("Encrypted string:                 "+encrStr+" ("+encrStr.length()+")");
+      System.out.println("Encrypted string bytes:           "+Arrays.toString(encrStrBytes)+" ("+encrStrBytes.length+")");
+      System.out.println("Encrypted string converted bytes: "+Arrays.toString(encrStrBytes2)+" ("+encrStrBytes2.length+")");
+      
+      String decrStr = CryptUtil.decryptString(encrStrBytes2, keyBytes);
+      System.out.println("Decrypted string:                 "+decrStr+" ("+decrStr.length()+")");
+      System.out.println("Decrypted string bytes:           "+Arrays.toString(decrStr.getBytes()));
+
+      assertEquals(text,decrStr);
+      assertTrue(Arrays.equals(textBytes,decrStr.getBytes()));
+	}
 }



More information about the Schmitzm-commits mailing list