从 JCE 前往充气城堡(Blowfish)

问题描述 投票:0回答:1

我需要将基于 JCE 的代码转换为基于 Bouncy Castle 的代码。我对 Bouncy Castle 完全陌生,找不到对该主题或具体问题的易于理解的介绍。这是基于 JCE 的类:

import java.security.NoSuchAlgorithmException;
import java.security.Security;

import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;

import com.sun.crypto.provider.SunJCE;

public class JCEBlowfishEncrypterDecrypter {
    
    private static final String ALGORITHM = "Blowfish";
    public static SecretKeySpec key;

    public static String crypt(String msg, String k) throws Exception {
        SecretKeySpec key = init(k);
        return Hex.byte2hex(intCrypt(msg, key));
    }

    public static String decrypt(String msg, String k) throws Exception {
        SecretKeySpec key = init(k);
        return new String(intDecrypt(Hex.hex2byte(msg), key));
    }

    private static byte[] intCrypt(String msg, SecretKeySpec key) throws Exception {
        Cipher cipher = Cipher.getInstance(ALGORITHM);
        cipher.init(1, key);
        return cipher.doFinal(msg.getBytes());
    }

    private static byte[] intDecrypt(byte encrypted[], SecretKeySpec key) throws Exception {
        Cipher cipher = Cipher.getInstance(ALGORITHM);
        cipher.init(2, key);
        return cipher.doFinal(encrypted);
    }

    private static SecretKeySpec init(String myKey) throws NoSuchAlgorithmException {
        SunJCE sunJce = new SunJCE();
        Security.addProvider(sunJce);
        byte raw[] = myKey.getBytes();
        return new SecretKeySpec(raw, ALGORITHM);
    }

}

...这是基于 Bouncy Castle 的课程:

import org.bouncycastle.crypto.BufferedBlockCipher;
import org.bouncycastle.crypto.engines.BlowfishEngine;
import org.bouncycastle.crypto.paddings.PKCS7Padding;
import org.bouncycastle.crypto.paddings.PaddedBufferedBlockCipher;
import org.bouncycastle.crypto.params.KeyParameter;

public class BouncyCastleBlowfishEncrypterDecrypter {

    public static String crypt(String msg, String key) throws Exception {
        PaddedBufferedBlockCipher cipher = new PaddedBufferedBlockCipher(new BlowfishEngine(), new PKCS7Padding());
        cipher.init(true, new KeyParameter(key.getBytes()));
        byte[] inputAsBytes = msg.getBytes();
        byte[] encryptedAsBytes = new byte[cipher.getOutputSize(inputAsBytes.length)];
        int numberOfBytesCopiedOnEncryptedAsBytes = cipher.processBytes(inputAsBytes, 0, inputAsBytes.length,
                encryptedAsBytes, 0);
        cipher.doFinal(encryptedAsBytes, numberOfBytesCopiedOnEncryptedAsBytes);
        return Hex.byte2hex(encryptedAsBytes);
    }

    public static String decrypt(String msg, String key) throws Exception {
        PaddedBufferedBlockCipher cipher = new PaddedBufferedBlockCipher(new BlowfishEngine(), new PKCS7Padding());
        cipher.init(false, new KeyParameter(key.getBytes()));
        byte[] outputConvertedAsBytes = Hex.hex2byte(msg);
        byte[] decryptedAsBytes = new byte[cipher.getOutputSize(outputConvertedAsBytes.length)];
        int numberOfBytesCopiedOnDecryptedAsBytes = cipher.processBytes(outputConvertedAsBytes, 0,
                outputConvertedAsBytes.length, decryptedAsBytes, 0);
        cipher.doFinal(decryptedAsBytes, numberOfBytesCopiedOnDecryptedAsBytes);
        return new String(decryptedAsBytes);
    }
}

...这是主要方法:


    public static void main(String[] args) throws Exception {
        String encrypted = JCEBlowfishEncrypterDecrypter.crypt("Test", "mmTSQOFzSL9xAwXGLMEe1Q==");
        String decrypted = JCEBlowfishEncrypterDecrypter.decrypt(encrypted, "mmTSQOFzSL9xAwXGLMEe1Q==");

        System.out.println("--------------");
        System.out.println("encrypted   -> " + encrypted);
        System.out.println("decrypted -> " + decrypted);
        
        encrypted = BouncyCastleBlowfishEncrypterDecrypter.crypt("Test",
                "mmTSQOFzSL9xAwXGLMEe1Q==");
        decrypted = BouncyCastleBlowfishEncrypterDecrypter.decrypt(encrypted,
                "mmTSQOFzSL9xAwXGLMEe1Q==");

        System.out.println("--------------");
        System.out.println("encrypted   -> " + encrypted);
        System.out.println("decrypted -> " + decrypted);
    }

...但是解密时我得到了一些奇怪的附加字符: 怎么了?

java cryptography bouncycastle jce
1个回答
0
投票

最后,根据对我问题的评论,我这样解决了它,但我不确定它是否经过优化并且完全正确:也许更有知识的人可以编辑我的答案,以便我可以接受它:

import org.bouncycastle.crypto.engines.BlowfishEngine;
import org.bouncycastle.crypto.paddings.PKCS7Padding;
import org.bouncycastle.crypto.paddings.PaddedBufferedBlockCipher;
import org.bouncycastle.crypto.params.KeyParameter;

public class BouncyCastleBlowfishEncrypterDecrypter implements EncrypterDecrypter {

    @Override
    public String crypt(String decryptedInput, String key) throws Exception {
        PaddedBufferedBlockCipher cipher = buildCipher(key, true);
        byte[] inputAsBytes = decryptedInput.getBytes();
        byte[] encryptedAsBytes = new byte[cipher.getOutputSize(inputAsBytes.length)];
        int numberOfBytesCopiedOnEncryptedAsBytes = cipher.processBytes(inputAsBytes, 0, inputAsBytes.length,
                encryptedAsBytes, 0);
        cipher.doFinal(encryptedAsBytes, numberOfBytesCopiedOnEncryptedAsBytes);
        return Hex.byte2hex(encryptedAsBytes);
    }

    @Override
    public String decrypt(String encryptedInput, String key) throws Exception {
        PaddedBufferedBlockCipher cipher = buildCipher(key, false);
        byte[] encryptedInputConvertedAsBytes = Hex.hex2byte(encryptedInput);
        byte[] decryptedBytesBuffer = new byte[buildCipher(key, false).getOutputSize(encryptedInputConvertedAsBytes.length)];
        int numberOfBytesCopiedOnDecryptedAsBytes = cipher.processBytes(encryptedInputConvertedAsBytes, 0,
                encryptedInputConvertedAsBytes.length, decryptedBytesBuffer, 0);
        byte[] dencryptedAsBytes = new byte[cipher.doFinal(decryptedBytesBuffer, numberOfBytesCopiedOnDecryptedAsBytes)];
        System.arraycopy(decryptedBytesBuffer, 0, dencryptedAsBytes, 0, dencryptedAsBytes.length);
        return new String(dencryptedAsBytes);
    }
    
    private static PaddedBufferedBlockCipher buildCipher(String key, boolean encrypt) {
        PaddedBufferedBlockCipher cipher = new PaddedBufferedBlockCipher(
            new BlowfishEngine(),
            new PKCS7Padding()
        );
        cipher.init(encrypt, new KeyParameter(key.getBytes()));
        return cipher;
    }

}
© www.soinside.com 2019 - 2024. All rights reserved.