我正在尝试加密java中的密码,以下是连接到C#系统的代码。此加密使用Rfc2898DeriveBytes完成。
class StringCipher
{
private const int Keysize = 256;
private const int DerivationIterations = 1000;
public static string Encrypt(string plainText, string passPhrase)
{
var saltStringBytes = Generate256BitsOfRandomEntropy();
var ivStringBytes = Generate256BitsOfRandomEntropy();
var plainTextBytes = Encoding.UTF8.GetBytes(plainText);
using (var password = new Rfc2898DeriveBytes(passPhrase, saltStringBytes, DerivationIterations))
{
var keyBytes = password.GetBytes(Keysize / 8);
using (var symmetricKey = new RijndaelManaged())
{
symmetricKey.BlockSize = 256;
symmetricKey.Mode = CipherMode.CBC;
symmetricKey.Padding = PaddingMode.PKCS7;
using (var encryptor = symmetricKey.CreateEncryptor(keyBytes, ivStringBytes))
{
using (var memoryStream = new MemoryStream())
{
using (var cryptoStream = new CryptoStream(memoryStream, encryptor, CryptoStreamMode.Write))
{
cryptoStream.Write(plainTextBytes, 0, plainTextBytes.Length);
cryptoStream.FlushFinalBlock();
var cipherTextBytes = saltStringBytes;
cipherTextBytes = cipherTextBytes.Concat(ivStringBytes).ToArray();
cipherTextBytes = cipherTextBytes.Concat(memoryStream.ToArray()).ToArray();
memoryStream.Close();
cryptoStream.Close();
return Convert.ToBase64String(cipherTextBytes);
}
}
}
}
}
}
private static byte[] Generate256BitsOfRandomEntropy()
{
var randomBytes = new byte[32];
using (var rngCsp = new RNGCryptoServiceProvider())
{
rngCsp.GetBytes(randomBytes);
}
return randomBytes;
}
}
下面是我在java中尝试过的并且它对我不起作用.net加密是使用Rfc2898DeriveBytes完成的,这里我使用了SecureRandom sr = new SecureRandom();作为一个等价物,但由于某种原因,我无法加密密码。请帮忙
public class PasswordEncryption {
private final static int DerivationIterations = 1000;
private final int Keysize = 256;
private static byte[] getSalt() throws NoSuchAlgorithmException {
SecureRandom sr = new SecureRandom();
byte[] salt = new byte[32];
sr.nextBytes(salt);
return salt;
}
public static String Encrypt(String plainText, String passPhrase)
{
Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());
byte[] saltStringBytes = getSalt();
byte[] IvStringBytes=getSalt();
byte[] pwd = plainText.getBytes("UTF-8");
char[] chars = passPhrase.toCharArray();
SecretKeyFactory factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1");
KeySpec pbeKeySpec = new PBEKeySpec(chars, saltStringBytes, DerivationIterations, 256);
SecretKey secretKey = factory.generateSecret(pbeKeySpec);
System.out.println(secretKey.toString());
SecretKey secret = new SecretKeySpec(secretKey.getEncoded(), "AES");
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS7Padding","BC");
cipher.init(Cipher.ENCRYPT_MODE, secret);
byte[] result = cipher.doFinal(pwd);
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
outputStream.write(saltStringBytes);
outputStream.write(IvStringBytes);
outputStream.write(result);
byte end[] = outputStream.toByteArray();
String base64Encoded = Base64.getEncoder().encodeToString(end);
System.out.println(base64Encoded);
return base64Encoded;
}
public static void main(String[] args) {
Encrypt("passwordhere", "ABC");
}
}