Blowfish in C# from Java
Is there anything in c# already out there with an example of doing the same thing like this in Java. I am trying the below code. Does the encryption look right? It has a bunch of ? marks in it.
Cipher cipher = Cipher.getInstance("BlowFish/ECB/PKCS5Padding");
byte[] keyBytes = Enco开发者_开发知识库ding.ASCII.GetBytes(key);
string keyHex = Hex.ToHexString(keyBytes); //4b334c33315551354f38325059344739
string parameters = "{\"userId\":\"6440870\"}";
byte[] parametersByte = Encoding.ASCII.GetBytes(parameters);
string parametersHex = Hex.ToHexString(parametersByte); //7b22757365724964223a2236343430383730227d
BlowFish bl = new BlowFish(keyHex);
byte[] outputEncryptedByte = bl.Encrypt_ECB(parametersByte);
string outputEncrypted = Encoding.ASCII.GetString(outputEncryptedByte); //7lC[t$?mQd?g???kE?W?[?
string outputBase64 = System.Convert.
ToBase64String(outputEncryptedByte); //N2xDW3Qk/xgObVFkpmfBgchrRepXnVu9
Its not implemented in Framework but you can get an Implementation in C# from here
http://www.schneier.com/blowfish-download.html
just put that code in a .cs file in your project directory and use it like this.
BlowFish b = new BlowFish("04B915BA43FEB5B6");
string plainText = "The quick brown fox jumped over the lazy dog.";
string cipherText = b.Encrypt_CBC(plainText);
MessageBox.Show(cipherText);
plainText = b.Decrypt_CBC(cipherText);
MessageBox.Show(plainText);
One more implementation of BlowFish in Java -- http://dexxtr.com/post/57145943236/blowfish-encrypt-and-decrypt-in-java-android
private byte[] encrypt(String key, String plainText) throws GeneralSecurityException {
SecretKey secret_key = new SecretKeySpec(key.getBytes(), ALGORITM);
Cipher cipher = Cipher.getInstance(ALGORITM);
cipher.init(Cipher.ENCRYPT_MODE, secret_key);
return cipher.doFinal(plainText.getBytes());
}
精彩评论