将加密代码从vb.NET转换为java

将加密代码从vb.NET转换为java,java,vb.net,encryption,Java,Vb.net,Encryption,我需要帮助将我的vb.net代码转换为java,并生成与vb.net中相同的加密字符串 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

我需要帮助将我的vb.net代码转换为java,并生成与vb.net中相同的加密字符串

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 = "0123456789ABCDEF";
 private static void appendHex(StringBuffer sb, byte b) {
         sb.append(HEX.charAt((b>>4)&0x0f)).append(HEX.charAt(b&0x0f));
 }
}
import java.security.MessageDigest;
导入java.security.SecureRandom;
导入javax.crypto.Cipher;
导入javax.crypto.KeyGenerator;
导入javax.crypto.SecretKey;
导入javax.crypto.spec.SecretKeySpec;
公共类SimpleCrypto{
公共静态字符串加密(字符串种子、字符串明文)引发异常{
byte[]rawKey=getRawKey(seed.getBytes(“US-ASCII”);
byte[]result=encrypt(rawKey,cleartext.getBytes());
返回到hex(结果);
}
私有静态字节[]getRawKey(字节[]种子)引发异常{
信息文摘md;
md=MessageDigest.getInstance(“MD5”);
//医学博士(种子);
字节[]temp=md.digest(种子);
字节[]原始=新字节[32];
系统阵列副本(温度,0,原始,0,温度长度);
系统阵列副本(温度、0、原始、温度长度、温度长度);
返回原材料;
//返回null;
}
私有静态字节[]加密(字节[]原始,字节[]清除)引发异常{
SecretKeySpec skeySpec=新SecretKeySpec(原始,“AES/ECB/NoPadding”);
Cipher Cipher=Cipher.getInstance(“AES/ECB/NoPadding”);
cipher.init(cipher.ENCRYPT_模式,skeySpec);
字节[]加密=cipher.doFinal(清除);
返回加密;
}
公共静态字符串toHex(字符串txt){
返回到hex(txt.getBytes());
}
公共静态字符串fromHex(字符串十六进制){
返回新字符串(toByte(hex));
}
公共静态字节[]toByte(字符串hexString){
int len=hexString.length()/2;
字节[]结果=新字节[len];
对于(int i=0;i>4)和0x0f)).append(十六进制字符(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) main(start.java:12)


首先使用ECB是不好的,必须避免,如果可以在vb.net和java中进行更改,请使用CBC。

由于美国法律的限制,问题可能来自您的密钥的大小。要更改此限制,请参阅oracle文档并编辑jdk/jre:


欢迎来到StackOverflow(SO)!当您遇到特定问题时(如将代码的特定部分转换为另一种语言),最好包括您尝试过的所有内容以及在执行此操作时遇到的任何问题。因此,它不是一个代码编写服务。我们希望你至少做一些工作。当你这样做的时候,你会得到更好的答案。当您自己尝试时,请包含示例字符串以验证加密是否有效。请添加start.java并在代码中用注释标记stacktrace中的行。您可以更改VB.net代码吗?“它在几个方面被破坏了。@CodesInChaos从好的方面来看,
getRawKey
仍然比Android代码片段中的要好:P