从Java到C的AES加密#
我用Java编写了这段代码,并且成功地加密了字符串从Java到C的AES加密#,java,c#,encryption,aes,Java,C#,Encryption,Aes,我用Java编写了这段代码,并且成功地加密了字符串 public AESCrypt(String password) throws Exception { // hash password with SHA-256 and crop the output to 128-bit for key MessageDigest digest = MessageDigest.getInstance("SHA-256"); digest.update(password.getByte
public AESCrypt(String password) throws Exception
{
// hash password with SHA-256 and crop the output to 128-bit for key
MessageDigest digest = MessageDigest.getInstance("SHA-256");
digest.update(password.getBytes("UTF-8"));
byte[] keyBytes = new byte[32];
System.arraycopy(digest.digest(), 0, keyBytes, 0, keyBytes.length);
cipher = Cipher.getInstance("AES/CBC/PKCS7Padding");
key = new SecretKeySpec(keyBytes, "AES");
spec = getIV();
}
public AlgorithmParameterSpec getIV()
{
byte[] iv = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, };
IvParameterSpec ivParameterSpec;
ivParameterSpec = new IvParameterSpec(iv);
return ivParameterSpec;
}
public String encrypt(String plainText) throws Exception
{
cipher.init(Cipher.ENCRYPT_MODE, key, spec);
byte[] encrypted = cipher.doFinal(plainText.getBytes("UTF-8"));
String encryptedText = new String(Base64.encode(encrypted, Base64.DEFAULT), "UTF-8");
return encryptedText;
}
示例:我有字符串093949,通过上面的代码我们得到“b28VNQIxfmlCfLcjQRS7Dw=”
在C#中,我试图得到同样的结果,但我做错了什么,我被绊倒了。
我需要在java中得到相同的结果
C#
我们这样称呼c#方法:
string senhaencrypted = Encrypt(string_tobeencrypt, password);
我的回报是:“Nv1V4RIJ1TczqnKGdN8jMA==”而不是“b28VNQIxfmlCfLcjQRS7Dw==”
如果有人知道这是怎么回事,请告诉我。
谢谢 这里有一个从Java到C#的端口(就我作为C#开发人员所能解释的Java代码而言) 但是,使用纯文本
“093949”
和密码“1OnePlaceServicesDecryptionKey”
代码不会产生“b28VNQIxfmlCfLcjQRS7Dw=”
。所以还是有问题。但我认为下面的代码应该更接近原始代码
public static string Encrypt(string plainText, string password)
{
byte[] keyBytes;
using (SHA256 hash = SHA256.Create())
{
keyBytes = hash.ComputeHash(Encoding.UTF8.GetBytes(password));
}
using (Aes aes = Aes.Create())
{
aes.Mode = CipherMode.CBC;
aes.Padding = PaddingMode.PKCS7;
aes.Key = keyBytes;
aes.IV = new byte[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
using (ICryptoTransform cryptoTransform = aes.CreateEncryptor())
{
byte[] plainBytes = Encoding.UTF8.GetBytes(plainText);
byte[] encryptedBytes = cryptoTransform.TransformFinalBlock(plainBytes, 0, plainBytes.Length);
return Convert.ToBase64String(encryptedBytes);
}
}
}
标题是AES-256,代码中的注释是AES 128位。我试着像java代码一样改成256,但我做不到,我真的被绊住了。我试着逐行转换,因为我不知道c语言和java语言有多像。密码是“1OnePlaceServicesDecryptionKey”。当:1)您使用不同的密钥大小,2)您用Java而不是C对密码进行哈希运算时,您怎么能期望在C#中得到相同的答案?哈希em 256位是什么意思?我用同样的钥匙尺寸在这里试着…但还是有差异。谢谢你的回答,我的朋友!
public static string Encrypt(string plainText, string password)
{
byte[] keyBytes;
using (SHA256 hash = SHA256.Create())
{
keyBytes = hash.ComputeHash(Encoding.UTF8.GetBytes(password));
}
using (Aes aes = Aes.Create())
{
aes.Mode = CipherMode.CBC;
aes.Padding = PaddingMode.PKCS7;
aes.Key = keyBytes;
aes.IV = new byte[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
using (ICryptoTransform cryptoTransform = aes.CreateEncryptor())
{
byte[] plainBytes = Encoding.UTF8.GetBytes(plainText);
byte[] encryptedBytes = cryptoTransform.TransformFinalBlock(plainBytes, 0, plainBytes.Length);
return Convert.ToBase64String(encryptedBytes);
}
}
}