Android与C#之间的加密兼容 [英] Encryption compatible between Android and C#

查看:157
本文介绍了Android与C#之间的加密兼容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经发现了很多示例,如何在C#中加密,还有一些用于Android,但是我特别寻找一种方法来处理来自Android的加密(使用AES,TripleDES等),以及最终在C#中被解密。我在Android中发现了一个用于编码AES 的示例,编码/解码AES ,但我不知道这些是否兼容(C#需要一个IV,在Android示例)。另外,关于通过HTTP(Base64?)传输的加密字符串编码的好方法的建议将是有帮助的。谢谢。

解决方案

http://oogifu.blogspot.com/2009/01/aes-in-java-and-c.html 。 p>

这是我的Java类:

  package com.neocodenetworks.smsfwd; 

import java.security。*;
import javax.crypto。*;
import javax.crypto.spec。*;
import android.util.Log;

public class Crypto {
public static final String TAG =smsfwd;

私有静态密码aesCipher;
private static SecretKey secretKey;
private static IvParameterSpec ivParameterSpec;

private static String CIPHER_TRANSFORMATION =AES / CBC / PKCS5Padding;
private static String CIPHER_ALGORITHM =AES;
//替换一个16字节的密钥,在Java和C#之间共享
private static byte [] rawSecretKey = {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};

private static String MESSAGEDIGEST_ALGORITHM =MD5;

public Crypto(String passphrase){
byte [] passwordKey = encodeDigest(passphrase);

try {
aesCipher = Cipher.getInstance(CIPHER_TRANSFORMATION);
} catch(NoSuchAlgorithmException e){
Log.e(TAG,No such algorithm+ CIPHER_ALGORITHM,e);
} catch(NoSuchPaddingException e){
Log.e(TAG,No such padding PKCS5,e);
}

secretKey = new SecretKeySpec(passwordKey,CIPHER_ALGORITHM);
ivParameterSpec = new IvParameterSpec(rawSecretKey);
}

public String encryptAsBase64(byte [] clearData){
byte [] encryptedData = encrypt(clearData);
return net.iharder.base64.Base64.encodeBytes(encryptedData);
}

public byte [] encrypt(byte [] clearData){
try {
aesCipher.init(Cipher.ENCRYPT_MODE,secretKey,ivParameterSpec);
} catch(InvalidKeyException e){
Log.e(TAG,无效键,e);
返回null;
} catch(InvalidAlgorithmParameterException e){
Log.e(TAG,Invalid algorithm+ CIPHER_ALGORITHM,e);
返回null;
}

byte [] encryptedData;
try {
encryptedData = aesCipher.doFinal(clearData);
} catch(IllegalBlockSizeException e){
Log.e(TAG,非法块大小,e);
返回null;
} catch(BadPaddingException e){
Log.e(TAG,Bad padding,e);
返回null;
}
return encryptedData;
}

private byte [] encodeDigest(String text){
MessageDigest digest;
try {
digest = MessageDigest.getInstance(MESSAGEDIGEST_ALGORITHM);
return digest.digest(text.getBytes());
} catch(NoSuchAlgorithmException e){
Log.e(TAG,No such algorithm+ MESSAGEDIGEST_ALGORITHM,e);
}

返回null;
}
}

我使用了 http://iharder.sourceforge.net/current/java/base64/



这是我的C#类:

  using System; 
使用System.Text;
使用System.Security.Cryptography;

命名空间smsfwdClient
{
public class Crypto
{
private ICryptoTransform rijndaelDecryptor;
//替换一个16字节的密钥,在Java和C#之间共享
private static byte [] rawSecretKey = {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};

public Crypto(string passphrase)
{
byte [] passwordKey = encodeDigest(passphrase);
RijndaelManaged rijndael = new RijndaelManaged();
rijndaelDecryptor = rijndael.CreateDecryptor(passwordKey,rawSecretKey);
}

public string Decrypt(byte [] encryptedData)
{
byte [] newClearData = rijndaelDecryptor.TransformFinalBlock(encryptedData,0,encryptedData.Length);
return Encoding.ASCII.GetString(newClearData);
}

public string DecryptFromBase64(string encryptedBase64)
{
return Decrypt(Convert.FromBase64String(encryptedBase64));
}

private byte [] encodeDigest(string text)
{
MD5CryptoServiceProvider x = new System.Security.Cryptography.MD5CryptoServiceProvider();
byte [] data = Encoding.ASCII.GetBytes(text);
return x.ComputeHash(data);
}
}
}

我真的希望这有助于某人其他!


I've found plenty of examples how to do encryption in C#, and a couple for Android, but I'm particularly looking for a way to handle encrypting (using something like AES, TripleDES, etc.) from Android, and eventually wind up being decrypted in C#. I found an example for encoding AES in Android and encoding/decoding AES in C# but am not sure if these are compatible (C# requires an IV, nothing is specified for this in the Android example). Also, a recommendation on a good way of encoding the encrypted string for transmission over HTTP (Base64?) would be helpful. Thanks.

解决方案

Got some help from http://oogifu.blogspot.com/2009/01/aes-in-java-and-c.html.

Here is my Java class:

package com.neocodenetworks.smsfwd;

import java.security.*;
import javax.crypto.*;
import javax.crypto.spec.*;
import android.util.Log;

public class Crypto {
    public static final String TAG = "smsfwd";

    private static Cipher aesCipher;
    private static SecretKey secretKey;
    private static IvParameterSpec ivParameterSpec;

    private static String CIPHER_TRANSFORMATION = "AES/CBC/PKCS5Padding";
    private static String CIPHER_ALGORITHM = "AES";
    // Replace me with a 16-byte key, share between Java and C#
    private static byte[] rawSecretKey = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
                                          0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};

    private static String MESSAGEDIGEST_ALGORITHM = "MD5";

    public Crypto(String passphrase) {
        byte[] passwordKey = encodeDigest(passphrase);

        try {
            aesCipher = Cipher.getInstance(CIPHER_TRANSFORMATION);
        } catch (NoSuchAlgorithmException e) {
            Log.e(TAG, "No such algorithm " + CIPHER_ALGORITHM, e);
        } catch (NoSuchPaddingException e) {
            Log.e(TAG, "No such padding PKCS5", e);
        }

        secretKey = new SecretKeySpec(passwordKey, CIPHER_ALGORITHM);
        ivParameterSpec = new IvParameterSpec(rawSecretKey);
    }

    public String encryptAsBase64(byte[] clearData) {
        byte[] encryptedData = encrypt(clearData);
        return net.iharder.base64.Base64.encodeBytes(encryptedData);
    }

    public byte[] encrypt(byte[] clearData) {
        try {
            aesCipher.init(Cipher.ENCRYPT_MODE, secretKey, ivParameterSpec);
        } catch (InvalidKeyException e) {
            Log.e(TAG, "Invalid key", e);
            return null;
        } catch (InvalidAlgorithmParameterException e) {
            Log.e(TAG, "Invalid algorithm " + CIPHER_ALGORITHM, e);
            return null;
        }

        byte[] encryptedData;
        try {
            encryptedData = aesCipher.doFinal(clearData);
        } catch (IllegalBlockSizeException e) {
            Log.e(TAG, "Illegal block size", e);
            return null;
        } catch (BadPaddingException e) {
            Log.e(TAG, "Bad padding", e);
            return null;
        }
        return encryptedData;
    }

    private byte[] encodeDigest(String text) {
        MessageDigest digest;
        try {
            digest = MessageDigest.getInstance(MESSAGEDIGEST_ALGORITHM);
            return digest.digest(text.getBytes());
        } catch (NoSuchAlgorithmException e) {
            Log.e(TAG, "No such algorithm " + MESSAGEDIGEST_ALGORITHM, e);
        }

        return null;
    }
}

I used http://iharder.sourceforge.net/current/java/base64/ for the base64 encoding.

Here's my C# class:

using System;
using System.Text;
using System.Security.Cryptography;

namespace smsfwdClient
{
    public class Crypto
    {
        private ICryptoTransform rijndaelDecryptor;
        // Replace me with a 16-byte key, share between Java and C#
        private static byte[] rawSecretKey = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
                                              0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};

        public Crypto(string passphrase)
        {
            byte[] passwordKey = encodeDigest(passphrase);
            RijndaelManaged rijndael = new RijndaelManaged();
            rijndaelDecryptor = rijndael.CreateDecryptor(passwordKey, rawSecretKey);
        }

        public string Decrypt(byte[] encryptedData)
        {
            byte[] newClearData = rijndaelDecryptor.TransformFinalBlock(encryptedData, 0, encryptedData.Length);
            return Encoding.ASCII.GetString(newClearData);
        }

        public string DecryptFromBase64(string encryptedBase64)
        {
            return Decrypt(Convert.FromBase64String(encryptedBase64));
        }

        private byte[] encodeDigest(string text)
        {
            MD5CryptoServiceProvider x = new System.Security.Cryptography.MD5CryptoServiceProvider();
            byte[] data = Encoding.ASCII.GetBytes(text);
            return x.ComputeHash(data);
        }
    }
}

I really hope this helps someone else!

这篇关于Android与C#之间的加密兼容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆