iphonesdk中的加密和解密

时间:2011-03-08 05:20:33

标签: ios4

您好 这是我的加密和解密的Java代码 现在我想使用iphonesdk以目标c语言编写(转换)此代码 任何人都知道PLZ帮助我...

//public static byte ENC_DEC_KEY = 3;
/*
public static String encrypt(byte key, String cleartext) throws Exception 
{
    char[] chars = cleartext.toCharArray();
    for (int i=0; i < chars.length; i++)
    {
        char c = chars[i];
        if (c > 32 && c < 127)
        {
            int x = c - 32;
            x = (x + key) % 96; // if x > 96 - shift then modulo is 1
            chars[i] = (char) (x + 32);
        } 
    }
    return new String(chars);
}

public static String decrypt(byte key, String encrypted) throws Exception 
{
    char[] chars = encrypted.toCharArray();
    key  = (byte)(0 - key);
    for (int i=0; i < chars.length; i++)
    {
        char c = chars[i];
        if (c > 32 && c < 127)
        {
            // Change base to make life easier, and use an
            // int explicitly to avoid worrying... cast later
            int x = c - 32;
            x = (x + key) % 96;
            //x = x - shift;
            chars[i] = (char) (x + 32);
        }
    }
    return new String(chars);
}*/

1 个答案:

答案 0 :(得分:1)

我建议您使用Apple提供的方法,而不是实现您自己的加密/解密算法。查看详细描述的this文档(包括示例代码),以便在您的应用中实现安全的各个方面。