这是C#代码。我想在JAVA中完全实现它。 加密结果与C#相同。如果你知道的话。请留意我!!
public static byte[] HexStringToByteArray(string hex)
{
int hexLen = hex.Length;
byte[] ret = new byte[hexLen / 2];
for (int i = 0; i < hexLen; i += 2)
{
ret[i / 2] = Convert.ToByte(hex.Substring(i, 2), 16);
}
return ret;
}
public string doEncryption(string password)
{
// If needed, working sample values for mod and exp can be provided
string mod = "9e5787bead673a363178b9437030edf4ea6e26640c1f318e447d5685ddcf582002d15f459b392abffe0fd6001cb1f289f14be8b67da21e7982edf72b15ff8f7859e4c6bbb26abc99eb58ead851b4eec9f7a2a9b0c3dca3339c03f1c1a9a6d9a458728f8eb129cb52dee0606e5e6ffbfbf0fa777f8b3ca04a63da66ce4ef32b5aad4ee11928b8c3a6af10fedc2b76b0d7afc3b5c53d8572ee04cd16fcc364aa8486d53d4b773a20a2628f3db4483eea00fc441755e061b1184f2a21bd8753fb33982330eb0fc94f30960640dfa3876ddf7d8ef907f9eff23b67a225d40d6c15dea90649f51fd113c9f39467961962f1b9992140e3c10b003f0afeb8c95eec7235";
string exp = "010001";
RNGCryptoServiceProvider secureRandom = new RNGCryptoServiceProvider(); // Import System.Security.Cryptography
byte[] encryptedPasswordBytes;
using (var rsaEncryptor = new RSACryptoServiceProvider())
{
var passwordBytes = Encoding.ASCII.GetBytes(password);
var rsaParameters = rsaEncryptor.ExportParameters(false);
rsaParameters.Exponent = HexStringToByteArray(exp);
rsaParameters.Modulus = HexStringToByteArray(mod);
rsaEncryptor.ImportParameters(rsaParameters);
encryptedPasswordBytes = rsaEncryptor.Encrypt(passwordBytes, false);
}
// Encrypted password should be identical in the C# and Java versions
string encryptedPassword = Convert.ToBase64String(encryptedPasswordBytes);
return encryptedPassword;
}