[英]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 我需要将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
i have tried the following java code but im getting an exception 我已经尝试了以下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));
}
}
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) 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)
First use ECB is bad and must be avoided, use CBC instead if you can change in both vb.net and java. 第一次使用ECB是不好的,必须避免使用,如果可以在vb.net和java中进行更改,请改用CBC。 https://www.adayinthelifeof.nl/2010/12/08/encryption-operating-modes-ecb-vs-cbc/ 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 : 要更改此限制,请参见oracle文档并编辑您的jdk / jre:
http://www.oracle.com/technetwork/java/javase/downloads/index.html http://www.oracle.com/technetwork/java/javase/downloads/index.html
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.