我們加密/用C#應用程序
的Java decrytpion&C#加密非法密鑰大小問題
var msData = new MemoryStream();
CryptoStream cs = null;
if (inputFileStream.CanSeek)
inputFileStream.Seek(0, SeekOrigin.Begin);
try
{
long inputFileLength = inputFileStream.Length;
var byteBuffer = new byte[4096];
long bytesProcessed = 0;
int bytesInCurrentBlock = 0;
var csRijndael = new RijndaelManaged();
switch (action)
{
case CryptoAction.Encrypt:
cs = new CryptoStream(msData, csRijndael.CreateEncryptor(this.Key, this.IV), CryptoStreamMode.Write);
break;
case CryptoAction.Decrypt:
cs = new CryptoStream(msData, csRijndael.CreateDecryptor(this.Key, this.IV), CryptoStreamMode.Write);
break;
}
while (bytesProcessed < inputFileLength)
{
bytesInCurrentBlock = inputFileStream.Read(byteBuffer, 0, 4096);
cs.Write(byteBuffer, 0, bytesInCurrentBlock);
bytesProcessed += bytesInCurrentBlock;
}
cs.FlushFinalBlock();
return msData.ToArray();
}
catch(Exception ex)
{
new Com.Library.SqlLog().InsertSqlLog(Auth.currentMemberInfo.LoginId, MethodInfo.GetCurrentMethod().Name, ex);
return null;
}
,它是工作fine.Now我創建一個Java應用程序,它是從這個代碼解密我們組織的文檔應該too.Initially解密這些文件我試圖通過這個代碼的Java
的
try{
AlgorithmParameterSpec paramSpec = new IvParameterSpec(IV);
Cipher cipher = Cipher.getInstance("AES");
SecretKeySpec key = new SecretKeySpec(keyGen, "AES");
cipher.init(Cipher.DECRYPT_MODE, key, paramSpec);
byte[] output = new BASE64Decoder().decodeBuffer(new String(convertDocToByteArra("//Path/somePDF.pdf")));
byte[] decrypted = cipher.doFinal(output);
convertByteArrayToDoc(decrypted);
}catch(Exception e){
e.printStackTrace();
}
現在這裏來解密文件是我的疑難問題。通過這個作爲初始化向量
static byte IV[] = new byte[] {0x0E,0x15,(byte)0xC4,(byte)0xBB,
(byte)0xEE,(byte)0xDF,0x72,0x0C,0x4F,0x7E,(byte)0xBE,0x67,0x4A,(byte)0xB6,0x45,(byte)0xDE};
並將此作爲一個重要
static byte[] keyGen = new byte[]{0x78,(byte)0x9C,(byte)0xF1,0x01,0x12,0x31,(byte)0xCD,0x1E,0x1F,0x16,0x54,0x19,0x1D,(byte)0xFF,(byte)0xC7,
0x00,0x51,(byte)0xBF,(byte)0xFD,0x31,(byte)0xE1,(byte)0xA1,(byte)0xDC,(byte)0xC9,0x23,0x49,(byte)0xAD,0x11,0x16,0x17,0x1D,0x1F};
但是當我運行這個程序,然後將它拋出以下
java.security.InvalidKeyException: Illegal key size
at javax.crypto.Cipher.checkCryptoPerm(Cipher.java:1023)
at javax.crypto.Cipher.implInit(Cipher.java:789)
at javax.crypto.Cipher.chooseProvider(Cipher.java:848)
at javax.crypto.Cipher.init(Cipher.java:1347)
at javax.crypto.Cipher.init(Cipher.java:1281)
at DecryptAspEncryptedDocs.main(DecryptAspEncryptedDocs.java:80)
現在給出一個例外,我堅持在這裏做不知道該怎麼做。任何幫助將不勝感激。
編輯1:
我見過this question和罐子放在JRE/lib/security中和JDK/lib目錄/ security.But仍然問題掛起並拋出illegal key size
異常
適用於我(當我將模式更改爲CBC時)。 –
@GregS我提供了AES/CBC/NoPadding,但它再次說「非法密鑰大小」 – Despicable
正如我所說,它適用於我,我知道我已經安裝了無限制的加密策略。儘管有你的信仰,但你似乎沒有。也許您安裝了多個Java版本,或者您錯誤地安裝了策略文件。 –