📄 cryptohandlerecc.java
字号:
/*
* Created on 15 Jun 2006
* Created by Paul Gardner
* Copyright (C) 2006 Aelitis, All Rights Reserved.
*
* 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.
*
* AELITIS, SAS au capital de 46,603.30 euros
* 8 Allee Lenotre, La Grille Royale, 78600 Le Mesnil le Roi, France.
*
*/
package com.aelitis.azureus.core.security.impl;
import java.math.BigInteger;
import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.Key;
import java.security.KeyFactory;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.SecureRandom;
import java.security.Signature;
import java.security.spec.AlgorithmParameterSpec;
import java.security.spec.KeySpec;
import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import org.bouncycastle.jce.ECNamedCurveTable;
import org.bouncycastle.jce.interfaces.ECPrivateKey;
import org.bouncycastle.jce.interfaces.ECPublicKey;
import org.bouncycastle.jce.provider.JCEIESCipher;
import org.bouncycastle.jce.spec.ECNamedCurveParameterSpec;
import org.bouncycastle.jce.spec.ECParameterSpec;
import org.bouncycastle.jce.spec.ECPrivateKeySpec;
import org.bouncycastle.jce.spec.ECPublicKeySpec;
import org.bouncycastle.jce.spec.IEKeySpec;
import org.bouncycastle.jce.spec.IESParameterSpec;
import org.bouncycastle.math.ec.ECPoint;
import org.gudy.azureus2.core3.config.COConfigurationManager;
import org.gudy.azureus2.core3.util.SystemTime;
import com.aelitis.azureus.core.security.CryptoHandler;
import com.aelitis.azureus.core.security.CryptoManager;
import com.aelitis.azureus.core.security.CryptoManagerException;
import com.aelitis.azureus.core.security.CryptoManagerPasswordException;
import com.aelitis.azureus.core.security.CryptoManagerPasswordHandler;
import com.aelitis.azureus.core.security.CryptoSTSEngine;
public class
CryptoHandlerECC
implements CryptoHandler
{
private static final ECNamedCurveParameterSpec ECCparam = ECNamedCurveTable.getParameterSpec("prime192v2");
private static final byte[] ECIES_D = new byte[] {(byte)0x6d, (byte)0xc1, (byte)0x62, (byte)0x32, (byte)0x15, (byte)0x4d, (byte)0x0f, (byte)0x7b };
private static final byte[] ECIES_E = new byte[] {(byte)0x6a, (byte)0x64, (byte)0x98, (byte)0xde, (byte)0x1a, (byte)0xa4, (byte)0x98, (byte)0xcc };
private static final int TIMEOUT_DEFAULT_SECS = 60*60;
private CryptoManagerImpl manager;
private String CONFIG_PREFIX = "core.crypto.ecc.";
private PrivateKey use_method_private_key;
private PublicKey use_method_public_key;
private long last_unlock_time;
protected
CryptoHandlerECC(
CryptoManagerImpl _manager,
int _instance_id )
{
manager = _manager;
CONFIG_PREFIX += _instance_id + ".";
}
public void
unlock(
char[] password )
throws CryptoManagerException
{
getMyPrivateKey( password, "" );
}
public synchronized void
lock()
{
use_method_private_key = null;
}
public int
getUnlockTimeoutSeconds()
{
return( COConfigurationManager.getIntParameter( CONFIG_PREFIX + "timeout", TIMEOUT_DEFAULT_SECS ));
}
public void
setUnlockTimeoutSeconds(
int secs )
{
COConfigurationManager.setParameter( CONFIG_PREFIX + "timeout", secs );
}
public byte[]
sign(
byte[] data,
char[] password )
throws CryptoManagerException
{
return( sign( data, password, null ));
}
public byte[]
sign(
byte[] data,
String reason )
throws CryptoManagerException
{
return( sign( data, null, reason ));
}
protected byte[]
sign(
byte[] data,
char[] password,
String reason )
throws CryptoManagerException
{
PrivateKey priv = getMyPrivateKey( password, reason );
Signature sig = getSignature( priv );
try{
sig.update( data );
return( sig.sign());
}catch( Throwable e ){
throw( new CryptoManagerException( "Signature failed", e ));
}
}
public boolean
verify(
byte[] public_key,
byte[] data,
byte[] signature )
throws CryptoManagerException
{
PublicKey pub = rawdataToPubkey( public_key );
Signature sig = getSignature( pub );
try{
sig.update( data );
return( sig.verify( signature ));
}catch( Throwable e ){
throw( new CryptoManagerException( "Signature failed", e ));
}
}
public byte[]
encrypt(
byte[] other_public_key,
byte[] data,
char[] password )
throws CryptoManagerException
{
return( encrypt( other_public_key, data, password, null ));
}
public byte[]
encrypt(
byte[] other_public_key,
byte[] data,
String reason )
throws CryptoManagerException
{
return( encrypt( other_public_key, data, null, reason ));
}
protected byte[]
encrypt(
byte[] other_public_key,
byte[] data,
char[] password,
String reason )
throws CryptoManagerException
{
try{
IEKeySpec key_spec = new IEKeySpec( getMyPrivateKey( password, reason ), rawdataToPubkey( other_public_key ));
IESParameterSpec param = new IESParameterSpec(ECIES_D, ECIES_E, 128);
InternalECIES cipher = new InternalECIES();
cipher.internalEngineInit( Cipher.ENCRYPT_MODE, key_spec, param, null );
return( cipher.internalEngineDoFinal(data, 0, data.length ));
}catch( CryptoManagerException e ){
throw( e );
}catch( Throwable e){
throw( new CryptoManagerException( "Encrypt failed", e ));
}
}
public byte[]
decrypt(
byte[] other_public_key,
byte[] data,
char[] password )
throws CryptoManagerException
{
return( decrypt( other_public_key, data, password, null ));
}
public byte[]
decrypt(
byte[] other_public_key,
byte[] data,
String reason )
throws CryptoManagerException
{
return( decrypt( other_public_key, data, null, reason ));
}
protected byte[]
decrypt(
byte[] other_public_key,
byte[] data,
char[] password,
String reason )
throws CryptoManagerException
{
try{
IEKeySpec key_spec = new IEKeySpec( getMyPrivateKey( password, reason ), rawdataToPubkey( other_public_key ));
IESParameterSpec param = new IESParameterSpec(ECIES_D, ECIES_E, 128);
InternalECIES cipher = new InternalECIES();
cipher.internalEngineInit( Cipher.DECRYPT_MODE, key_spec, param, null );
return( cipher.internalEngineDoFinal(data, 0, data.length ));
}catch( CryptoManagerException e ){
throw( e );
}catch( Throwable e){
throw( new CryptoManagerException( "Decrypt failed", e ));
}
}
public CryptoSTSEngine
getSTSEngine(
char[] password )
throws CryptoManagerException
{
return( getSTSEngine( password, null ));
}
public CryptoSTSEngine
getSTSEngine(
String reason )
throws CryptoManagerException
{
return( getSTSEngine( null, reason ));
}
protected CryptoSTSEngine
getSTSEngine(
char[] password,
String reason )
throws CryptoManagerException
{
return( new CryptoSTSEngineImpl( this, getMyPublicKey( password, reason ), getMyPrivateKey( password, reason )));
}
public byte[]
getPublicKey(
char[] password )
throws CryptoManagerException
{
return( keyToRawdata( getMyPublicKey( password, null )));
}
public byte[]
getPublicKey(
String reason )
throws CryptoManagerException
{
return( keyToRawdata( getMyPublicKey( null, reason )));
}
protected byte[]
getPublicKey(
char[] password,
String reason )
throws CryptoManagerException
{
return( keyToRawdata( getMyPublicKey( password, reason )));
}
public byte[]
getEncryptedPrivateKey(
char[] password )
throws CryptoManagerException
{
return( getEncryptedPrivateKey( password, null ));
}
public byte[]
getEncryptedPrivateKey(
String reason )
throws CryptoManagerException
{
return( getEncryptedPrivateKey( null, reason ));
}
protected byte[]
getEncryptedPrivateKey(
char[] password,
String reason )
throws CryptoManagerException
{
getMyPrivateKey( password, reason );
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -