您好我有一个用java编写的web服务,我用它来安装Android应用程序。现在我必须为iPhone开发相同的应用程序,所以我想为iPhone使用相同的网络服务器。 Web服务在DES中加密/解密字符串。我试图加密Objective C中的字符串,它给出了相同模式和相同键的不同结果。 任何人都可以告诉我代码有什么问题。
这是我写的目标C代码(我是iPhone开发的新手),我使用了this帖子
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));
}
}