Discussions
Categories
- 197K All Categories
- 2.5K Data
- 546 Big Data Appliance
- 1.9K Data Science
- 450.8K Databases
- 221.9K General Database Discussions
- 3.8K Java and JavaScript in the Database
- 31 Multilingual Engine
- 552 MySQL Community Space
- 479 NoSQL Database
- 7.9K Oracle Database Express Edition (XE)
- 3.1K ORDS, SODA & JSON in the Database
- 556 SQLcl
- 4K SQL Developer Data Modeler
- 187.2K SQL & PL/SQL
- 21.4K SQL Developer
- 296.3K Development
- 17 Developer Projects
- 139 Programming Languages
- 293K Development Tools
- 110 DevOps
- 3.1K QA/Testing
- 646.1K Java
- 28 Java Learning Subscription
- 37K Database Connectivity
- 158 Java Community Process
- 105 Java 25
- 22.1K Java APIs
- 138.2K Java Development Tools
- 165.3K Java EE (Java Enterprise Edition)
- 19 Java Essentials
- 162 Java 8 Questions
- 86K Java Programming
- 81 Java Puzzle Ball
- 65.1K New To Java
- 1.7K Training / Learning / Certification
- 13.8K Java HotSpot Virtual Machine
- 94.3K Java SE
- 13.8K Java Security
- 205 Java User Groups
- 24 JavaScript - Nashorn
- Programs
- 468 LiveLabs
- 39 Workshops
- 10.2K Software
- 6.7K Berkeley DB Family
- 3.5K JHeadstart
- 5.7K Other Languages
- 2.3K Chinese
- 175 Deutsche Oracle Community
- 1.1K Español
- 1.9K Japanese
- 233 Portuguese
Encryption without special charactors.

Kashif Iftikhar
Member Posts: 45 Blue Ribbon
in Cryptography
Hi Everyone,
I'm trying to encrypt my file names and folders names.
For this I'm using javax.crypto and sun.misc.BASE64Encoder packages with UTF8 Char-set for encrypting strings which will be used as file/folder names..
The problems is that encrypted string contain special characters like (/,\) and they cannot be used in file/folder names.
Please guide me how can i correct this code. I tried different character set along with sun.misc Encoders but they did not work.
import java.io.UnsupportedEncodingException; import java.security.InvalidKeyException; import java.security.NoSuchAlgorithmException; import java.security.spec.KeySpec; import javax.crypto.Cipher; import javax.crypto.NoSuchPaddingException; import javax.crypto.SecretKey; import javax.crypto.SecretKeyFactory; import javax.crypto.spec.DESKeySpec; import javax.crypto.spec.DESedeKeySpec; import sun.misc.BASE64Encoder; import sun.misc.BASE64Decoder; public class MyEnc { public static final String DESEDE_ENCRYPTION_SCHEME = "DESede"; public static final String DES_ENCRYPTION_SCHEME = "DES"; public static final String DEFAULT_ENCRYPTION_KEY = "This is a fairly long phrase used to encrypt"; public static final String encryptionKey = "012345678901234567890123456789"; public static final String encryptionScheme = DESEDE_ENCRYPTION_SCHEME; private KeySpec keySpec; private SecretKeyFactory keyFactory; private Cipher cipher; private static final String UNICODE_FORMAT = "UTF8"; public MyEnc( ) throws EncryptionException { if ( encryptionKey == null ) throw new IllegalArgumentException( "encryption key was null" ); if ( encryptionKey.trim().length() < 24 ) throw new IllegalArgumentException( "encryption key was less than 24 characters" ); try { byte[] keyAsBytes = encryptionKey.getBytes( UNICODE_FORMAT ); if ( encryptionScheme.equals( DESEDE_ENCRYPTION_SCHEME) ) { keySpec = new DESedeKeySpec( keyAsBytes ); } else if ( encryptionScheme.equals( DES_ENCRYPTION_SCHEME ) ) { keySpec = new DESKeySpec( keyAsBytes ); } else { throw new IllegalArgumentException( "Encryption scheme not supported: " + encryptionScheme ); } keyFactory = SecretKeyFactory.getInstance( encryptionScheme ); cipher = Cipher.getInstance( encryptionScheme ); } catch (InvalidKeyException e) { throw new EncryptionException( e ); } catch (UnsupportedEncodingException e) { throw new EncryptionException( e ); } catch (NoSuchAlgorithmException e) { throw new EncryptionException( e ); } catch (NoSuchPaddingException e) { throw new EncryptionException( e ); } } public String encrypt( String unencryptedString ) throws EncryptionException { if ( unencryptedString == null || unencryptedString.trim().length() == 0 ) throw new IllegalArgumentException( "unencrypted string was null or empty" ); try { SecretKey key = keyFactory.generateSecret( keySpec ); cipher.init( Cipher.ENCRYPT_MODE, key ); byte[] cleartext = unencryptedString.getBytes( UNICODE_FORMAT ); byte[] ciphertext = cipher.doFinal( cleartext ); BASE64Encoder base64encoder = new BASE64Encoder(); return base64encoder.encode( ciphertext ); } catch (Exception e) { throw new EncryptionException( e ); } } public String decrypt( String encryptedString ) throws EncryptionException { if ( encryptedString == null || encryptedString.trim().length() <= 0 ) throw new IllegalArgumentException( "encrypted string was null or empty" ); try { SecretKey key = keyFactory.generateSecret( keySpec ); cipher.init( Cipher.DECRYPT_MODE, key ); BASE64Decoder base64decoder = new BASE64Decoder(); byte[] cleartext = base64decoder.decodeBuffer( encryptedString ); byte[] ciphertext = cipher.doFinal( cleartext ); return bytes2String( ciphertext ); } catch (Exception e) { throw new EncryptionException( e ); } } private static String bytes2String( byte[] bytes ) { StringBuffer stringBuffer = new StringBuffer(); for (int i = 0; i < bytes.length; i++) { stringBuffer.append( (char) bytes[i] ); } return stringBuffer.toString(); } public static class EncryptionException extends Exception { public EncryptionException( Throwable t ) { super( t ); } } }
This discussion has been closed.