我有以下DHL提供的源代码,它在PHP中执行MCRYPT_RIJNDAEL_256和MCRYPT_RIJNDAEL_128加密:
<?php
function encrypt_128($data, $key) {
return base64_encode(mcrypt_encrypt(MCRYPT_RIJNDAEL_128, $key, $data, MCRYPT_MODE_ECB));
}
function encrypt_256($data, $key) {
return base64_encode(mcrypt_encrypt(MCRYPT_RIJNDAEL_256, $key, $data, MCRYPT_MODE_ECB));
}
$data = "sellerid=Company&returnsreference=0003&contents_value=101&contents_description=clothes&fullname=Joe%20Bloggs&company=Company&email=joe.bloggs@gmail.com&email_confirm=joe.bloggs@gmail.com&telephone=123&address_line_1=1&city=Acampo&postcode=952200001&country_code=US&weight=1&height=2&length=3&width=4";
$key = "b1U995YFbERWuzO72GmKSBWpACVIb3L9";
echo '128:' . PHP_EOL;
echo encrypt_128($data, $key) . PHP_EOL;
echo PHP_EOL;
echo '256:' . PHP_EOL;
echo encrypt_256($data, $key) . PHP_EOL;
echo PHP_EOL;
?>
哪个输出:
128:
AEt1OHf8hjEnQYeJq07qIxj1j8M0Tyzbg4/5NCwzMwIAA72SecGEdi4XoxwXlgpTbNRyum642KVM3POo2pcPPiRiA6+WPEf6XB0eTAzIUVp8yt2Ibjk83SsNVHCK7n4lR5wXICQ4Go/zFdz68Uwc2Y2mu3SCdiferc2noyhLaInEtej8Q8yF2NwwsZreLtf/6zM7S4RKR6SDa7G2EM3wu0FjOhcyA+6HaTpl+3ZFSdMs5YwqksZjWD5SYQskVXks5y4wtw++sduJ77p2iMe/bLdIAtNwgwLHty9y7jFellxlWMeWmysmyYFNUPaoUPbXM4kDlBle3jz4sjpLiUdjQdLJIfCN6UWyQf47vFzxubt3IfsbiqHU2Qo9mHb7WQh4vtPC+U3wjM9kzV9eXcMCRQ==
256:
BUMVdaCQoce//aQWEsMgSHNRsWVPvi02ljCtF1PUfEA8Wm0tTZ6EUBEGsAiTOSINwiWikaoGm5jyBRaajHgtgj4y/7R8t5KkxEwM5H9iU/X56c8H65p3pV2P7wNayLiu5eUcLJBXb+siqCCi2VFETgGOMC8o9zjXPQ0oNCYYy5+tT2NGxSGebjM7XMIOAkIKCKvSbF+lUdxPRS1vs5ILuvzx9LP4+461ZYGYmmDKX35qPbCnsx8xaYkXnhAO8W1ddm1ylcanyAFWb2hk6UUmS7SGpSmJMAVwyUDT2uFxaizL4Z6p+KeBRgjPW2i9b5gC/MpjphJO4IKhTqKfHwziinYQ6FZOtDKtD7JC8om1kINpaMIZEHLXHEHF/iuqRbaFcH+M1Qph4Uu95d0eJ0x1A2rs2zRdlWVR72RHVIWez0w=
我需要在Java中执行256位Rijndael加密。我写了以下代码:
import org.apache.commons.codec.binary.Base64;
import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import java.io.UnsupportedEncodingException;
import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.spec.AlgorithmParameterSpec;
public class Encrypt {
public static byte[] ivBytes = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
public static void main(String[] args) throws InvalidKeyException, UnsupportedEncodingException, NoSuchAlgorithmException, NoSuchPaddingException, InvalidAlgorithmParameterException, IllegalBlockSizeException, BadPaddingException {
String data = "sellerid=Company&returnsreference=0003&contents_value=101&contents_description=clothes&fullname=Joe%20Bloggs&company=Company&email=joe.bloggs@gmail.com&email_confirm=joe.bloggs@gmail.com&telephone=123&address_line_1=1&city=Acampo&postcode=952200001&country_code=US&weight=1&height=2&length=3&width=4";
String key = "b1U995YFbERWuzO72GmKSBWpACVIb3L9";
String encoded = encode(data, key);
System.out.println("Encoded: " + encoded);
String decoded = decode(encoded, key);
System.out.println("Decoded: " + decoded);
}
public static String encode(String data, String key) throws java.io.UnsupportedEncodingException, NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, InvalidAlgorithmParameterException, IllegalBlockSizeException, BadPaddingException {
byte[] textBytes = data.getBytes("UTF-8");
AlgorithmParameterSpec ivSpec = new IvParameterSpec(ivBytes);
SecretKeySpec newKey = new SecretKeySpec(key.getBytes("UTF-8"), "AES");
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
cipher.init(Cipher.ENCRYPT_MODE, newKey, ivSpec);
return Base64.encodeBase64String(cipher.doFinal(textBytes));
}
public static String decode(String data, String key) throws java.io.UnsupportedEncodingException, NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, InvalidAlgorithmParameterException, IllegalBlockSizeException, BadPaddingException {
byte[] textBytes = Base64.decodeBase64(data);
AlgorithmParameterSpec ivSpec = new IvParameterSpec(ivBytes);
SecretKeySpec newKey = new SecretKeySpec(key.getBytes("UTF-8"), "AES");
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
cipher.init(Cipher.DECRYPT_MODE, newKey, ivSpec);
return new String(cipher.doFinal(textBytes), "UTF-8");
}
}
正在输出:
Encoded: AEt1OHf8hjEnQYeJq07qIxtV0c4Zcfro0Yidrx/rb91OBcbyZ1RZ5id7dYCVhJe4WUvIb2OWyOVDqeVu7cHkM7zHrxRoY5hyQDWKsj4BIuxqxKO0grLYRgqQ2MZtm23D5Ko23h5sJetEYsKI1AxeXJLPQmUDB+RecELIDzJparWto9UcU6llik/v8ZX7DDmrg6K57xaSlCKWVkCKI2HivblAYwTirh7D9n/hO46c3MLngM1/Ao32S1y2XwkNX8k7PTFce5M6BLsDlzoZKjkv+t8n8GTAPPLheC0z9iLBHQbEs2WhzVY4UoCNStITWitn6QI9EwCuVX7oRVlQMi2pqow0GaGwFJ3kqGn5nbXlrbc5Lg4G8SUnM1NB01LbIEBVnVuzXQg8y+Zam47uMJ/0iQ==
Decoded: sellerid=Company&returnsreference=0003&contents_value=101&contents_description=clothes&fullname=Joe%20Bloggs&company=Company&email=joe.bloggs@gmail.com&email_confirm=joe.bloggs@gmail.com&telephone=123&address_line_1=1&city=Acampo&postcode=952200001&country_code=US&weight=1&height=2&length=3&width=4
即。它使用128位加密对其进行加密。如何获得256位Rijndael加密?
答案 0 :(得分:3)
找到了Artjom的解决方案。
import org.apache.commons.codec.binary.Base64;
import org.bouncycastle.crypto.BlockCipher;
import org.bouncycastle.crypto.InvalidCipherTextException;
import org.bouncycastle.crypto.engines.RijndaelEngine;
import org.bouncycastle.crypto.paddings.PaddedBufferedBlockCipher;
import org.bouncycastle.crypto.paddings.ZeroBytePadding;
import org.bouncycastle.crypto.params.KeyParameter;
import java.nio.charset.Charset;
public class Encrypt {
public static void main(String[] args) throws InvalidCipherTextException {
String key = "b1U995YFbERWuzO72GmKSBWpACVIb3L9";
String data = "sellerid=Company&returnsreference=0003&contents_value=101&contents_description=clothes&fullname=Joe%20Bloggs&company=Company&email=joe.bloggs@gmail.com&email_confirm=joe.bloggs@gmail.com&telephone=123&address_line_1=1&city=Acampo&postcode=952200001&country_code=US&weight=1&height=2&length=3&width=4";
encrypt(key, data);
}
private static void encrypt(String key, String data) throws InvalidCipherTextException {
byte[] givenKey = key.getBytes(Charset.forName("ASCII"));
final int keysize = 256;
byte[] keyData = new byte[keysize / Byte.SIZE];
System.arraycopy(givenKey, 0, keyData, 0, Math.min(givenKey.length, keyData.length));
KeyParameter keyParameter = new KeyParameter(keyData);
BlockCipher rijndael = new RijndaelEngine(256);
ZeroBytePadding c = new ZeroBytePadding();
PaddedBufferedBlockCipher pbbc = new PaddedBufferedBlockCipher(rijndael, c);
pbbc.init(true, keyParameter);
byte[] plaintext = data.getBytes(Charset.forName("UTF8"));
byte[] ciphertext = new byte[pbbc.getOutputSize(plaintext.length)];
int offset = 0;
offset += pbbc.processBytes(plaintext, 0, plaintext.length, ciphertext, offset);
offset += pbbc.doFinal(ciphertext, offset);
System.out.println("Encrypted: " + Base64.encodeBase64String(ciphertext));
}
}
这打印出以下内容:
Encrypted: BUMVdaCQoce//aQWEsMgSHNRsWVPvi02ljCtF1PUfEA8Wm0tTZ6EUBEGsAiTOSINwiWikaoGm5jyBRaajHgtgj4y/7R8t5KkxEwM5H9iU/X56c8H65p3pV2P7wNayLiu5eUcLJBXb+siqCCi2VFETgGOMC8o9zjXPQ0oNCYYy5+tT2NGxSGebjM7XMIOAkIKCKvSbF+lUdxPRS1vs5ILuvzx9LP4+461ZYGYmmDKX35qPbCnsx8xaYkXnhAO8W1ddm1ylcanyAFWb2hk6UUmS7SGpSmJMAVwyUDT2uFxaizL4Z6p+KeBRgjPW2i9b5gC/MpjphJO4IKhTqKfHwziinYQ6FZOtDKtD7JC8om1kINpaMIZEHLXHEHF/iuqRbaFcH+M1Qph4Uu95d0eJ0x1A2rs2zRdlWVR72RHVIWez0w=