VBA CBC 256和Java Bouncy castle加密

VBA CBC 256和Java Bouncy castle加密,vba,java-8,bouncycastle,rijndael,Vba,Java 8,Bouncycastle,Rijndael,我正在尝试解密Rijndael VBA代码中的加密字符串。 Java8代码 public static void Decrypt() throws Exception{ String mydata = "3m/WeZ1cAUEqexeH64gPehkMdQSRvx7K9TKhtpUfEg=="; byte[] encryptedBytes = Base64.getDecoder().decode(mydata); byte[]

我正在尝试解密Rijndael VBA代码中的加密字符串。 Java8代码

public static void Decrypt() throws Exception{
        String mydata = "3m/WeZ1cAUEqexeH64gPehkMdQSRvx7K9TKhtpUfEg==";

        byte[] encryptedBytes = Base64.getDecoder().decode(mydata);
        byte[] key = Base64.getDecoder().decode("VGhpcnR5VHdvQnl0ZXMzJFRoaXJ0eVR3b0J5dGVzMyQ=");          
        byte[] iv = Base64.getDecoder().decode("MyRUaHJlZVR3b0J5dGVzMzMkVGhyZWVUd29CeXRlczM=");
        
        PaddedBufferedBlockCipher bufferedBlock = new PaddedBufferedBlockCipher(new CBCBlockCipher(new RijndaelEngine(256)), new PKCS7Padding());
        CipherParameters keyAndIV = new ParametersWithIV(new KeyParameter(key), iv);
        bufferedBlock.init(false, keyAndIV);        
        
        byte[] decryptedBytes = new byte[bufferedBlock.getOutputSize(encryptedBytes.length)];
        int processed = bufferedBlock.processBytes(encryptedBytes, 0, encryptedBytes.length, decryptedBytes, 0);
        processed += bufferedBlock.doFinal(decryptedBytes, processed);
                
        System.out.println(new String(decryptedBytes, 0, processed, StandardCharsets.UTF_8));       
    }
上面的代码在第行给出了一个错误“解密中最后一个块未完成”

这是VBA代码加密:

Function Encrypt(plaintext, aesKey)
    Dim cipherBytes, aesKeyBytes, ivKeyBytes, plainBytes() As Byte
    
    Dim utf8, AES, aesEnc, cipherMode As Object
    Dim aesIV() As Byte
        
    Set AES = CreateObject("System.Security.Cryptography.RijndaelManaged")
    Set utf8 = CreateObject("System.Text.UTF8Encoding")
   
    AES.KeySize = 256
    AES.BlockSize = 256
    'CipherMode.CBC
    AES.Mode = 1
    'PaddingMode.PKCS7
    AES.Padding = 2
    AES.Key = utf8.GetBytes_4("ThirtyTwoBytes3$ThirtyTwoBytes3$")
    AES.IV = utf8.GetBytes_4("3$ThreeTwoBytes33$ThreeTwoBytes3")
    plainBytes = utf8.GetBytes_4(plaintext)
    'plainBytes = B64Decode(plaintext)
    'Set aesEnc = AES.CreateEncryptor_2((aesKeyBytes), (ivKeyBytes))
    cipherBytes = AES.CreateEncryptor().TransformFinalBlock((plainBytes), 0, UBound(plainBytes))
    Encrypt = B64Encode(cipherBytes)
End Function

我正在尝试对VBA发送的数据进行加密、解密和使用。请帮助我更正Java代码以匹配VBA代码

问题是在
B64Encode()
encrypt()
中,字节数组的长度的指定不正确(请参阅代码中的修复1、修复2)
UBound()
返回最大索引,因此长度为
UBound()+1
。或者也可以使用

以下VBA代码加密纯文本:

功能最小值(a、b)
最小=a
如果bUBound(字节)
长度=最小值(块大小,LenB(字节)-偏移量)'LenB(字节)-->UBound(字节)+1修复1
b64Block=b64Enc.TransformFinalBlock((字节)、偏移量、长度)
result=result&utf8.GetString((b64Block))
下一个
B64Encode=结果
端函数
函数加密(明文)
设置AES=CreateObject(“System.Security.Cryptography.RijndaelManaged”)
设置utf8=CreateObject(“System.Text.UTF8Encoding”)
AES.KeySize=256
AES.BlockSize=256
AES.Mode=1'CipherMode.CBC
AES.Padding=2'PaddingMode.PKCS7
AES.Key=utf8.GetBytes_4(“ThirtyTwoBytes3$ThirtyTwoBytes3$”)
AES.IV=utf8.GetBytes_4(“3$ThreeTwoBytes33$ThreeTwoBytes3”)
明文=utf8.GetBytes_4(明文)
cipherBytes=AES.CreateEncryptor().TransformFinalBlock((纯字节),0,LenB(纯字节))'LenB(纯字节)-->UBound(纯字节)+1修复2
encrypt=B64编码(加密字节)
端函数
子加密数据()
Debug.Print encrypt(“快速棕色狐狸跳过懒狗”)'2WVYo0DvgbKXBUn+/eI/ytvujs0zyxen9lu5ytxhjrwpdrn5y4huwpjamsg47gtg4dc2abl5eyivdg1n91t5a==
端接头
可以使用以下Java代码(使用BouncyCastle)对其进行解密:

public static void Decrypt()引发异常{
字符串mydata=“2WVYo0DvgbKXBUn+/eI/ytvujs0zyxen9lu5ytxhjrwpdrn5y4huwpjamsg47gtg4dc2abl5eyivdg1n91t5a=”;
byte[]encryptedBytes=Base64.getDecoder().decode(mydata);
byte[]key=“ThirtyTwoBytes3$ThirtyTwoBytes3$”.getBytes(StandardCharsets.UTF_8);
byte[]iv=“3$ThreeTwoBytes33$ThreeTwoBytes3.getBytes(StandardCharsets.UTF_8);
//byte[]key=Base64.getDecoder().decode(“VGhpcnR5VHdvQnl0ZXMzJFRoaXJ0eVR3b0J5dGVzMyQ=”);//也适用于Base64编码的key和IV
//字节[]iv=Base64.getDecoder().decode(“myruahjlzvr3b0j5dgvzmkvghyzwvud29cexrlczm=”);
PaddedBufferedBlockCipher bufferedBlock=新的PaddedBufferedBlockCipher(新的CBCBlockCipher(新的RijndaLengine(256)),新的PKCS7Padding());
CipherParameters keyAndIV=新参数Swithiv(新的KeyParameters(key),iv);
bufferedBlock.init(false,keyAndIV);
byte[]decryptedBytes=新字节[bufferedBlock.getOutputSize(encryptedBytes.length)];
int processed=bufferedBlock.processBytes(encryptedBytes,0,encryptedBytes.length,decryptedBytes,0);
已处理+=bufferedBlock.doFinal(decryptedBytes,已处理);
System.out.println(新字符串(decryptedBytes,0,processed,StandardCharsets.UTF_8));
}

请注意,代码使用的Rijndael块大小为256字节,因此没有AES。AES是Rijndael的子集,块大小为128字节。应用AES更有意义,因为这是标准(Rijndael不是)。

您的密钥和iv不是Base64编码的,因此任何“解码”它们的尝试都会失败。第二:您应该更改行“byte[]encryptedBytes=Base64.getDecoder().decode(mydata.getBytes(StandardCharsets.UTF_8));”到'byte[]encryptedBytes=Base64.getDecoder().decode(mydata);'在VBA代码中,您似乎使用CBC()作为模式,使用PKCS7()作为填充(这两个都是
RijndaelManaged
的默认设置)。在Java代码中,应用了CBC和零填充,即填充不一致。您还使用了256位的块大小。因为这在两种代码中都是一致的,所以这是可行的。然而,它不再是AES。是Rijndael的一个子集,仅应用一个块大小,即128位。@MichaelFehr,谢谢,更新了代码,现在我得到了“解密中的最后一个块未完成”@Topaco,谢谢,我将代码更新为PKCS7填充和上述问题。我还有其他运行时错误,但我的java代码现在是否与VBA一致?非常感谢,非常好用。再次感谢您花时间回复。对于VBA和Java,您是否有128字节AES的示例代码或参考?谢谢你advance@Pat-代码基本相同。对于AES,您只需在VBA代码中设置
AES.BlockSize=128
。在Java代码中,必须使用新的RijndaelEngine(128)。此外,对于AES,只需应用16字节IV,因为IV的大小始终与块大小相对应。再次感谢,我也会尝试。你们太棒了,你们让我开心了,这是一个长期存在的问题,现在已经解决了。
Function Encrypt(plaintext, aesKey)
    Dim cipherBytes, aesKeyBytes, ivKeyBytes, plainBytes() As Byte
    
    Dim utf8, AES, aesEnc, cipherMode As Object
    Dim aesIV() As Byte
        
    Set AES = CreateObject("System.Security.Cryptography.RijndaelManaged")
    Set utf8 = CreateObject("System.Text.UTF8Encoding")
   
    AES.KeySize = 256
    AES.BlockSize = 256
    'CipherMode.CBC
    AES.Mode = 1
    'PaddingMode.PKCS7
    AES.Padding = 2
    AES.Key = utf8.GetBytes_4("ThirtyTwoBytes3$ThirtyTwoBytes3$")
    AES.IV = utf8.GetBytes_4("3$ThreeTwoBytes33$ThreeTwoBytes3")
    plainBytes = utf8.GetBytes_4(plaintext)
    'plainBytes = B64Decode(plaintext)
    'Set aesEnc = AES.CreateEncryptor_2((aesKeyBytes), (ivKeyBytes))
    cipherBytes = AES.CreateEncryptor().TransformFinalBlock((plainBytes), 0, UBound(plainBytes))
    Encrypt = B64Encode(cipherBytes)
End Function