Tengo las siguientes funciones para encriptar y desencriptar un string en C#,y necesito desencriptarlo en java con misma key e initvector。
请帮助我。
对于C#中的字符串加密和解密,我具有以下功能。我需要使用相同的密钥和initvector在Java中对其进行解密。
谢谢。
public const string PASS_PHRASE = "123456789";
private const string initVector = "pemgail9uzpgzl88";
private const int keysize = 256;
public string EncryptString(string plainText)
{
byte[] initVectorBytes = Encoding.UTF8.GetBytes(initVector);
byte[] plainTextBytes = Encoding.UTF8.GetBytes(plainText);
PasswordDeriveBytes password = new PasswordDeriveBytes(PASS_PHRASE, null);
byte[] keyBytes = password.GetBytes(keysize / 8);
RijndaelManaged symmetricKey = new RijndaelManaged();
symmetricKey.Mode = CipherMode.CBC;
ICryptoTransform encryptor = symmetricKey.CreateEncryptor(keyBytes, initVectorBytes);
MemoryStream memoryStream = new MemoryStream();
CryptoStream cryptoStream = new CryptoStream(memoryStream, encryptor, CryptoStreamMode.Write);
cryptoStream.Write(plainTextBytes, 0, plainTextBytes.Length);
cryptoStream.FlushFinalBlock();
byte[] cipherTextBytes = memoryStream.ToArray();
memoryStream.Close();
cryptoStream.Close();
return Convert.ToBase64String(cipherTextBytes);
}
public string DecryptString(string cipherText)
{
if (string.IsNullOrEmpty(cipherText)) return null;
byte[] initVectorBytes = Encoding.ASCII.GetBytes(initVector);
byte[] cipherTextBytes = Convert.FromBase64String(cipherText);
PasswordDeriveBytes password = new PasswordDeriveBytes(PASS_PHRASE, null);
byte[] keyBytes = password.GetBytes(keysize / 8);
RijndaelManaged symmetricKey = new RijndaelManaged();
symmetricKey.Mode = CipherMode.CBC;
ICryptoTransform decryptor = symmetricKey.CreateDecryptor(keyBytes, initVectorBytes);
MemoryStream memoryStream = new MemoryStream(cipherTextBytes);
CryptoStream cryptoStream = new CryptoStream(memoryStream, decryptor, CryptoStreamMode.Read);
byte[] plainTextBytes = new byte[cipherTextBytes.Length];
int decryptedByteCount = cryptoStream.Read(plainTextBytes, 0, plainTextBytes.Length);
memoryStream.Close();
cryptoStream.Close();
return Encoding.UTF8.GetString(plainTextBytes, 0, decryptedByteCount);
}
答案 0 :(得分:0)
这是我在Java项目中用于加密/解密的一些代码。它使用AES作为加密算法,但是您应该能够找出将其与其他算法结合使用所需的更改。这些方法之外,还可以对base64进行编码/解码。
public static byte[] encryptAes(byte[] messageBytes, byte[] keyBytes, byte[] ivBytes) {
try {
IvParameterSpec iv = new IvParameterSpec(ivBytes);
SecretKeySpec skeySpec = new SecretKeySpec(keyBytes, "AES");
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5PADDING");
cipher.init(Cipher.ENCRYPT_MODE, skeySpec, iv);
byte[] encrypted = cipher.doFinal(messageBytes);
return encrypted;
} catch (Exception ex) {
ex.printStackTrace();
}
return null;
}
public static byte[] decryptAes(byte[] messageBytes, byte[] keyBytes, byte[] ivBytes) {
try {
IvParameterSpec iv = new IvParameterSpec(ivBytes);
SecretKeySpec skeySpec = new SecretKeySpec(keyBytes, "AES");
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5PADDING");
cipher.init(Cipher.DECRYPT_MODE, skeySpec, iv);
byte[] original = cipher.doFinal(messageBytes);
return original;
} catch (Exception ex) {
ex.printStackTrace();
}
return null;
}
无论如何,您可以在这里找到越来越多的特定信息: https://docs.oracle.com/javase/7/docs/api/javax/crypto/package-summary.html