我需要幫助,我的vb.net代碼轉換爲Java和產生相同的加密字符串作爲在vb.net從vb.NET轉換加密代碼到Java
Public Function AES_Encrypt(ByVal input As String) As String
Dim AES As New System.Security.Cryptography.RijndaelManaged
Dim Hash_AES As New System.Security.Cryptography.MD5CryptoServiceProvider
Dim encrypted As String = ""
Try
Dim hash(31) As Byte
Dim temp As Byte() = Hash_AES.ComputeHash(System.Text.ASCIIEncoding.ASCII.GetBytes("mykey1"))
Array.Copy(temp, 0, hash, 0, 16)
Array.Copy(temp, 0, hash, 15, 16)
AES.Key = hash
AES.Mode = Security.Cryptography.CipherMode.ECB
Dim DESEncrypter As System.Security.Cryptography.ICryptoTransform = AES.CreateEncryptor
Dim Buffer As Byte() = System.Text.ASCIIEncoding.ASCII.GetBytes(input)
encrypted = Convert.ToBase64String(DESEncrypter.TransformFinalBlock(Buffer, 0, Buffer.Length))
Return encrypted
Catch ex As Exception
End Try
End Function
我曾嘗試下面的Java代碼,但即時通訊得到一個例外
import java.security.MessageDigest;
import java.security.SecureRandom;
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
public class SimpleCrypto {
public static String encrypt(String seed, String cleartext) throws Exception {
byte[] rawKey = getRawKey(seed.getBytes("US-ASCII"));
byte[] result = encrypt(rawKey, cleartext.getBytes());
return toHex(result);
}
private static byte[] getRawKey(byte[] seed) throws Exception {
MessageDigest md;
md = MessageDigest.getInstance("MD5");
// md.update(seed);
byte[] temp=md.digest(seed);
byte[] raw =new byte[32];
System.arraycopy(temp, 0, raw, 0, temp.length);
System.arraycopy(temp, 0, raw, temp.length, temp.length);
return raw;
// return null;
}
private static byte[] encrypt(byte[] raw, byte[] clear) throws Exception {
SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES/ECB/NoPadding");
Cipher cipher = Cipher.getInstance("AES/ECB/NoPadding");
cipher.init(Cipher.ENCRYPT_MODE, skeySpec);
byte[] encrypted = cipher.doFinal(clear);
return encrypted;
}
public static String toHex(String txt) {
return toHex(txt.getBytes());
}
public static String fromHex(String hex) {
return new String(toByte(hex));
}
public static byte[] toByte(String hexString) {
int len = hexString.length()/2;
byte[] result = new byte[len];
for (int i = 0; i < len; i++)
result[i] = Integer.valueOf(hexString.substring(2*i, 2*i+2), 16).byteValue();
return result;
}
public static String toHex(byte[] buf) {
if (buf == null)
return "";
StringBuffer result = new StringBuffer(2*buf.length);
for (int i = 0; i < buf.length; i++) {
appendHex(result, buf[i]);
}
return result.toString();
}
private final static String HEX = "ABCDEF";
private static void appendHex(StringBuffer sb, byte b) {
sb.append(HEX.charAt((b>>4)&0x0f)).append(HEX.charAt(b&0x0f));
}
}
但即時得到這個錯誤
java.security.InvalidKeyException:非法密鑰大小或默認的 個參數 在javax.crypto.Cipher.checkCryptoPerm(Cipher.java:1010) 在javax.crypto.Cipher.implInit(Cipher.java:785) 在javax.crypto.Cipher.chooseProvider(Cipher.java:848) 在javax.crypto.Cipher.init(Cipher.java:1212) 在javax.crypto.Cipher.init(Cipher.java:1152) 在SimpleCrypto.encrypt(SimpleCrypto.java:53) 在SimpleCrypto.encrypt( SimpleCrypto.java:11) 在start.main(start.java:12)
歡迎的StackOverflow(SO)!當您遇到特定問題時(例如將特定部分代碼轉換爲其他語言),最好包括您嘗試的所有內容以及您遇到的任何問題。 SO不是代碼寫入服務。我們希望你至少做一些工作。當你這樣做時,你會得到更好的答案。當您自己嘗試時,請添加示例字符串以驗證加密是否有效。 – 2014-11-04 10:27:04
請添加start.java並在代碼中使用註釋標記stacktrace中的行。 – 2014-11-04 10:42:51
你可以更改VB.net代碼嗎?它在幾個方面被打破。 – CodesInChaos 2014-11-04 11:52:11