一步一步写算法(之)

import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import java.security.spec.AlgorithmParameterSpec;


public class BlowfishTest {

    // 密钥算法名称
    public static final String BLOWFISH = "Blowfish";
    // 转换模式
    public static final String TRANSFORMATION = "Blowfish/CBC/PKCS5Padding";

    public static byte[] encrypt(String key, byte[] text, String initializationVector)throws Exception {
    	
    	// 根据给定的字节数组构造一个密钥  Blowfish-与给定的密钥内容相关联的密钥算法的名称
    	SecretKeySpec sksSpec = new SecretKeySpec(key.getBytes(), BLOWFISH);
    	// 使用 initializationVector 中的字节作为 IV 来构造一个 IvParameterSpec 对象
    	AlgorithmParameterSpec iv = new IvParameterSpec(initializationVector.getBytes());

        // 返回实现指定转换的 Cipher 对象
        Cipher cipher = Cipher.getInstance(TRANSFORMATION);
        // 用密钥和随机源初始化此 Cipher
        cipher.init(Cipher.ENCRYPT_MODE, sksSpec, iv);

        // 加密文本
        byte[] encrypted = cipher.doFinal(text);

        return encrypted;
    }

    public static byte[] decrypt(String key, byte[] text, String initializationVector)throws Exception {
    	
        SecretKeySpec skeSpect = new SecretKeySpec(key.getBytes(), BLOWFISH);
        AlgorithmParameterSpec iv = new IvParameterSpec(initializationVector.getBytes());

        Cipher cipher = Cipher.getInstance(TRANSFORMATION);
        cipher.init(Cipher.DECRYPT_MODE, skeSpect, iv);

        byte[] decrypted = cipher.doFinal(text);

        return decrypted;
    }
}

 


 

import re
import configparser

def MmlCmdToMmlHelp(mmlCmd):
    mmlHelp = mmlCmd
    mmlHelp = mmlHelp.lower().replace(' ','_')

    return mmlHelp + '.xml'

def MmlHelpToMmlCmd(mmlHelp):
    mmlCmd = mmlHelp
    mmlCmd = mmlCmd.replace('.xml','')
    mmlCmd = mmlCmd.upper().replace('_',' ')
    return mmlCmd

def getMmlFromMacroByType(macro_name, mml_type):
    mmlList =[]
    config = configparser.ConfigParser()
    config.read('macro.ini')
    for section in config.sections():
        if 'isnotshow' in config.options(section):
            Type = int(config.get(section,"isnotshow"))
        else:
            Type = 0
        if(Type == mml_type):
            mmlList.append(section)
    
    return mmlList

def getMmlFromDitemap(ditemap_name):
    mmlList =[]
    file= open(ditemap_name)
    try:
        fileContext = file.read()
    finally:
        file.close()   
    for mml in re.findall(r'<topic name = "mml/([\s\S]*?)"></topic>',fileContext,re.M):
        mmlList.append(MmlHelpToMmlCmd(mml))
        
    return mmlList

def getNeedAddFromDitemap(ditemapList, macroList):
    mmlList = []
    for mml in macroList:
        if mml not in ditemapList:
            mmlList.append(MmlCmdToMmlHelp(mml))
    return mmlList

def getNeedRmvFromDitemap(ditemapList, macroList):
    mmlList = []
    for mml in ditemapList:
        if mml not in macroList:
            mmlList.append(MmlCmdToMmlHelp(mml))
    return mmlList

if __name__ == '__main__':
    ditemap_name = "mml.ditemap"
    macro_name = 'macro.ini'
    mml_type = 3
    MacroMmlList = getMmlFromMacroByType(macro_name, mml_type)
    DitemapMmlList = getMmlFromDitemap(ditemap_name)
    needAddList = getNeedAddFromDitemap(DitemapMmlList,MacroMmlList)
    needRmvList = getNeedRmvFromDitemap(DitemapMmlList,MacroMmlList)
    print ('MacroMmlList = ',MacroMmlList)
    print ('DitemapMmlList = ',DitemapMmlList)
    print ('needAddList = ',needAddList)
    print ('needRmvList = ',needRmvList)
    print ("end__")  


add...

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值