gpt4 book ai didi

ios - DES加密值不匹配android和ios

转载 作者:可可西里 更新时间:2023-11-01 05:43:48 24 4
gpt4 key购买 nike

IOS代码为

#import "DESCodec.h"
#import <CommonCrypto/CommonCryptor.h>


@implementation DESCodec
{
NSString *key;
}

-(id) init{
self=[super init];
if(self){
key=@"12345678";
}
return self;
}

-(NSString *) decode:(NSString *)encoded{
NSData *inputData = [[NSData alloc] initWithBase64EncodedString:encoded options:0];
NSData *keyData = [key dataUsingEncoding:NSUTF8StringEncoding];
size_t outLength;

NSMutableData *outputData = [NSMutableData dataWithLength:(inputData.length +

kCCBlockSizeDES)];

CCCryptorStatus
result = CCCrypt(kCCDecrypt, // operation
kCCAlgorithmDES, // Algorithm
kCCOptionPKCS7Padding , // options
keyData.bytes, // key
keyData.length, // keylength
nil,// iv
inputData.bytes, // dataIn
inputData.length, // dataInLength,
outputData.mutableBytes, // dataOut
outputData.length, // dataOutAvailable
&outLength); // dataOutMoved

if (result != kCCSuccess) {
return nil;
}
[outputData setLength:outLength];
return [[NSString alloc] initWithData:outputData `encoding:NSUTF8StringEncoding];`
}


-(NSString *) encode:(NSString *)decoded{

NSData *inputData = [decoded dataUsingEncoding:NSUTF8StringEncoding];
NSData *keyData = [key dataUsingEncoding:NSUTF8StringEncoding];
size_t outLength;

NSMutableData *outputData = [NSMutableData dataWithLength:(inputData.length + kCCBlockSizeDES)];

CCCryptorStatus result = CCCrypt(kCCEncrypt, // operation
kCCAlgorithmDES, // Algorithm
kCCOptionPKCS7Padding, // options
keyData.bytes, // key
keyData.length, // keylength
nil,// iv
inputData.bytes, // dataIn
inputData.length, // dataInLength,
outputData.mutableBytes, // dataOut
outputData.length, // dataOutAvailable
&outLength); // dataOutMoved

if (result != kCCSuccess) {
return nil;
}
[outputData setLength:outLength];
NSString *r = [outputData base64EncodedStringWithOptions:0];


return r;
}

@end

DESCodec *codec=[[DESCodec alloc] init];
NSString *encoded=[codec encode:@"12345678"];
NSString decoded=[codec decode:encoded];
NSLog(@" %@ %@",encoded,decoded);

值为 ltACiHjVjImOJQ1fTHZkSw== 和 12345678

但在java中加密的文本是“ltACiHjVjIn+uVm31GQvyw==”

我不擅长 Objective C,无法触发问题。

谁能帮帮我。感谢和问候

Java代码是

public class DESCodec {

/**
* Secret key that shall be used for encryption and decryption.
*/
private String strSecretKey = "12345678";

private static final String UNICODE_FORMAT = "UTF-8";

private static final String DES_ENCRYPTION_SCHEME = "DES";

private static final String TAG = "DESCodec";

private Cipher cipher;

private SecretKey key;


public DESCodec() {
try {
this.strSecretKey = strSecretKey;
String myEncryptionScheme = DES_ENCRYPTION_SCHEME;
byte[] keyAsBytes = strSecretKey.getBytes(UNICODE_FORMAT);
DESKeySpec myKeySpec = new DESKeySpec(keyAsBytes);
SecretKeyFactory mySecretKeyFactory = SecretKeyFactory.getInstance(myEncryptionScheme);
cipher = Cipher.getInstance(myEncryptionScheme);
key = mySecretKeyFactory.generateSecret(myKeySpec);
} catch (Exception e) {
e.printStackTrace();
}
}



public String desEncrypt(String message) {
String encryptedString = null;
try {
cipher.init(Cipher.ENCRYPT_MODE, key);
byte[] plainText = message.getBytes(UNICODE_FORMAT);
byte[] encryptedText = cipher.doFinal(plainText);

encryptedString = Base64.encodeToString(encryptedText, Base64.DEFAULT);
} catch (Exception e) {
e.printStackTrace();
}
return encryptedString;
}

public String desDecrypt(String message) {
String decryptedText = null;
try {
cipher.init(Cipher.DECRYPT_MODE, key);
byte[] encryptedText = Base64.decode(message, Base64.DEFAULT);
byte[] plainText = cipher.doFinal(encryptedText);
decryptedText = bytes2String(plainText);
} catch (Exception e) {
e.printStackTrace();
}
return decryptedText;
}

private String bytes2String(byte[] bytes) {
try {
return new String(bytes, UNICODE_FORMAT);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
return null;
}

}

最佳答案

很明显只是操作方式的问题,因为第一个block就匹配上了。在 Java 中,您使用的是 ECB 模式,因为“DES”默认为“DES/ECB/PKCS5Padding”。我认为 CCCryptor 默认为 CBC。


永远不要使用 ECB 模式。它在语义上不安全。您至少需要使用带有随机 IV 的 CBC 模式。 IV 不必是 secret 的,因此您可以将其添加到密文之前。请查看具有附加安全功能(如密文身份验证)的 RNCryptor。它还具有 Java 实现。

不要再使用 DES。它不再安全了。你应该使用 AES。三重 DES 也不错。

关于ios - DES加密值不匹配android和ios,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30908637/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com