⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 abstractcrypt.java

📁 Wicket一个开发Java Web应用程序框架。它使得开发web应用程序变得容易而轻松。 Wicket利用一个POJO data beans组件使得它可以与任何持久层技术相结合。
💻 JAVA
字号:
/* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements.  See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You under the Apache License, Version 2.0 * (the "License"); you may not use this file except in compliance with * the License.  You may obtain a copy of the License at * *      http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */package org.apache.wicket.util.crypt;import java.io.UnsupportedEncodingException;import java.security.GeneralSecurityException;import javax.crypto.Cipher;import org.apache.wicket.WicketRuntimeException;import org.slf4j.Logger;import org.slf4j.LoggerFactory;/** * Abstract base class for JCE based ICrypt implementations. *  * @author Juergen Donnerstag */public abstract class AbstractCrypt implements ICrypt{	/** Default encryption key */	private static final String DEFAULT_ENCRYPTION_KEY = "WiCkEt-CrYpT";	/** Encoding used to convert java String from and to byte[] */	private static final String CHARACTER_ENCODING = "UTF-8";	/** Log. */	private static final Logger log = LoggerFactory.getLogger(AbstractCrypt.class);	/** Key used to de-/encrypt the data */	private String encryptionKey = DEFAULT_ENCRYPTION_KEY;	/**	 * Constructor	 */	public AbstractCrypt()	{	}	/**	 * Decrypts a string into a string.	 * 	 * @param text	 *            text to decrypt	 * @return the decrypted text	 */	public final String decryptUrlSafe(final String text)	{		try		{			byte[] encrypted = Base64UrlSafe.decodeBase64(text.getBytes());			return new String(decryptByteArray(encrypted), CHARACTER_ENCODING);		}		catch (UnsupportedEncodingException ex)		{			throw new WicketRuntimeException(ex.getMessage());		}	}	/**	 * Encrypt a string into a string using URL safe Base64 encoding.	 * 	 * @param plainText	 *            text to encrypt	 * @return encrypted string	 */	public final String encryptUrlSafe(final String plainText)	{		try		{			byte[] cipherText = encryptStringToByteArray(plainText);			return new String(Base64UrlSafe.encodeBase64(cipherText));		}		catch (GeneralSecurityException e)		{			log.error("Unable to encrypt text '" + plainText + "'", e);			return null;		}	}	/**	 * Get encryption private key	 * 	 * @return encryption private key	 */	public String getKey()	{		return encryptionKey;	}	/**	 * Set encryption private key	 * 	 * @param key	 *            private key to make de-/encryption unique	 */	public void setKey(final String key)	{		encryptionKey = key;	}	/**	 * Crypts the given byte array	 * 	 * @param input	 *            byte array to be crypted	 * @param mode	 *            crypt mode	 * @return the input crypted. Null in case of an error	 * @throws GeneralSecurityException	 */	protected abstract byte[] crypt(final byte[] input, final int mode)			throws GeneralSecurityException;	/**	 * Decrypts an encrypted, but Base64 decoded byte array into a byte array.	 * 	 * @param encrypted	 *            byte array to decrypt	 * @return the decrypted text	 */	private final byte[] decryptByteArray(final byte[] encrypted)	{		try		{			return crypt(encrypted, Cipher.DECRYPT_MODE);		}		catch (GeneralSecurityException e)		{			throw new WicketRuntimeException("Unable to decrypt the text '" + encrypted.toString() +					"'", e);		}	}	/**	 * Encrypts the given text into a byte array.	 * 	 * @param plainText	 *            text to encrypt	 * @return the string encrypted	 * @throws GeneralSecurityException	 */	private final byte[] encryptStringToByteArray(final String plainText)			throws GeneralSecurityException	{		try		{			return crypt(plainText.getBytes(CHARACTER_ENCODING), Cipher.ENCRYPT_MODE);		}		catch (UnsupportedEncodingException ex)		{			throw new WicketRuntimeException(ex.getMessage());		}	}}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -