C# 我使用的是哪种AES加密?
我试图在android设备上的文件上使用AES256,并用c#解码。这是我第一次尝试这个,所以我真的很困惑。我读了很多信息,但我得到的重要信息是,用于加密的密钥将确定使用哪种加密。 我从这里得到这些课程: 我做了一些修改,效果非常好,但我对此有疑问C# 我使用的是哪种AES加密?,c#,android,encryption,aes,C#,Android,Encryption,Aes,我试图在android设备上的文件上使用AES256,并用c#解码。这是我第一次尝试这个,所以我真的很困惑。我读了很多信息,但我得到的重要信息是,用于加密的密钥将确定使用哪种加密。 我从这里得到这些课程: 我做了一些修改,效果非常好,但我对此有疑问 这是AES256还是AES128 它是否显示了使用AES的正确方法 方法编码摘要(字符串)的目的是什么 此外,我对这两个类的encodeDigest(String)方法进行了更改,以便它们返回大小为32的byte[]。为什么?因为我调试了这一行中的
编码摘要(字符串)
的目的是什么encodeDigest(String)
方法进行了更改,以便它们返回大小为32的byte[]
。为什么?因为我调试了这一行中的android部件kgen.init(256)
:这一行和接下来的几行返回一个大小为32的字节[]
。
我知道这不是一个好主意,但我做到了。我只是想把这件事做好,但现在我真的迷路了
这是我在Android中创建对象的方式:
AESEncryption aesEnc = new AESEncryption("asdfgasdfgasdfgasdfgasdfgasdfgas");
对于Android,我使用这个类:
public class AESEncryption {
public static final String TAG = "AES";
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 AESEncryption(String passphrase) {
byte[] passwordKey = encodeDigest(passphrase);
// KeyGenerator kgen = null;
try {
aesCipher = Cipher.getInstance(CIPHER_TRANSFORMATION);
//kgen = KeyGenerator.getInstance("AES");
} catch (NoSuchAlgorithmException e) {
Log.e(TAG, "No such algorithm " + CIPHER_ALGORITHM, e);
} catch (NoSuchPaddingException e) {
Log.e(TAG, "No such padding PKCS5", e);
}
//kgen.init(256); // 192 and 256 bits may not be available
// Generate the secret key specs.
//SecretKey skey = kgen.generateKey(); //Cantget 'test' in here...
//byte[] raw = skey.getEncoded();
//secretKey = new SecretKeySpec(raw, "AES");
secretKey = new SecretKeySpec(passwordKey, CIPHER_ALGORITHM);
ivParameterSpec = new IvParameterSpec(rawSecretKey);
}
public String encryptAsBase64(byte[] data) {
byte[] encryptedData = encrypt(data);
return Base64.encodeToString(encryptedData, Base64.DEFAULT);
}
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;
}
public byte[] dencrypt(byte[] clearData) {
try {
aesCipher.init(Cipher.DECRYPT_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[] dencryptedData;
try {
dencryptedData = 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 dencryptedData;
}
private byte[] encodeDigest(String text) {
MessageDigest digest;
try {
digest = MessageDigest.getInstance(MESSAGEDIGEST_ALGORITHM);
//return digest.digest(text.getBytes());
return text.getBytes();
} catch (NoSuchAlgorithmException e) {
Log.e(TAG, "No such algorithm " + MESSAGEDIGEST_ALGORITHM, e);
}
return null;
}
}
对于C#这是我创建对象的方式:
Crypto cr = new Crypto("asdfgasdfgasdfgasdfgasdfgasdfgas");
对于C#我使用这个类:
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);
//AesManaged as256 = new AesManaged();
//as256.CreateDecryptor(passwordKey, rawSecretKey);
RijndaelManaged rijndael = new RijndaelManaged();
rijndael.KeySize = 256;
rijndael.Mode = CipherMode.CBC;
rijndael.Padding = PaddingMode.PKCS7;
rijndaelDecryptor = rijndael.CreateDecryptor(passwordKey, rawSecretKey);
}
public byte[] Decrypt(byte[] encryptedData)
{
byte[] newClearData = rijndaelDecryptor.TransformFinalBlock(encryptedData, 0, encryptedData.Length);
// return Encoding.ASCII.GetString(newClearData);
return newClearData;
}
public string DecryptFromBase64(string encryptedBase64)
{
return null;// Decrypt(Convert.FromBase64String(encryptedBase64));
}
private byte[] encodeDigest(string text)
{
MD5CryptoServiceProvider x = new System.Security.Cryptography.MD5CryptoServiceProvider();
return Encoding.ASCII.GetBytes(text);
//return x.ComputeHash(data);
}
}
实际上,你的C代码是错的。256/8=32字节。您提供了16个字节作为AES密钥(字节[]rawSecretKey)。我认为Android也是如此。编译时可能会出现错误。它们工作正常,我没有收到任何错误。如果我在android类上设置32字节,我会得到一个错误,即IV参数应该是16字节长,而在我得到的c#类上:指定的初始化向量(IV)与此算法的块大小不匹配。IV应该始终是CBC的块大小,对于AES,它是16字节。但是在prime6669的答案中,我没有看到任何关于IV的提及,我只看到了密钥大小的提及。在这一行中,RijndaeldCryptor=rijndael.CreateDecryptor(passwordKey,rawSecretKey)我需要传递密钥,而IV不要将密钥作为IV重复使用。您需要为每个要加密的消息创建一个新的IV,并将其与密文一起传输。无论密钥大小如何,IV必须为16字节。