Search in sources :

Example 1 with UnlockKeyException

use of com.itrus.portal.exception.UnlockKeyException in project portal by ixinportal.

the class UnlockKeyServiceImpl method generateUnlockCode.

/**
 * 根据输入的管理员pin码1和2,或者自动计算等生成对应的一个或者两个解锁码
 * @param adminPin
 * @param adminPin2
 * @param projectKeyInfo
 * @return
 * @throws UnlockKeyException
 * @throws NoSuchPaddingException
 * @throws NoSuchAlgorithmException
 * @throws Exception
 */
public KeyUnlock generateUnlockCode(String adminpin, String adminpin2, KeyUnlock keyunlock) throws UnlockKeyException, Exception {
    // 如果没有输入管理员PIN码,则检查预设的管理员PIN码
    // 根据解锁申请的序列号,查询序列号配置信息
    ProjectKeyInfo projectkeyinfo = cacheCustomer.findProjectByKey(keyunlock.getKeySn());
    if (null == projectkeyinfo) {
        throw new UnlockKeyException("无法识别的key序列号:" + keyunlock.getKeySn());
    }
    if (StringUtils.isBlank(adminpin)) {
        if (projectkeyinfo == null || projectkeyinfo.getAdminPinType() == null || projectkeyinfo.getAdminPinType().equals("null")) {
            return null;
        }
        if (projectkeyinfo.getAdminPinType().equals("fix")) {
            String unlockCipher = "AES";
            SecretKeySpec skeySpec = new SecretKeySpec(ProjectKeyInfoController.adminPinEncKey.substring(0, 16).getBytes(), unlockCipher);
            IvParameterSpec ivSpec = new IvParameterSpec(ProjectKeyInfoController.adminPinEncKey.substring(16, 32).getBytes());
            Cipher cipher = Cipher.getInstance(unlockCipher + "/CBC/PKCS5Padding");
            cipher.init(Cipher.DECRYPT_MODE, skeySpec, ivSpec, null);
            byte[] decadminpin = cipher.doFinal(Base64.decode(projectkeyinfo.getAdminPinValue().getBytes()));
            adminpin = new String(decadminpin);
        } else if (// 自动计算序列号
        projectkeyinfo.getAdminPinType().equals("autoht"))
            adminpin = HMACSHA1.getSoPinHT(keyunlock.getKeySn());
        else if (projectkeyinfo.getAdminPinType().equals("autoft"))
            adminpin = HMACSHA1.getSoPinFT(keyunlock.getKeySn());
        else if (projectkeyinfo.getAdminPinType().equals("autokoal"))
            adminpin = HMACSHA1.getSoPinKOAL(keyunlock.getKeySn());
    }
    // 再次判断,如果管理员PIN码为空,则要求重新输入
    if (adminpin == null || adminpin.length() == 0) {
        return null;
    }
    // 产生 encPrivateKeyKMC
    String unlockCipher = "AES";
    SecretKeySpec skeySpec = new SecretKeySpec(keyunlock.getReqCode().substring(0, 16).getBytes(), unlockCipher);
    IvParameterSpec ivSpec = new IvParameterSpec(keyunlock.getReqCode().substring(16).getBytes());
    Cipher cipher = Cipher.getInstance(unlockCipher + "/CBC/PKCS5Padding");
    cipher.init(Cipher.ENCRYPT_MODE, skeySpec, ivSpec, null);
    byte[] encadminpin = cipher.doFinal(adminpin.getBytes());
    String sencadminpin = new String(Base64.encode(encadminpin));
    keyunlock.setRepCode(sencadminpin);
    // 产生第二个解锁码
    if (StringUtils.isBlank(adminpin2)) {
        if (null != projectkeyinfo && null != projectkeyinfo.getAdminPinType2() && !projectkeyinfo.getAdminPinType2().toLowerCase().equals("null")) {
            // 固定值序列号
            if (projectkeyinfo.getAdminPinType2().equals("fix")) {
                String unlockCipher2 = "AES";
                SecretKeySpec skeySpec2 = new SecretKeySpec(ProjectKeyInfoController.adminPinEncKey.substring(0, 16).getBytes(), unlockCipher2);
                IvParameterSpec ivSpec2 = new IvParameterSpec(ProjectKeyInfoController.adminPinEncKey.substring(16, 32).getBytes());
                Cipher cipher2 = Cipher.getInstance(unlockCipher2 + "/CBC/PKCS5Padding");
                cipher2.init(Cipher.DECRYPT_MODE, skeySpec2, ivSpec2, null);
                byte[] decadminpin2 = cipher2.doFinal(Base64.decode(projectkeyinfo.getAdminPinValue2().getBytes()));
                adminpin2 = new String(decadminpin2);
            } else // 自动计算序列号
            if (projectkeyinfo.getAdminPinType2().equals("autoht"))
                adminpin2 = HMACSHA1.getSoPinHT(keyunlock.getKeySn());
            else if (projectkeyinfo.getAdminPinType2().equals("autoft"))
                adminpin2 = HMACSHA1.getSoPinFT(keyunlock.getKeySn());
            else if (projectkeyinfo.getAdminPinType2().equals("autokoal"))
                adminpin2 = HMACSHA1.getSoPinKOAL(keyunlock.getKeySn());
        }
    }
    // 再次判断,如果管理员PIN2码为空,则返回输入的第一个管理员pin码即可
    if (StringUtils.isBlank(adminpin2)) {
        keyunlock.setApproveTime(new Date());
        keyunlock.setStatus("APPROVE");
        sqlSession.update("com.itrus.portal.db.KeyUnlockMapper.updateByPrimaryKey", keyunlock);
        return keyunlock;
    }
    // 产生 encPrivateKeyKMC
    String unlockCipher2 = "AES";
    SecretKeySpec skeySpec2 = new SecretKeySpec(keyunlock.getReqCode().substring(0, 16).getBytes(), unlockCipher2);
    IvParameterSpec ivSpec2 = new IvParameterSpec(keyunlock.getReqCode().substring(16).getBytes());
    Cipher cipher2 = Cipher.getInstance(unlockCipher2 + "/CBC/PKCS5Padding");
    cipher2.init(Cipher.ENCRYPT_MODE, skeySpec2, ivSpec2, null);
    byte[] encadminpin2 = cipher.doFinal(adminpin2.getBytes());
    String sencadminpin2 = new String(Base64.encode(encadminpin2));
    keyunlock.setRepCode2(sencadminpin2);
    keyunlock.setApproveTime(new Date());
    keyunlock.setStatus("APPROVE");
    sqlSession.update("com.itrus.portal.db.KeyUnlockMapper.updateByPrimaryKey", keyunlock);
    return keyunlock;
}
Also used : UnlockKeyException(com.itrus.portal.exception.UnlockKeyException) ProjectKeyInfo(com.itrus.portal.db.ProjectKeyInfo) SecretKeySpec(javax.crypto.spec.SecretKeySpec) IvParameterSpec(javax.crypto.spec.IvParameterSpec) Cipher(javax.crypto.Cipher) Date(java.util.Date)

Aggregations

ProjectKeyInfo (com.itrus.portal.db.ProjectKeyInfo)1 UnlockKeyException (com.itrus.portal.exception.UnlockKeyException)1 Date (java.util.Date)1 Cipher (javax.crypto.Cipher)1 IvParameterSpec (javax.crypto.spec.IvParameterSpec)1 SecretKeySpec (javax.crypto.spec.SecretKeySpec)1