[英]AES encryption with SHA256
What i need is to encrypt a string in AES with Sha256 with a key java code for the same encryption is 我需要的是使用相同的密钥Java代码使用Sha256用AES加密AES中的字符串
private static final String AES_KEY = "something";
String encodedText = null;
try {
final MessageDigest md = MessageDigest.getInstance("SHA-256");
final byte[] digestOfPassword = md.digest(AES_KEY.getBytes("utf-8"));
final SecretKey key = new SecretKeySpec(digestOfPassword, "AES");
final Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
final IvParameterSpec iv = new IvParameterSpec(new byte[16]);
cipher.init(Cipher.ENCRYPT_MODE, key, iv);
final byte[] plainTextBytes = orignalText.getBytes("utf-8");
final byte[] encodeTextBytes = cipher.doFinal(plainTextBytes);
encodedText = new Base64().encodeToString(encodeTextBytes);
}
but i need equivalent in C#, what i was able to develop as 但我需要在C#中具有同等水平,因此我能够将其开发为
private static byte[] ivBytes = new byte[] { 0x00, 0x01, 0x02, 0x03, 0x04,
0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f };
// keyBytes
private static byte[] keyBytes = new byte[] { 0x60, 0x3d, (byte) 0xeb,
0x10, 0x15, (byte) 0xca, 0x71, (byte) 0xbe, 0x2b, 0x73,
(byte) 0xae, (byte) 0xf0, (byte) 0x85, 0x7d, 0x77, (byte) 0x81,
0x1f, 0x35, 0x2c, 0x07, 0x3b, 0x61, 0x08, (byte) 0xd7, 0x2d,
(byte) 0x98, 0x10, (byte) 0xa3, 0x09, 0x14, (byte) 0xdf,
(byte) 0xf4 };
public string AES_Encrypt(string ToBeEncrypted, string password)
{
RijndaelManaged aes = new RijndaelManaged();
aes.BlockSize = 128;
aes.KeySize = 256;
// It is equal in java
/// Cipher _Cipher = Cipher.getInstance("AES/CBC/PKCS5PADDING");
aes.Mode = CipherMode.CBC;
aes.Padding = PaddingMode.PKCS7;
SHA256 sha = new SHA256Managed();
aes.Key = sha.ComputeHash(Encoding.UTF8.GetBytes(password));
aes.IV = ivBytes;
ICryptoTransform encrypto = aes.CreateEncryptor();
byte[] plainTextByte = ASCIIEncoding.UTF8.GetBytes(ToBeEncrypted);
byte[] CipherText = encrypto.TransformFinalBlock(plainTextByte, 0, plainTextByte.Length);
string enc = BitConverter.ToString(CipherText).Replace("-", string.Empty);
return Convert.ToBase64String(CipherText) + "----"+ Convert.ToBase64String(ASCIIEncoding.UTF8.GetBytes(enc));
}
I am so much confused with term SHA256 in AES, and PKCS5Padding. 我对AES和PKCS5Padding中的术语SHA256感到非常困惑。 I have used the same key that is used for encryption, but not able to get same output as java code gives.
我使用了与加密相同的密钥,但无法获得与Java代码相同的输出。
In Java you are using an empty/zero: IV 在Java中,您使用的是空值/零值:IV
final IvParameterSpec iv = new IvParameterSpec(new byte[16]);
In C# you are using 000102030405060708090A0B0C0D0E0F
在C#中,您正在使用
000102030405060708090A0B0C0D0E0F
private static byte[] ivBytes = new byte[] { 0x00, 0x01, 0x02, 0x03, 0x04,
0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f };
Since you're having both perform the encryption and you're comparing the encrypted results, this will have a massive effect on the output. 由于您都在执行加密并且要比较加密的结果,因此这将对输出产生巨大影响。 (If one was decrypting and the other encrypting the errors would be limited to the first 16 bytes).
(如果一个正在解密而另一个正在加密,则错误将限于前16个字节)。
I also feel compelled to point out that SHA-2-256 is a terrible key derivation function. 我也不得不指出SHA-2-256是一个可怕的密钥派生功能。 If you're trying to turn a password into a key you should use a real KDF, like PBKDF2 (password-based key derivation function (version) 2).
如果尝试将密码转换为密钥,则应使用真实的KDF,例如PBKDF2(基于密码的密钥派生功能(版本2))。 In .NET it's implemented as
Rfc2898DeriveBytes
. 在.NET中,它实现为
Rfc2898DeriveBytes
。 In Java it seems to be the "PBKDF2WithHmacSHA1" SecretKeyFactory
. 在Java中,它似乎是“ PBKDF2WithHmacSHA1”
SecretKeyFactory
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.