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

📄 snmpusmkeychange.java

📁 this api for java to do the network managerment of system,i like it ,so share for a
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
        int[] userIndex=null;        if(ownKeyChange)        {            userIndex = surg.stringToIntegerArray(userName);        }        else        {            userIndex = surg.stringToIntegerArray(oldUserName);        }                        String userNameOID = surg.intArrayToString(userIndex);                // Initialize the random value based on the protocol used.        byte[] random;        if(authProtocol == USMUserEntry.MD5_AUTH)        {            random = new byte[AUTH_MD5_LEN];        }        else        {            random = new byte[AUTH_SHA_LEN];        }        byte[] engID2 = null;        try        {            engID2 = engID.getBytes(ENC);        }        catch(Exception ex)        {            engID2 = engID.getBytes();          }        USMUserTable userTable = (USMUserTable)api.getSecurityProvider().                                    getTable(USM_SECURITY_MODEL);        USMUserEntry entry = userTable.getEntry(userName.getBytes(),                                                engID2);                byte[] oldKey=null;        if(ownKeyChange)        {            oldKey = entry.getAuthKey();        }        else        {            oldKey = USMUtils.password_to_key(                            oldAuthProtocol,                            oldAuthPassword.getBytes(),                            oldAuthPassword.getBytes().length,                            engineID);        }        // Generate the keyChange value based on the secret        // authKey of the  user and the new secret key        // to be used for the user. Let us call this akcValue.            String akcValue = surg.getKeyChangeValue(                                engID,                                authProtocol,                                newAuthPassword,                                oldKey,                                random,                                false);            if(authProtocol == USMUserEntry.MD5_AUTH)        {            try            {                random = akcValue.substring(0,AUTH_MD5_LEN).getBytes(ENC);            }            catch(Exception e)            {                random = akcValue.substring(0,AUTH_MD5_LEN).getBytes();            }        }        else        {            try            {                random = akcValue.substring(0,AUTH_SHA_LEN).getBytes(ENC);            }            catch(Exception e)            {                random = akcValue.substring(0,AUTH_SHA_LEN).getBytes();            }        }        String keyChangeOID=null;        if(ownKeyChange)        {            keyChangeOID = USM_ENTRY + "." + AUTH_OWN_KEY_CHANGE_COL + "."                 + firstindex.length + engIDOID + "."                 + userIndex.length + userNameOID;        }        else        {            keyChangeOID = USM_ENTRY + "." + AUTH_KEY_CHANGE_COL + "."                 + firstindex.length + engIDOID + "."                 + userIndex.length + userNameOID;        }        String randomOID = USM_ENTRY + "." + USM_PUBLIC_COL + "." +                + firstindex.length + engIDOID + "." +                 userIndex.length + userNameOID;                        SnmpOID setOID1 = new SnmpOID(SPIN_LOCK_OID);        surg.addvarbind(pdu, setOID1,"INTEGER",                            new Integer(spinLock).toString());                SnmpOID setOID2 = new SnmpOID(keyChangeOID); //check this up        surg.addvarbind(pdu, setOID2,"STRING",akcValue);        SnmpOID setOID3 = new SnmpOID(randomOID);        String randomString;        try        {            randomString = new String(random,ENC);        }        catch(Exception e)        {            randomString = new String(random);        }        surg.addvarbind(pdu, setOID3,"STRING", randomString);        System.out.println("Sending a request to set the authKeyChange\n");        pdu.setCommand( SnmpAPI.SET_REQ_MSG );        try        {            // Send PDU and receive response PDU            System.out.println("Sending Request for KeyChange");            pdu = session.syncSend(pdu);        }        catch (SnmpException e)        {            System.err.println("Sending PDU"+e.getMessage());            System.exit(1);        }            if (pdu == null)        {            // timeout            System.out.println("Request timed out to: " + opt.remArgs[0] );            System.exit(1);        }        System.out.println("Response PDU for keyChange  received from " +                            pdu.getProtocolOptions().getSessionId());        if (pdu.getErrstat() != 0)        {            System.out.println("KeyChange SET request returned error "                                    + "User NOT Successfully cloned");            System.err.println(pdu.getError());            System.exit(1);        }        else        {            // print the response pdu varbinds            System.out.println(pdu.printVarBinds());        }        // Since we are reusing the PDU, we will remove the varbinds        // and set the reqid to 0.        surg.removeAllVarBinds(pdu);        //pdu.setReqid(0);        // This is to set the new AuthKey on our side for receiving         //the usmUserPublic        // value after it has been set.        if(ownKeyChange)        {            entry.setAuthPassword(newAuthPassword.getBytes());            byte[] engID1 = null;            try            {                engID1 = engID.getBytes(ENC);            }            catch(Exception ex)            {                engID1 = engID.getBytes();              }                       byte[] newKey = USMUtils.password_to_key(authProtocol,                            newAuthPassword.getBytes(),                            newAuthPassword.getBytes().length,                            engID1);            entry.setAuthKey(newKey);        }        // Get the usmUserPublic value        pdu.setCommand( SnmpAPI.GET_REQ_MSG );                SnmpOID oid = new SnmpOID(randomOID);        if (oid.toValue() == null)         {            System.err.println("Invalid OID argument: " + randomOID);        }        else        {            pdu.addNull(oid);        }                 try        {            // Send PDU and receive response PDU            pdu = session.syncSend(pdu);        }        catch (SnmpException e)        {            System.err.println("Sending PDU "+e.getMessage());            System.exit(1);        }            if (pdu == null)        {            // timeout            System.out.println("Request timed out to: " + opt.remArgs[0] );            System.exit(1);        }        // print and exit        System.out.println("Response PDU for usmUserPublic  received from " +                            pdu.getProtocolOptions().getSessionId());                    // Check for error in response        if (pdu.getErrstat() != 0)        {            System.out.println("usmUserPublic GET request returned error "                                    + "User NOT Successfully cloned");            System.err.println(pdu.getError());            System.exit(1);        }        else        {            // print the response pdu varbinds            System.out.println(pdu.printVarBinds());        }        String userPublic = (pdu.getVariable(0)).toString();        String tempRandom;        try        {            tempRandom = new String(random,ENC);        }        catch(Exception e)        {            tempRandom = new String(random);        }        if(userPublic.equals(tempRandom))        {            System.out.println("usmUserPulic value is set appropriately\n");        }        else        {            System.out.println("usmUserPulic value is NOT set appropriately");            System.out.println("User NOT Successfully cloned");            System.exit(1);        }        // Since we are reusing the PDU, we will remove the varbinds        // and set the reqid to 0.        surg.removeAllVarBinds(pdu);        //pdu.setReqid(0);        // Start the privKeyChange        if(privPassword!=null && privPassword.length()>0 && newPrivPassword!=null && newPrivPassword.length()>0)        {            newPrivPassword = values[NEW_PRIV_PASSWORD];            SnmpPDU pdu1 = new SnmpPDU();            pdu1.setUserName(userName.getBytes());            if(contextName!=null)            {                pdu1.setContextName(contextName.getBytes());            }            if(contextID!=null)            {                pdu1.setContextID(contextID.getBytes());            }            // Step 1. Retrive the USMUserSpinLock            spinLock = surg.sendSpinLockRequest(pdu1,session);            if(spinLock < 0)            {                System.out.println("Error in retriving SnmpLock");                System.exit(1);            }            System.out.println("Spin lock value retrived successfully\n");            // Since we are reusing the PDU, we will remove the varbinds            // and set the reqid to 0.            surg.removeAllVarBinds(pdu1);            // Initialize the random value based on the protocol used.            byte[] priv_random = new byte[16]; // always 16 for Priv            byte[] oldPrivKey=null;            if(ownKeyChange)            {                oldPrivKey = entry.getPrivKey();            }            else            {                System.out.println("Got here 518");                oldPrivKey = USMUtils.password_to_key(                                oldAuthProtocol,                                oldPrivPassword.getBytes(),                                oldPrivPassword.getBytes().length,                                engineID);            }            String pkcValue=null;            // Generate the keyChange value based on the secret            // privKey of the user and the new secret key            // to be used for the user. Let us call this pkcValue.            pkcValue = surg.getKeyChangeValue(                            engID,                            USMUserEntry.MD5_AUTH,                            newPrivPassword,                            oldPrivKey,                            random,                            true);            try            {                priv_random = pkcValue.substring(0,16).getBytes(ENC);            }            catch(Exception e)            {                priv_random = pkcValue.substring(0,16).getBytes();            }            if(ownKeyChange)            {                keyChangeOID = USM_ENTRY + "." + PRIV_OWN_KEY_CHANGE_COL + "."                         + firstindex.length + engIDOID + "."                         + userIndex.length + userNameOID;            }            else            {                System.out.println("GOT Here 540 ");                 keyChangeOID = USM_ENTRY + "." + PRIV_KEY_CHANGE_COL + "."                     + firstindex.length + engIDOID + "."                     + userIndex.length + userNameOID;            }            randomOID = USM_ENTRY + "." + USM_PUBLIC_COL + "." +                    + firstindex.length + engIDOID + "." +                     userIndex.length + userNameOID;            setOID1 = new SnmpOID(SPIN_LOCK_OID);            surg.addvarbind(pdu1, setOID1,"INTEGER",                            new Integer(spinLock).toString());            setOID2 = new SnmpOID(keyChangeOID); //check this up            surg.addvarbind(pdu1, setOID2,"STRING",pkcValue);            setOID3 = new SnmpOID(randomOID);            try            {                randomString = new String(priv_random,ENC);            }            catch(Exception e)            {                randomString = new String(priv_random);            }            surg.addvarbind(pdu1, setOID3,"STRING", randomString);

⌨️ 快捷键说明

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