我們有一個需要將某些數據傳遞給Java應用程序的.NET應用程序。我們想要給它一些簡單的加密,M.C.霍金不會被黑客入侵,但它不需要純文本。C#(幫助)中的AES加密和Java中的解密(完成)
我發現了一些很棒的Java代碼,可以讓我使用AES進行加密/解密。我希望能找到的是C#的匹配,它可以讓我加密一個可以用我的Java例程解密的字符串。
這裏是我的java類:
class SimpleProtector
{
private final String ALGORITHM = "AES";
private final byte[] keyValue = new byte[] { 'T', 'h', 'i', 's', 'I', 's', 'A', 'S', 'e', 'c', 'r', 'e', 't', 'K', 'e', 'y' };
public String encrypt(String valueToEnc) throws Exception
{
Key key = generateKey();
Cipher c = Cipher.getInstance(ALGORITHM);
c.init(Cipher.ENCRYPT_MODE, key);
byte[] encValue = c.doFinal(valueToEnc.getBytes());
String encryptedValue = new BASE64Encoder().encode(encValue);
return encryptedValue;
}
public String decrypt(String encryptedValue) throws Exception
{
Key key = generateKey();
Cipher c = Cipher.getInstance(ALGORITHM);
c.init(Cipher.DECRYPT_MODE, key);
byte[] decordedValue = new BASE64Decoder().decodeBuffer(encryptedValue);
byte[] decValue = c.doFinal(decordedValue);
String decryptedValue = new String(decValue);
return decryptedValue;
}
private Key generateKey() throws Exception
{
Key key = new SecretKeySpec(keyValue, ALGORITHM);
return key;
}
}
感謝您的任何提示!
Protip:切勿直接將文本用作加密/解密密鑰。這大大減少了搜索空間(同樣,重複使用相同的對稱密鑰可以更容易地恢復)。相反,使用PBKDF2等將其轉換爲更合適的密鑰材料。獎勵積分,使用像OpenPGP一樣的真正的密鑰管理協議;這樣你確保沒有兩個消息使用相同的會話密鑰,你可以隨意撤銷公鑰等。 – 2010-09-03 01:45:01