簡體   English   中英

將加密代碼從vb.NET轉換為Java

[英]Converting Encryption code from vb.NET to java

我需要將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));
 }
}

但我收到此錯誤

java.security.InvalidKeyException:密鑰大小非法或默認參數位於javax.crypto.Cipher.chooseProvider處的javax.crypto.Cipher.implInit(Cipher.java:785)處的javax.crypto.Cipher.checkCryptoPerm(Cipher.java:1010) (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)

第一次使用ECB是不好的,必須避免使用,如果可以在vb.net和java中進行更改,請改用CBC。 https://www.adayinthelifeof.nl/2010/12/08/encryption-operating-modes-ecb-vs-cbc/

由於美國法律的限制,問題可能出在密鑰的大小上。 要更改此限制,請參見oracle文檔並編輯您的jdk / jre:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM