cryptohandlerecc.java
来自「这是一个基于java编写的torrent的P2P源码」· Java 代码 · 共 746 行 · 第 1/2 页
JAVA
746 行
byte[] res = COConfigurationManager.getByteParameter( CONFIG_PREFIX + "privatekey", null );
if ( res == null ){
throw( new CryptoManagerException( "Private key unavailable" ));
}
return( res );
}
public synchronized void
recoverKeys(
byte[] public_key,
byte[] encrypted_private_key )
throws CryptoManagerException
{
use_method_private_key = null;
use_method_public_key = null;
COConfigurationManager.setParameter( CONFIG_PREFIX + "publickey", public_key );
COConfigurationManager.setParameter( CONFIG_PREFIX + "privatekey", encrypted_private_key );
COConfigurationManager.save();
}
public synchronized void
resetKeys(
char[] password )
throws CryptoManagerException
{
use_method_private_key = null;
use_method_public_key = null;
COConfigurationManager.removeParameter( CONFIG_PREFIX + "publickey" );
COConfigurationManager.removeParameter( CONFIG_PREFIX + "privatekey" );
COConfigurationManager.save();
createAndStoreKeys( password, "" );
}
public synchronized void
changePassword(
char[] old_password,
char[] new_password )
throws CryptoManagerException
{
// ensure old password is correct
use_method_private_key = null;
use_method_public_key = null;
getMyPrivateKey( old_password, "" );
getMyPublicKey( old_password, "" );
storeKeys( new_password );
}
protected synchronized PrivateKey
getMyPrivateKey(
char[] password,
String reason )
throws CryptoManagerException
{
if ( use_method_private_key != null ){
int timeout_secs = getUnlockTimeoutSeconds();
if ( timeout_secs > 0 ){
if ( SystemTime.getCurrentTime() - last_unlock_time >= timeout_secs * 1000 ){
use_method_private_key = null;
}
}
}
if ( use_method_private_key == null ){
byte[] encoded = COConfigurationManager.getByteParameter( CONFIG_PREFIX + "privatekey", null );
if ( encoded == null ){
createAndStoreKeys( password, reason );
}else{
if ( password == null ){
password = manager.getPassword(
CryptoManager.HANDLER_ECC,
CryptoManagerPasswordHandler.ACTION_DECRYPT,
reason );
}
use_method_private_key = rawdataToPrivkey( manager.decryptWithPBE( encoded, password ));
last_unlock_time = SystemTime.getCurrentTime();
boolean ok = false;
try{
byte[] test_data = "test".getBytes();
ok = verify( keyToRawdata( getMyPublicKey( password, reason )), test_data, sign( test_data, password, reason ));
if ( !ok ){
throw( new CryptoManagerPasswordException());
}
}catch( CryptoManagerException e ){
throw( e );
}catch( Throwable e ){
throw( new CryptoManagerException( "Password incorrect", e ));
}finally{
if ( !ok ){
use_method_private_key = null;
}
}
}
}
if ( use_method_private_key == null ){
throw( new CryptoManagerException( "Failed to get private key" ));
}
return( use_method_private_key );
}
protected synchronized PublicKey
getMyPublicKey(
char[] password,
String reason )
throws CryptoManagerException
{
if ( use_method_public_key == null ){
byte[] key_bytes = COConfigurationManager.getByteParameter( CONFIG_PREFIX + "publickey", null );
if ( key_bytes == null ){
createAndStoreKeys( password, reason );
}else{
use_method_public_key = rawdataToPubkey( key_bytes );
}
}
if ( use_method_public_key == null ){
throw( new CryptoManagerException( "Failed to get public key" ));
}
return( use_method_public_key );
}
protected void
createAndStoreKeys(
char[] password,
String reason )
throws CryptoManagerException
{
if ( password == null ){
password = manager.getPassword(
CryptoManager.HANDLER_ECC,
CryptoManagerPasswordHandler.ACTION_ENCRYPT,
reason );
}
KeyPair keys = createKeys();
use_method_public_key = keys.getPublic();
use_method_private_key = keys.getPrivate();
storeKeys( password );
}
protected void
storeKeys(
char[] password )
throws CryptoManagerException
{
COConfigurationManager.setParameter( CONFIG_PREFIX + "publickey", keyToRawdata( use_method_public_key ));
byte[] priv_raw = keyToRawdata( use_method_private_key );
byte[] priv_enc = manager.encryptWithPBE( priv_raw, password );
COConfigurationManager.setParameter( CONFIG_PREFIX + "privatekey", priv_enc );
COConfigurationManager.save();
}
protected KeyPair
createKeys()
throws CryptoManagerException
{
try
{
KeyPairGenerator keyGen = KeyPairGenerator.getInstance("ECDSA", "BC");
keyGen.initialize(ECCparam);
return keyGen.genKeyPair();
}catch(Throwable e){
throw( new CryptoManagerException( "Failed to create keys", e ));
}
}
public Signature
getSignature(
Key key )
throws CryptoManagerException
{
try
{
Signature ECCsig = Signature.getInstance("SHA1withECDSA", "BC");
if( key instanceof ECPrivateKey ){
ECCsig.initSign((ECPrivateKey)key);
}else if( key instanceof ECPublicKey ){
ECCsig.initVerify((ECPublicKey)key);
}else{
throw new CryptoManagerException("Invalid Key Type, ECC keys required");
}
return ECCsig;
}catch( CryptoManagerException e ){
throw( e );
}catch( Throwable e ){
throw( new CryptoManagerException( "Failed to create Signature", e ));
}
}
protected byte[]
keyToRawdata(
PrivateKey privkey )
throws CryptoManagerException
{
if(!(privkey instanceof ECPrivateKey)){
throw( new CryptoManagerException( "Invalid private key" ));
}
return ((ECPrivateKey)privkey).getD().toByteArray();
}
protected PrivateKey
rawdataToPrivkey(
byte[] input )
throws CryptoManagerException
{
BigInteger D = new BigInteger(input);
KeySpec keyspec = new ECPrivateKeySpec(D,(ECParameterSpec)ECCparam);
PrivateKey privkey = null;
try{
privkey = KeyFactory.getInstance("ECDSA","BC").generatePrivate(keyspec);
return privkey;
}catch( Throwable e ){
throw( new CryptoManagerException( "Failed to decode private key" ));
}
}
protected byte[]
keyToRawdata(
PublicKey pubkey )
throws CryptoManagerException
{
if(!(pubkey instanceof ECPublicKey)){
throw( new CryptoManagerException( "Invalid public key" ));
}
return ((ECPublicKey)pubkey).getQ().getEncoded();
}
protected PublicKey
rawdataToPubkey(
byte[] input )
throws CryptoManagerException
{
ECPoint W = ECCparam.getCurve().decodePoint(input);
KeySpec keyspec = new ECPublicKeySpec(W,(ECParameterSpec)ECCparam);
try{
return KeyFactory.getInstance("ECDSA", "BC").generatePublic(keyspec);
}catch (Throwable e){
throw( new CryptoManagerException( "Failed to decode private key" ));
}
}
class InternalECIES
extends JCEIESCipher.ECIES
{
// we use this class to obtain compatability with BC
public void
internalEngineInit(
int opmode,
Key key,
AlgorithmParameterSpec params,
SecureRandom random )
throws InvalidKeyException, InvalidAlgorithmParameterException
{
engineInit(opmode, key, params, random);
}
protected byte[]
internalEngineDoFinal(
byte[] input,
int inputOffset,
int inputLen )
throws IllegalBlockSizeException, BadPaddingException
{
return engineDoFinal(input, inputOffset, inputLen);
}
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?