在cbc模式下输入另一个加密

时间:2013-05-07 08:41:59

标签: java encryption des

当前我使用块加密DES来做CBC,我想为用户提供一个选项,以便使用其他分组密码,例如Tiny Encryption。微加密的输出与我使用的DES类不同,因此我无法添加它。任何人都可以在这里给一点帮助?

private static int delta = 0x9E3779B9; /* a key schedule constant */
public void code(int[] v, int[] k) { 
int y=v[0], z=v[1], sum=0, n=32; 
int k0=k[0], k1=k[1], k2=k[2], k3=k[3]; /* cache key */
while (n-- > 0) { 
 sum += delta; 
 y += ((z << 4) + k0) ^ (z + sum) ^ ((z >>> 5) + k1); 
 z += ((y << 4) + k2) ^ (y + sum) ^ ((y >>> 5) + k3); 
 } 
 v[0]=y; v[1]=z; 
 }   

``````````````````````````````````````````````` ````````````````````````````````````````````

private static void cbc(byte [] raw_key, PrintStream ofstream) {
    byte [] cbc_ciphertext = null;
    byte [] IV = CryptoUtil.getIV();
    try {
        SecretKey key = new SecretKeySpec(raw_key, "DES");
        Cipher c = Cipher.getInstance ("DES/ECB/NoPadding");
        c.init(Cipher.ENCRYPT_MODE, key);

        /* increment in 8-byte blocks through plaintext */
        for (int i = 0; i < plaintext.length; i += 8) {
            byte [] block = CryptoUtil.getBlock(plaintext, i);
            if (i == 0)
                cbc_ciphertext = c.doFinal(CryptoUtil.xor (IV, block));
            else {
                ciphertext = CryptoUtil.append(ciphertext, cbc_ciphertext);
                cbc_ciphertext = 
                    c.doFinal(CryptoUtil.xor(cbc_ciphertext, block));
            }
        }
    }
    catch (Exception e) {
        e.printStackTrace();
        System.exit(1);
    }
    ciphertext = CryptoUtil.append (ciphertext, cbc_ciphertext);
    ciphertext = CryptoUtil.append (IV, ciphertext); 
    // ciphertext now has chained encrypted cipher blocks
    ofstream.print(CryptoUtil.encode(ciphertext));
    System.out.println("encrypted ciphertext (DES/CBC) to " +
                "ciphertext.txt");
}

0 个答案:

没有答案
相关问题