Objective C AESencryption等于java版本

我有java代码来encryption密码,

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

现在我需要使用objective-cencryption/解密,我做了很多search,没有一个方法会生成相同的encryption输出。
什么是Objective-C版本代码等于Java代码?

testing用例:encryptToString(“test”,“password”)=> DA180930496EC69BFEBA923B7311037A

我相信这个问题的答案是你在找什么: AESencryption解密的任何cocoa源代码?

我修改了一个发布的答案: https : //gist.github.com/4335132

使用:

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

这不完全相同 ,因为您的Java代码不使用密码本身进行encryption,而是使用它来播种随机数字生成器。 但我认为这应该仍然与你正在尝试做的工作。