目前我正在使用块加密 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");
}