Java中的Bouncy Castle加密.Net中的解密
我使用以下命令使用密码加密字符串Java中的Bouncy Castle加密.Net中的解密,java,c#,encryption,bouncycastle,Java,C#,Encryption,Bouncycastle,我使用以下命令使用密码加密字符串 static String algorithm = "PBEWITHSHA256AND128BITAES-CBC-BC"; static byte[] salt = "b9v4n38s".getBytes(StandardCharsets.UTF_8); static int derivedKeyLength = 128; static int iterations = 20000; public static
static String algorithm = "PBEWITHSHA256AND128BITAES-CBC-BC";
static byte[] salt = "b9v4n38s".getBytes(StandardCharsets.UTF_8);
static int derivedKeyLength = 128;
static int iterations = 20000;
public static byte[] encrypt(String plainText, String password) throws NoSuchPaddingException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException, InvalidKeySpecException, NoSuchAlgorithmException {
Security.addProvider(new BouncyCastleProvider());
KeySpec spec = new PBEKeySpec(password.toCharArray(), salt, iterations, derivedKeyLength);
SecretKeyFactory f = SecretKeyFactory.getInstance(algorithm);
SecretKey key = f.generateSecret(spec);
Cipher cipher = Cipher.getInstance(algorithm);
cipher.init(Cipher.ENCRYPT_MODE, key);
byte[] text = plainText.getBytes(StandardCharsets.UTF_8);
byte[] encrypted = cipher.doFinal(text);
return encrypted;
}
其结果是base64编码并作为arg[0]发送到.Net(arg[1]是相同的密码)。现在我正试图用这段代码解密.Net中的字符串
private static string Decrypt(string[] args)
{
int derivedKeyLength = 128;
int iterations = 20000;
string algorithm = "PBEWITHSHA256AND128BITAES-CBC-BC";
byte[] salt = Encoding.UTF8.GetBytes("b9v4n38s");
PbeParametersGenerator pGen = new Pkcs12ParametersGenerator(new Sha256Digest());
pGen.Init(Encoding.UTF8.GetBytes(args[1]), salt, iterations);
ICipherParameters par = pGen.GenerateDerivedParameters("AES256", derivedKeyLength);
IBufferedCipher c = CipherUtilities.GetCipher(algorithm);
c.Init(false, par);
var input = Convert.FromBase64String(args[0]);
byte[] enc = c.DoFinal(input);
var decoded = Encoding.UTF8.GetString(enc);
return decoded;
}
不幸的是,它在DoFinal上失败,消息为Org.bounchycastle.Crypto.InvalidCipherTextException:“pad block corrupted”
SecretKeyFactory.getInstance(algorithm)
使用与java中的Cipher.getInstance(algorithm)
相同的算法字符串,但如果我尝试pGen.GenerateDrivedParameters(algorithm,derivedKeyLength)
在.Net中,它抛出Org.BouncyCastle.Security.SecurityUtilityException:“未识别算法PBEWITHSA256和128位AES-CBC-BC”。
我并不热衷于这种算法,只是想寻找一种在Java中加密字符串并在.Net中解密的方法。一种可能的C#/BC代码,用于解密由发布的Java代码生成的密文:
使用系统;
使用系统文本;
使用Org.BouncyCastle.Asn1;
使用Org.BouncyCastle.Crypto;
使用Org.BouncyCastle.Security;
...
私有静态字符串算法=“pbewithsha256和128bitaes-CBC-BC”;
私有静态字节[]salt=Encoding.UTF8.GetBytes(“b9v4n38s”);
私有静态int迭代=20000;
公共静态字符串解密(字符串密文B64,字符串密码)
{
IBufferdCipher cipher=CipherUtilities.GetCipher(算法);
ASN1可编码的algParams=PbeUtilities.GenerateAlgorithmParameters(算法、salt、迭代);
ICipherParameters cipherParams=PbeUtilities.GenerateCipherParameters(算法,密码.tocharray(),algParams);
cipher.Init(false,cipherParams);
字节[]cipherBytes=Convert.FromBase64字符串(ciphertextB64);
字节[]解密=cipher.DoFinal(cipherBytes);
返回Encoding.UTF8.GetString(已解密);
}
测试:
string decrypted=Decrypt(“mBy4YwAvUpvoSJhzBnpOCJw2kCayvdYfLJ/12x0BgUKh5m5bvArSheMMs2U5rYyE”,“我的密码”);
Console.WriteLine(已解密);//敏捷的棕色狐狸跳过了懒狗
其中,密文是使用密码MyPassword使用Java代码生成的
请注意,静态盐通常是不安全的(当然测试目的除外)