![](/img/trans.png)
[英]Encrypt it in C# then Decrypt it with Android ( AES | IllegalBlockSizeException )
[英]Encrypting file in C# and decrypt file in Android programming with aes algorithm
我想在c#中加密文件并在我的android应用中通过java代码解密c#加密文件,我知道最好的算法是AES256,我在android中的代码可以正常工作(加密和解密),但是我可以通过我的android应用解密C#结果文件,我使用以下代码(非常感谢):
加密和解密功能(android):
加密:
static void Encrypt() throws IOException, NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException { try { FileInputStream fis = new FileInputStream(Environment .getExternalStorageDirectory().getAbsolutePath() + "/logo.png"); FileOutputStream fos = new FileOutputStream(Environment .getExternalStorageDirectory().getAbsolutePath() + "/Encrypted"); SecretKeySpec aeskeySpec = new SecretKeySpec( "12345678901234567890123456789012".getBytes(), "AES"); tv.setText(aeskeySpec.getEncoded().toString()); Cipher cipher = Cipher.getInstance("AES"); cipher.init(Cipher.ENCRYPT_MODE, aeskeySpec); CipherOutputStream cos = new CipherOutputStream(fos, cipher); int b; byte[] d = new byte[8]; while ((b = fis.read(d)) != -1) { cos.write(d, 0, b); } cos.flush(); cos.close(); fis.close(); }// try catch (Exception e) { // TODO: handle exception tv.setText("Error :" + e.getMessage()); } }// encrypt
static void Decrypt() throws IOException, InvalidKeyException, NoSuchAlgorithmException, NoSuchPaddingException, IllegalBlockSizeException, BadPaddingException { File file = new File(Environment.getExternalStorageDirectory() .getAbsolutePath() + "/Encrypted"); FileInputStream fis = new FileInputStream(file); long length = file.length(); if (length > Integer.MAX_VALUE) { // File is too large } byte[] bytes = new byte[(int) length]; int offset = 0; int numRead = 0; bytes = IOUtils.toByteArray(fis); byte[] N = new byte[(int) length - offset]; int g, s = 0; for (g = offset; g < length; g++) { N[s++] = bytes[g]; } FileOutputStream fos = new FileOutputStream(Environment .getExternalStorageDirectory().getAbsolutePath() + "/Decrypted"); SecretKeySpec sks = new SecretKeySpec( "12345678901234567890123456789012".getBytes(), "AES"); Cipher cipher = Cipher.getInstance("AES"); cipher.init(Cipher.DECRYPT_MODE, sks); byte[] b = cipher.update(N); int j = 0; while (j < b.length) { fos.write(b[j]); j++; } fos.flush(); fos.close(); }
i use this code for Encrypt in c# :
public void Encrypt(string FIStr, string FOStr, string PassKey) { FileStream fsInput = new FileStream(FIStr, FileMode.Open, FileAccess.Read); FileStream fsEncrypted = new FileStream(FOStr, FileMode.Create, FileAccess.Write); AesCryptoServiceProvider AES = new AesCryptoServiceProvider(); System.Text.ASCIIEncoding encoding = new System.Text.ASCIIEncoding(); byte[] bytes=encoding.GetBytes(PassKey); AES.Key = bytes; ICryptoTransform aesencrypt = AES.CreateEncryptor(); CryptoStream cryptostream = new CryptoStream(fsEncrypted, aesencrypt, CryptoStreamMode.Write); byte[] bytearrayinput = new byte[fsInput.Length]; fsInput.Read(bytearrayinput, 0, bytearrayinput.Length); cryptostream.Write(bytearrayinput, 0, bytearrayinput.Length); cryptostream.Close(); fsInput.Close(); fsEncrypted.Close();}
这段代码是我的文件,但我无法通过我的Android应用程序解密此文件:(,请帮助我,谢谢大家。
以下是一些适合您的想法:
1)使用您的C#代码对一个简单的字符串(例如“ hello”)进行加密,然后将其转到网站http://www.everpassword.com/aes-encryptor ,查看其是否可以成功解密-如果可以,您可以验证您的加密代码效果很好。 同样适用于您的Android代码-转到我提到的相同站点,对诸如“嘿”之类的简单内容进行加密,将加密后的字符串通过您的android代码传递,看看它是否可以成功解密。
2)只需浏览一下代码,我注意到与与Cipher.getInstance()的android文档相比,Cipher.getInstance()的参数看起来有点裸露-http: //developer.android.com/reference/javax/crypto/Cipher .html-注意文档顶部的示例参数。 除了您提供的“ AES”外,它还指定参数,例如模式和填充。 您确定android中的默认模式和填充匹配您的C#加密吗? 只是一个想法。
祝好运!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.