使用aes算法在C#中加密文件并在Android编程中解密文件

时间:2012-08-12 06:10:52

标签: c# java android

我想在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应用程序解密这个文件:(,PLZ帮帮我,谢谢大家。

1 个答案:

答案 0 :(得分:0)

以下是一些想法:

1)使用您的c#代码加密一个简单的字符串,例如“hello”,然后将其带到网站http://www.everpassword.com/aes-encryptor,看看它是否会成功解密 - 如果是这样,您可以验证您的加密代码是否正常工作大。你的Android代码也是如此 - 转到我提到的同一个网站,加密一些简单的东西,比如“嘿你”,通过你的android代码传递加密的字符串,看看它是否会成功解密。

2)只是浏览一下你的代码我注意到Cipher.getInstance()的参数与Cipher.getInstance() - http://developer.android.com/reference/javax/crypto/Cipher.html的android文档相比看起来有点光秃 - 请注意顶部的示例参数文件。除了您提供的“AES”之外,它还指定参数,例如模式和填充。你确定android中的默认模式和填充匹配你的C#加密吗?只是一个想法。

祝你好运!