0

嗨,我有一个用 java 编写的 web 服务,用于 android 应用程序。现在我必须为 iPhone 开发相同的应用程序,所以我想为 iPhone 使用相同的网络服务器。Web 服务加密/解密 DES 中的字符串。我试图在 Objective C 中加密字符串,这给了我相同模式和相同密钥的不同结果。谁能告诉我代码有什么问题。

这是我编写的第一个 Objective C 代码(我是 iPhone 开发的新手),我使用了这篇文章

NSString* key = @"abc43HU0";
NSString *token = @"hellohello";

const void *vplainText;
size_t plainTextBufferSize = [token length];
vplainText = (const void *) [token UTF8String];    
CCCryptorStatus ccStatus;
uint8_t *bufferPtr = NULL;
size_t bufferPtrSize = 0;
size_t movedBytes = 0;

bufferPtrSize = (plainTextBufferSize + kCCBlockSizeDES) & ~(kCCBlockSizeDES - 1);
bufferPtr = malloc( bufferPtrSize * sizeof(uint8_t));
memset((void *)bufferPtr, 0x0, bufferPtrSize);

Byte iv [] = {0x65, 0x110, 0x68, 0x26, 0x69, 0x178, 0x200, 0x219};

const void *vkey = (const void *) [key UTF8String];

ccStatus = CCCrypt(kCCEncrypt,
                   kCCAlgorithmDES,
                   kCCOptionPKCS7Padding,
                   vkey, 
                   kCCKeySizeDES,
                   iv,
                   vplainText,
                   plainTextBufferSize,
                   (void *)bufferPtr,
                   bufferPtrSize,
                   &movedBytes);

NSData *myData = [NSData dataWithBytes:(const void *)bufferPtr length:(NSUInteger)movedBytes];
NSString* result = [base64 base64EncodeData:myData];//my own method to encoding with base64

这是我加密/解密字符串的Java类

import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;

import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

/**
 * @author rishad.ali
 *
 */
public class Security
{
    //The secret Key to encrypt and decrypt text
    private SecretKey key = null;

    /**
     * Constructor of the class to generate Key
     */
    public Security(String secretCode)
    {
        try
        {
            DESKeySpec keySpec = new DESKeySpec(secretCode.getBytes("UTF8")); 
            SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
            key = keyFactory.generateSecret(keySpec);
        }
        catch (Exception e)
        {
            e.printStackTrace();
        } 
    }
    /**
     * Returns the encrypted text for the given plain text.
     * 
     * @param plainText - given plain text
     * @return
     */
    public String encrypt (String plainText)
    {
        BASE64Encoder base64encoder = new BASE64Encoder();
        //Encrypted Text to be sent
        String encryptedText = "";
        //Encrypt the plain text
        try
        {
            byte[] cleartext = plainText.getBytes("UTF8");      
            Cipher cipher = Cipher.getInstance("DES"); 
            cipher.init(Cipher.ENCRYPT_MODE, key);
            encryptedText = base64encoder.encode(cipher.doFinal(cleartext));
        }
        catch (Exception e)
        {
            e.printStackTrace();
        } 
        return encryptedText;
    }

    /**
     * Returns the plain text for the given encrypted Text
     * 
     * @param encryptedText - Given Encrypted Text
     * @return
     */
    public String decrypt (String encryptedText)
    {
        sun.misc.BASE64Decoder base64decoder = new BASE64Decoder();
        //Plain text to be sent
        String plainText = "";
        // Decrypt the  encrypted text
        try
        {
            byte[] encrypedPwdBytes = base64decoder.decodeBuffer(encryptedText);
            Cipher cipher = Cipher.getInstance("DES");
            cipher.init(Cipher.DECRYPT_MODE, key);
            byte[] plainTextPwdBytes = (cipher.doFinal(encrypedPwdBytes));
            plainText = new String(plainTextPwdBytes, "US-ASCII");
        }
        catch (Exception e)
        {
            e.printStackTrace();
        } 
        return plainText;
    }

    public static void main (String args [])
    {
        Security sec = new Security("secrtkey");
        String text = "sometext";
        System.out.println("Encrypted Text : "+sec.encrypt(text));
    }
}
4

0 回答 0