你好,在使用j2me bouncy castle aes进行编码和解码时。它在设备上工作正常,但我注意到结果与通常的aes加密不同。因此,一旦用充气城堡加油,就无法与服务器共享信息。例如bouncy castle aes“hello”=“01Fvdltwa2VKVPCH3ueHAA ==”但是使用典型的aes加密“hello”=“0tUfc3Qwod0qw1kVaI8ksw ==”。 有没有人经历过这个?我该如何解决这个问题?请注意我的加密密钥是“re5fdh3jxlow5ncc” 。我需要回复 这是我的代码:
import com.codename1.util.Base64;
import org.bouncycastle.crypto.CipherParameters;
import org.bouncycastle.crypto.engines.AESEngine;
import org.bouncycastle.crypto.modes.CBCBlockCipher;
import org.bouncycastle.crypto.paddings.PaddedBufferedBlockCipher;
import org.bouncycastle.crypto.params.KeyParameter;
import org.bouncycastle.crypto.params.ParametersWithIV;
/**
*
* @author SAMUEL
*/
public class Tester {
static String strEnc = "Hi This is my String";
final static String strPassword = "password12345678";
private static byte[] cipherData(PaddedBufferedBlockCipher cipher, byte[] data)throws Exception
{
int minSize = cipher.getOutputSize(data.length);
byte[] outBuf = new byte[minSize];
int length1 = cipher.processBytes(data, 0, data.length, outBuf, 0);
int length2 = cipher.doFinal(outBuf, length1);
int actualLength = length1 + length2;
byte[] result = new byte[actualLength];
System.arraycopy(outBuf, 0, result, 0, result.length);
return result;
}
private static byte[] decrypt(byte[] cipher, byte[] key, byte[] iv) throws Exception
{
PaddedBufferedBlockCipher aes = new PaddedBufferedBlockCipher(new CBCBlockCipher(new AESEngine()));
CipherParameters ivAndKey = new ParametersWithIV(new KeyParameter(key), iv);
aes.init(false, ivAndKey);
return cipherData(aes, cipher);
}
private static byte[] encrypt(byte[] plain, byte[] key, byte[] iv) throws Exception
{
PaddedBufferedBlockCipher aes = new PaddedBufferedBlockCipher(new CBCBlockCipher(new AESEngine()));
CipherParameters ivAndKey = new ParametersWithIV(new KeyParameter(key), iv);
aes.init(true, ivAndKey);
return cipherData(aes, plain);
}
public static void main(String [] args) throws Exception{
byte[] enc= encrypt(strEnc.getBytes(),"password12345678".getBytes(), "password12345678".getBytes());
String encbase = Base64.encode(enc);
String encrypted =new String(encbase.getBytes(), "UTF-8");
System.out.println("Encrypted is:"+encbase);
byte[] decbase = Base64.decode(encrypted.getBytes());
byte[] dec= decrypt(decbase,"password12345678".getBytes() , "password12345678".getBytes());
System.out.println("Decrypted file is:"+new String(dec, "UTF-8"));
}
}
但请注意结果与典型的aes加密不同