📄 normalcryptor.java
字号:
/*
Simple Implementation of Kerberos protocol v5
Copyright (C) 2003 Thia Yeo Ching (tycordinal@yahoo.co.uk)
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
/*
Since this class combined the code found on
http://javaalmanac.com , a permission is requested
from the website owners at xeo.com to make it GPLed
and include in this package:
==========================================
Date: Mon, 05 May 2003 15:31:54 -0700
To: "tycordinal" <tycordinal@yahoo.co.uk>
From: "Patrick Chan" <chan2002@xeo.com>
Subject: Re: make portion of code GPLed
you have my permission.
Thanks for asking.
cheers,
pat
At 09:50 AM 5/5/2003 +0100, you wrote:
>hi,
>
>I have made a small library at
>http://sourceforge.net/projects/javakerberos/ , to
>provide a simple kerberos implementation with RMI.
>However I haven't upload the code yet, since I got a
>small problem here: inside one of my classes I have
>combined the example code at
>http://javaalmanac.com/egs/javax.crypto/PassKey.html
>and
>http://javaalmanac.com/egs/javax.crypto/EncryptObject.html?l=rel
>.
>
>I hereby request permission for making combined
>portion of the above mentioned code available in my
>class and I promise making them GPLed. Please reply to
>me so that I can really do like that.
>
>Thank you very much.
>
>ps. Here is the class that make use of the
>combination:
>----------------(contents of this class)
============================================
*/
package SimpleKerberos.tool;
import javax.crypto.*;
import javax.crypto.spec.PBEKeySpec;
import javax.crypto.spec.PBEParameterSpec;
import java.io.Serializable;
import java.io.UnsupportedEncodingException;
import java.security.spec.AlgorithmParameterSpec;
import java.security.spec.KeySpec;
/**
* Use DES for encrypy/decrypt
*/
public class NormalCryptor implements ICryptor
{
Cipher ecipher;
Cipher dcipher;
// 8-byte Salt
byte[] salt = {
(byte) 0xA9, (byte) 0x9B, (byte) 0xC8, (byte) 0x32,
(byte) 0x56, (byte) 0x35, (byte) 0xE3, (byte) 0x03
};
// Iteration count
int iterationCount = 19;
private static final String CRYPTOR_ALGO_NAME = "PBEWithMD5AndDES";
public SealedObject encryptObject(Serializable obj)
{
SealedObject so = null;
try
{
so = new SealedObject(obj, get_ecipher());
}
catch (Exception e)
{
e.printStackTrace();
}
return so;
}
public Object decryptObject(SealedObject so)
{
Object o = null;
try
{
o = so.getObject(get_dcipher());
}
catch (Exception e)
{
e.printStackTrace();
}
return o;
}
public Cipher get_ecipher()
{
return ecipher;
}
public Cipher get_dcipher()
{
return dcipher;
}
/**
* Encrypt/decrypt with the password
* @param hashedClientPassword hashed value of client password;
* shall be the HashedPassword in authentication database.
*/
public NormalCryptor(String hashedClientPassword)
{
try
{
// Create the key
KeySpec keySpec = new PBEKeySpec(hashedClientPassword.toCharArray(), salt, iterationCount);
SecretKey key = SecretKeyFactory.getInstance(
CRYPTOR_ALGO_NAME).generateSecret(keySpec);
ecipher = Cipher.getInstance(key.getAlgorithm());
dcipher = Cipher.getInstance(key.getAlgorithm());
// Prepare the parameter to the ciphers
AlgorithmParameterSpec paramSpec = new PBEParameterSpec(salt, iterationCount);
// Create the ciphers
ecipher.init(Cipher.ENCRYPT_MODE, key, paramSpec);
dcipher.init(Cipher.DECRYPT_MODE, key, paramSpec);
}
catch (java.security.InvalidAlgorithmParameterException e)
{
e.printStackTrace();
}
catch (java.security.spec.InvalidKeySpecException e)
{
e.printStackTrace();
}
catch (javax.crypto.NoSuchPaddingException e)
{
e.printStackTrace();
}
catch (java.security.NoSuchAlgorithmException e)
{
e.printStackTrace();
}
catch (java.security.InvalidKeyException e)
{
e.printStackTrace();
}
}
public String encrypt(String str)
{
try
{
// Encode the string into bytes using utf-8
byte[] utf8 = str.getBytes("UTF8");
// Encrypt
byte[] enc = ecipher.doFinal(utf8);
// Encode bytes to base64 to get a string
return new sun.misc.BASE64Encoder().encode(enc);
}
catch (javax.crypto.BadPaddingException e)
{
e.printStackTrace();
}
catch (IllegalBlockSizeException e)
{
e.printStackTrace();
}
catch (UnsupportedEncodingException e)
{
e.printStackTrace();
}
catch (java.io.IOException e)
{
e.printStackTrace();
}
return null;
}
/**
*
* @param str a string encrypted using encrypt()
* @return a string if pass phrase is given correctly and no exception occured;
* otherwise null if passphrase given is not correct
*/
public String decrypt(String str)
{
try
{
// Decode base64 to get bytes
byte[] dec = new sun.misc.BASE64Decoder().decodeBuffer(str);
// Decrypt
byte[] utf8 = dcipher.doFinal(dec);
// Decode using utf-8
return new String(utf8, "UTF8");
}
catch (BadPaddingException e)
{
e.printStackTrace();
}
catch (IllegalBlockSizeException e)
{
e.printStackTrace();
}
catch (UnsupportedEncodingException e)
{
e.printStackTrace();
}
catch (java.io.IOException e)
{
e.printStackTrace();
}
return null;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -