DES加密值不匹配android和ios

DES加密值不匹配android和ios,第1张

概述IOS代码是 #import "DESCodec.h"#import <CommonCrypto/CommonCryptor.h>@implementation DESCodec{ NSString *key;}-(id) init{self=[super init];if(self){ key=@"12345678";}return self;}-(NSS 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)];CCCryptorStatusresult = 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); // dataOutMovedif (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,// dataOut                                 outputData.length,// dataOutAvailable                                 &outLength); // dataOutMovedif (result != kCCSuccess) {    return nil;}[outputData setLength:outLength];Nsstring *r = [outputData base64EncodedStringWithOptions:0];    return r;}@endDESCodec *codec=[[DESCodec alloc] init];Nsstring *encoded=[codec encode:@"12345678"];Nsstring decoded=[codec decode:encoded];NSLog(@" %@ %@",encoded,decoded);

值为ltACiHjVjImOJQ1fTHZkSw ==和12345678

但在java encypted文本中是“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;    }}
解决方法 这显然只是 *** 作模式的一个问题,因为第一个块匹配.在Java中,您使用的是ECB模式,因为“DES”默认为“DES / ECB / PKCS5padding”.我认为CCCryptor默认为CBC.

不要使用ECB模式.它在语义上不安全.您需要至少使用随机IV的CBC模式. IV不必是秘密的,因此您可以将其添加到密文中.请查看具有其他安全功能的RNCryptor,例如密文验证.它还有一个Java实现.

不要再使用DES了.它不再安全了.你应该使用AES.三重DES也不是那么糟糕.

总结

以上是内存溢出为你收集整理的DES加密值不匹配android和ios全部内容,希望文章能够帮你解决DES加密值不匹配android和ios所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1127852.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-30
下一篇 2022-05-30

发表评论

登录后才能评论

评论列表(0条)

保存