[英]Converting .NET AES encryption to Java
I am trying to write the Java equivalent of some .NET encryption code so that they can decrypt our information over a webservice. 我正在尝试编写等效于某些.NET加密代码的Java,以便它们可以通过Web服务解密我们的信息。
Here is the .NET method: 这是.NET方法:
public static string AESEncrypt(string text, string keyPhrase)
{
byte[] salt = { 0x49, 0x76, 0x61, 0x6e, 0x20, 0x4d, 0x65, 0x64, 0x76, 0x65, 0x64, 0x65, 0x76 };
byte[] data = Encoding.Unicode.GetBytes(text);
PasswordDeriveBytes pdb = new PasswordDeriveBytes(keyPhrase, salt);
Rijndael algorithm = Rijndael.Create();
algorithm.Key = pdb.GetBytes(32);
algorithm.IV = pdb.GetBytes(16);
MemoryStream mStream = new MemoryStream();
CryptoStream cStream = new CryptoStream(mStream, algorithm.CreateEncryptor(), CryptoStreamMode.Write);
cStream.Write(data, 0, data.Length);
cStream.Close();
byte[] bytes = mStream.ToArray();
return Convert.ToBase64String(bytes);
}
Here is my failing attempt at a Java version: 这是我在Java版本上失败的尝试:
public static String encrypt(String text, String keyPhrase) throws Exception {
byte[] salt = { 0x49, 0x76, 0x61, 0x6e, 0x20, 0x4d, 0x65, 0x64, 0x76, 0x65, 0x64, 0x65, 0x76 };
byte[] data = text.getBytes("UTF-16LE");
PBEKeySpec spec = new PBEKeySpec(keyPhrase.toCharArray(), salt, 1);
SecretKey secret = new SecretKeySpec(keyPhrase.getBytes("UTF-16LE"), "AES");
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
cipher.init(Cipher.ENCRYPT_MODE, secret);
byte[] ciphertext = cipher.doFinal(data);
return Base64.encodeBase64String(ciphertext);
}
The first problem I have is figuring out how to match the PasswordDeriveBytes thing for the key and the iv, although I am sure the rest is wrong, but baby steps. 我遇到的第一个问题是弄清楚如何将KeyD和iv的PasswordDeriveBytes进行匹配,尽管我确信其余的都是错误的,但是要小心。 Does anyone know how to match the output in the Java version? 有谁知道如何匹配Java版本中的输出?
To match the PasswordDeriveBytes in java use the PasswordDeriveBytes(extending org.bouncycastle.crypto.generators.PKCS5S1ParametersGenerator) from this stackoverflow answer . 要在Java中匹配PasswordDeriveBytes,请使用此stackoverflow答案中的PasswordDeriveBytes(扩展org.bouncycastle.crypto.generators.PKCS5S1ParametersGenerator)。
But this works only for key under 20 byte!; 但这仅适用于20字节以下的密钥! This is the code to generate the key: 这是生成密钥的代码:
String text="marcoS";
String keyPhrase="password123";
byte[] salt={ 0x49, 0x76, 0x61, 0x6e, 0x20, 0x4d, 0x65, 0x64, 0x76, 0x65, 0x64, 0x65, 0x76 };
PasswordDeriveBytes generator = new PasswordDeriveBytes(new SHA1Digest());
generator.init(keyPhrase.getBytes("ASCII"), salt, 100);
byte[] key = ((KeyParameter) generator.generateDerivedParameters(KeySize)).getKey();
Alternatively replace in dotNet the PasswordDeriveBytes with Rfc2898DeriveBytes. 或者在dotNet中用Rfc2898DeriveBytes替换PasswordDeriveBytes。
To print bytes unsigned(for match with .net) use: 要打印无符号字节(与.net匹配),请使用:
private static void printByteArray(byte[] arr) {
if(arr == null || arr.length == 0){
logger.debug("Array vuoto");
return;
}
StringBuilder sb = new StringBuilder();
for(byte b : arr) {
int x = b;
if(x<0){
x+=256;
}
sb.append("["+x+"] ");
}
logger.debug(sb.toString());
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.