我在C#中有一个应用程序,用这种方法用AES算法加密我的文件:
private void decriptFile() throws NoSuchPaddingException, NoSuchAlgorithmException, BadPaddingException, IllegalBlockSizeException, InvalidAlgorithmParameterException, InvalidKeyException, IOException {
//byte[] docBytes = serialize(myDoc);
byte[] b = new byte[0];
try {
Resources res = getResources();
InputStream in_s = res.openRawResource(R.raw.output27);
b = new byte[in_s.available()];
in_s.read(b);
//txtHelp.setText(new String(b));
} catch (Exception e) {
// e.printStackTrace();
//txtHelp.setText("Error: can't show help.");
}
//byte[] dataBytes = FileUtils.readFileToByteArray(File file);
byte[] key = new byte[0];
try {
// key = ("HR$2pIjHR$2pIj12").getBytes("UTF-8");
key = ("HR$2pIjHR$2pIj12").getBytes("UTF-8");
Cipher c = Cipher.getInstance("AES/CBC/PKCS5Padding");
SecretKeySpec k = new SecretKeySpec(key, "AES");
IvParameterSpec iv = new IvParameterSpec(key);
c.init(Cipher.DECRYPT_MODE, k, iv);
// IllegalBlockSizeException Occurred
//File folder = new File(Environment.getExternalStorageDirectory(),
//"test");
File folder = new File("/sdcard",
"test");
if (!folder.exists()) {
folder.mkdir();
}
byte[] decryptedDocBytes = c.doFinal(b);
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(folder.getPath()+"/test.epub"));
bos.write(decryptedDocBytes);
bos.flush();
bos.close();
//DocumentsContract.Document decryptedDoc = (DocumentsContract.Document)deserialize(decryptedDocBytes);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
//IvParameterSpec iv = new IvParameterSpec(key);
//And my serialize/deserialize methods:
}
文件已加密而没有问题。
然后我想用我的Android(2.2)应用程序解密加密文件。所以我这样做:
- (NSData *)decrypt:(NSData *)plainText key:(NSData *)aSymmetricKey padding:(CCOptions *)pkcs7
{
//return [self doCipher:plainText key:aSymmetricKey context:kCCDecrypt padding:pkcs7];
return [self doCipher2:plainText iv:[self generateRandomIV:128] key:aSymmetricKey context:kCCDecrypt error:nil];
}
- (NSData *)doCipher2:(NSData *)dataIn
iv:(NSData *)iv
key:(NSData *)symmetricKey
context:(CCOperation)encryptOrDecrypt // kCCEncrypt or kCCDecrypt
error:(NSError **)error
{
CCCryptorStatus ccStatus = kCCSuccess;
size_t cryptBytes = 0;
NSMutableData *dataOut = [NSMutableData dataWithLength:dataIn.length + kCCBlockSizeAES128];
ccStatus = CCCrypt( encryptOrDecrypt,
kCCAlgorithmAES128,
0, //kCCOptionPKCS7Padding,
symmetricKey.bytes,
kCCKeySizeAES128,
iv.bytes,
dataIn.bytes,
dataIn.length,
dataOut.mutableBytes,
dataOut.length,
&cryptBytes);
if (ccStatus == kCCSuccess) {
dataOut.length = cryptBytes;
}
else {
if (error) {
*error = [NSError errorWithDomain:@"kEncryptionError"
code:ccStatus
userInfo:nil];
}
dataOut = nil;
}
return dataOut;
}
这次解密工作正常。用于解密Objective C中的相同文件 我使用以下方法:
<div class="row">
<div class="col-lg-6 col-lg-offset-3">
<form action="{% url 'chartboard:chart_url' %}" method="post" class="form">
{% csrf_token %}
{% bootstrap_form form layout='inline' %}
{% buttons %}
<button type="submit" class="btn btn-primary">
{% bootstrap_icon "star" %} Submit
</button>
{% endbuttons %}
</form>
</div>
</div>
这次没有运气。可能是什么问题? 任何帮助将不胜感激。
答案 0 :(得分:0)
在Android版本中,您指定PKCS5Padding
但不填充iOS版本。请注意,PKCS5Padding
和PKCS7Padding
相同,只有定义上的区别。
变化:
0, //kCCOptionPKCS7Padding,
到
kCCOptionPKCS7Padding,