Objective c 目标C AES加密等于java版本

Objective c 目标C AES加密等于java版本,objective-c,ios,Objective C,Ios,我有java代码来加密密码 public static String encryptToString(String content,String password) throws IOException { return parseByte2HexStr(encrypt(content, password)); } private static byte[] encrypt(String content, String password) { try { KeyG

我有java代码来加密密码

public static String encryptToString(String content,String password) throws IOException {
    return parseByte2HexStr(encrypt(content, password));
}
private static byte[] encrypt(String content, String password) {
    try {
        KeyGenerator kgen = KeyGenerator.getInstance("AES");
        SecureRandom secureRandom = SecureRandom.getInstance("SHA1PRNG");
        secureRandom.setSeed(password.getBytes());
        kgen.init(128, secureRandom);
        SecretKey secretKey = kgen.generateKey();
        byte[] enCodeFormat = secretKey.getEncoded();
        SecretKeySpec key = new SecretKeySpec(enCodeFormat, "AES");
        Cipher cipher = Cipher.getInstance("AES");
        byte[] byteContent = content.getBytes("utf-8");
        cipher.init(Cipher.ENCRYPT_MODE, key);
        byte[] result = cipher.doFinal(byteContent);
        return result;
    } catch (Exception e) {
        log.error(e.getMessage(),e);
    }
    return null;
}
public static String parseByte2HexStr(byte buf[]) {
    StringBuffer sb = new StringBuffer();
    for (int i = 0; i < buf.length; i++) {
        String hex = Integer.toHexString(buf[i] & 0xFF);
        if (hex.length() == 1) {
            hex = '0' + hex;
        }
        sb.append(hex.toUpperCase());
    }
    return sb.toString();
}
publicstaticstringencryptostring(字符串内容、字符串密码)引发IOException{
返回parseByte2HexStr(加密(内容、密码));
}
私有静态字节[]加密(字符串内容、字符串密码){
试一试{
KeyGenerator kgen=KeyGenerator.getInstance(“AES”);
SecureRandom SecureRandom=SecureRandom.getInstance(“SHA1PRNG”);
secureRandom.setSeed(password.getBytes());
kgen.init(128,随机);
SecretKey SecretKey=kgen.generateKey();
字节[]enCodeFormat=secretKey.getEncoded();
SecretKeySpec key=新SecretKeySpec(编码格式,“AES”);
Cipher Cipher=Cipher.getInstance(“AES”);
byte[]byteContent=content.getBytes(“utf-8”);
cipher.init(cipher.ENCRYPT_模式,密钥);
字节[]结果=cipher.doFinal(字节内容);
返回结果;
}捕获(例外e){
log.error(e.getMessage(),e);
}
返回null;
}
公共静态字符串parseByte2HexStr(字节buf[]){
StringBuffer sb=新的StringBuffer();
对于(int i=0;i
现在我需要用objective-c对它进行加密/解密,我做了很多搜索,没有一个方法会生成相同的加密输出
objective-c版本代码与java代码等同于什么?


测试用例:encryptToString(“测试”、“密码”)=>DA180930496EC69BFEBA923B7311037A

我相信这个问题的答案就是您想要的:

我修改了一个函数,有人将其发布为答案:

使用:

NSString *content = @"test";
NSData *dataToEncrypt = [content dataUsingEncoding:NSUTF8StringEncoding];
NSData *data = [dataToEncrypt AES128EncryptWithKey:@"password"];
NSString *hex = [data hexString];

这并不完全相同,因为Java代码不使用密码本身进行加密,而是使用它来为随机数生成器种子。但我认为这仍然适用于您正在尝试的操作。

是的,它显示了CommonCrypto中的AES函数是如何工作的。利用这些知识,您可以轻松地将Java代码转换为Objective-C。是的,如果我使用Java版本指定相同的密钥字节,这是可行的,现在的问题是如何生成/计算正确的密钥。如果我正确理解这一点,您将无法得到完全相同的结果,因为密钥只是随机字节。Security.framework中也有类似的方法。试试Rob Napiers AESKeyForPassword函数,他在他的博客上记录了这个函数:我需要与java版本相同的方法,因为我需要解密iOS上java代码生成的密码。我只是硬编码了关键字节,它就可以工作了。@xfx你能分享你的目标c代码吗?我得到了“D870F7E3B4B00465AB95EB58DB5278DF”使用上述目标c代码请帮助我需要更改什么??非常感谢。