简体   繁体   中英

Converting Encryption code from vb.NET to java

I need help converting my vb.net code to java and produce the same encrypted string as in 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

i have tried the following java code but im getting an exception

   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));
 }
}

but im getting this error

java.security.InvalidKeyException: Illegal key size or default parameters at javax.crypto.Cipher.checkCryptoPerm(Cipher.java:1010) at javax.crypto.Cipher.implInit(Cipher.java:785) at javax.crypto.Cipher.chooseProvider(Cipher.java:848) at javax.crypto.Cipher.init(Cipher.java:1212) at javax.crypto.Cipher.init(Cipher.java:1152) at SimpleCrypto.encrypt(SimpleCrypto.java:53) at SimpleCrypto.encrypt(SimpleCrypto.java:11) at start.main(start.java:12)

First use ECB is bad and must be avoided, use CBC instead if you can change in both vb.net and java. https://www.adayinthelifeof.nl/2010/12/08/encryption-operating-modes-ecb-vs-cbc/

The problem probably comes from the size of your key, because of US law restriction. To change this restriction sees oracle documentation and edit your jdk / jre :

http://www.oracle.com/technetwork/java/javase/downloads/index.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM