gpt4 book ai didi

c# - 需要我已经拥有的 Objective-C 版本的 Java 加密和 Base 64 编码

转载 作者:行者123 更新时间:2023-12-01 05:51:46 24 4
gpt4 key购买 nike

我有一个 Java 类,它对字符串进行加密,然后将其转换为 Base64,该类在 Android 应用程序中使用:(此示例中使用了此代码(请注意超链接中的空格):http ://stackoverflow。 com/questions/2090765/cryption-compatable- Between-android-and-c)

public class encryption {
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";
//the secret key in HEX is 'secretkey'
private static byte[] rawSecretKey = {Ox73, Ox65, Ox63, Ox72, Ox65, Ox74, Ox6B, Ox65, Ox79};

private static String MESSAGEDIGEST_ALGORITHM = "MD5";

public encryption(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);
}




//base 64 encryption
public String encryptAsBase64(byte[] clearData) {
byte[] encryptedData = encrypt(clearData);
return 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;
}

}

而base 64加密来自(注意超链接中的空格)http://iharder.sourceforge.net/current/java/base64/

所发生的情况是,它被传递到 C#/.net 服务器进行解密,并且一切在 Android 上运行良好。现在的问题是将其转换为 Objective-C 以在 iPhone 上使用。

我做了很多研究,最接近的答案是 http://dotmac.rationalmind.net/2009/02/aes-interoperability-between-net-and-iphone/

但是,当我使用该示例(直接从下载的 zip 中获取的代码)时,我收到“填充无效且无法删除”的消息。服务器端错误。

我的服务器端代码是:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

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

namespace test.Business
{
public class Crypto
{
private ICryptoTransform rijndaelDecryptor;
// Replace me with a 16-byte key, share between Java and C#
private static byte[] rawSecretKey = {Ox73, Ox65, Ox63, Ox72, Ox65, Ox74, Ox6B, Ox65, Ox79};

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

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

private string Decrypt(byte[] encryptedData)
{
byte[] newClearData;

try
{
newClearData = rijndaelDecryptor.TransformFinalBlock(encryptedData, 0, encryptedData.Length);
}
catch
{
throw;
}
return Encoding.ASCII.GetString(newClearData);
}

internal string DecyptString(string token)
{
//UTF8Encoding utf8 = new UTF8Encoding();
return Decrypt(ASCIIEncoding.ASCII.GetBytes(token));
}

internal 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);
}


//Encryption Code
public string EncryptAsBase64(string strData)
{
byte[] clearData = Encoding.ASCII.GetBytes(strData);
byte[] encryptedData = Encrypt(clearData);
return Convert.ToBase64String(encryptedData);
}

public byte[] Encrypt(byte[] clearData)
{
byte[] test1 = new byte[clearData.Length];
try
{
test1 = rijndaelDecryptor.TransformFinalBlock(clearData, 0, clearData.Length);
}
catch
{
throw;
}

return test1;
}
}
}

有什么想法吗?提前非常感谢您!

最佳答案

您可以在此处获取 NSData 类别来处理 Base64 编码/解码:

http://cocoawithlove.com/2009/06/base64-encoding-options-on-mac-and.html

您可以在此处找到进行 AES 加密的库和 NSData 类别:

http://iphonedevelopment.blogspot.com/2009/02/strong-encryption-for-cocoa-cocoa-touch.html

在两者之间,您可以复制您正在做的事情,并且可能使用更少的代码......

关于c# - 需要我已经拥有的 Objective-C 版本的 Java 加密和 Base 64 编码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4474872/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com