1 /* 2 * Copyright (c) 2002-2017, Mairie de Paris 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 9 * 1. Redistributions of source code must retain the above copyright notice 10 * and the following disclaimer. 11 * 12 * 2. Redistributions in binary form must reproduce the above copyright notice 13 * and the following disclaimer in the documentation and/or other materials 14 * provided with the distribution. 15 * 16 * 3. Neither the name of 'Mairie de Paris' nor 'Lutece' nor the names of its 17 * contributors may be used to endorse or promote products derived from 18 * this software without specific prior written permission. 19 * 20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 21 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 23 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE 24 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 30 * POSSIBILITY OF SUCH DAMAGE. 31 * 32 * License 1.0 33 */ 34 package fr.paris.lutece.plugins.myapps.util.crypto; 35 36 import fr.paris.lutece.portal.service.util.AppLogService; 37 38 import java.security.Provider; 39 import java.security.Security; 40 41 import javax.crypto.Cipher; 42 import javax.crypto.spec.DESKeySpec; 43 import javax.crypto.spec.SecretKeySpec; 44 45 46 /** 47 * This class provides some simple functions to encrypt or decrypt data. 48 * This implementation is based on the javax.crypto.* API and the Sun implementation. 49 * The algoritm used is the DES algorithm (symetric) 50 * 51 * @since 1.2.1 52 */ 53 @Deprecated 54 public final class CryptoUtil 55 { 56 private static final String ALGORITHM_DES = "DES"; 57 private static final String ENCODING_UTF8 = "UTF8"; 58 59 /** Private constructor */ 60 private CryptoUtil( ) 61 { 62 } 63 64 /** 65 * This function encrypt a given string using the DES algorithm. 66 * @param strDataToEncrypt The String to encrypt 67 * @param strKey The generated key used to encrypt 68 * @return The encrypted string 69 */ 70 public static String encrypt( String strDataToEncrypt, String strKey ) 71 { 72 byte[] key = strKey.getBytes( ); 73 74 // Get the KeyGenerator 75 Provider sunJCE = new com.sun.crypto.provider.SunJCE( ); 76 Security.addProvider( sunJCE ); 77 78 String strAlgorithm = ALGORITHM_DES; //On utilise un algorithme DES 79 SecretKeySpec keySpec = null; 80 DESKeySpec deskey = null; 81 String strResult = ""; 82 83 try 84 { 85 // Prepare the key 86 deskey = new DESKeySpec( key ); 87 keySpec = new SecretKeySpec( deskey.getKey( ), ALGORITHM_DES ); 88 89 // Instantiate the cipher 90 Cipher cipher = Cipher.getInstance( strAlgorithm ); 91 92 // Encrypt data 93 cipher.init( Cipher.ENCRYPT_MODE, keySpec ); 94 95 // Encode the string into bytes using utf-8 96 byte[] utf8 = strDataToEncrypt.getBytes( ENCODING_UTF8 ); // FIXME ? 97 98 // Encrypt 99 byte[] enc = cipher.doFinal( utf8 ); 100 101 // Encode bytes to base64 to get a string 102 strResult = new sun.misc.BASE64Encoder( ).encode( enc ); 103 } 104 catch ( Exception e ) 105 { 106 AppLogService.error( "Data encryption error", e ); 107 } 108 109 return strResult; 110 } 111 112 /** 113 * This function decrypt a given string using the DES algorithm. 114 * @param strDataToDecrypt The String to decrypt 115 * @param strKey The generated key used to decrypt 116 * @return The encrypted string 117 */ 118 public static String decrypt( String strDataToDecrypt, String strKey ) 119 { 120 byte[] key = strKey.getBytes( ); 121 122 // Get the KeyGenerator 123 Provider sunJCE = new com.sun.crypto.provider.SunJCE( ); 124 Security.addProvider( sunJCE ); 125 126 String strAlgorithm = ALGORITHM_DES; 127 SecretKeySpec keySpec = null; 128 DESKeySpec deskey = null; 129 String strResult = ""; 130 131 try 132 { 133 // Prepare the key 134 deskey = new DESKeySpec( key ); 135 keySpec = new SecretKeySpec( deskey.getKey( ), ALGORITHM_DES ); 136 137 // Instantiate the cipher 138 Cipher cipher = Cipher.getInstance( strAlgorithm ); 139 cipher.init( Cipher.DECRYPT_MODE, keySpec ); 140 141 // Decrypt data 142 // Decode base64 to get bytes 143 byte[] dec = new sun.misc.BASE64Decoder( ).decodeBuffer( strDataToDecrypt ); 144 145 // Decrypt 146 byte[] utf8 = cipher.doFinal( dec ); 147 148 // Decode using utf-8 149 return new String( utf8, ENCODING_UTF8 ); 150 } 151 152 catch ( Exception e ) 153 { 154 AppLogService.error( "Data decryption error", e ); 155 } 156 157 return strResult; 158 } 159 }