typedef struct structc_tag
{
char c;
double d;
int s;
} structc_t;
应用相同的分析,structc_t需要sizeof(char)+ 7字节填充+ sizeof(double)+ sizeof(int)= 1 + 7 + 8 + 4 = 20字节但sizeof(structc_t)将是24字节.I没有理解对齐。任何人都可以帮助我吗?
答案 0 :(得分:0)
如果我们全部写出来,你的结构就像这样:
RSACryptoServiceProvider encryptKey = new RSACryptoServiceProvider();
UnicodeEncoding encoding = new UnicodeEncoding();
encryptKey.FromXmlString(XML PublicKey);
byte[] usr = encryptKey.Encrypt(encoding.GetBytes(txtUser.Text),false);
byte[] pwd = encryptKey.Encrypt(encoding.GetBytes(txtPassword.Text),false);
let pubKey = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDzZtW7ETOkJGTwN/4adYI5oQZ7U7EPzfDtpZTf+cQ9zAcmcC6g6uAC6KuovBSsigcUNzw3s2eNh0RvYBl6ipJ71hH1awTBwVEWo4fl7uIqdpBjwvO1wWXg9UifpvSsV3GPff9YqMvuggDznOGc20CvsXusQKt9dDx8ESxP6yjqiwIDAQAB"
let keyData = NSData(base64Encoded: pubKey, options: NSData.Base64DecodingOptions.ignoreUnknownCharacters)
var dict: [NSString: AnyObject] = [
kSecClass: kSecClassKey,
kSecAttrKeyType: kSecAttrKeyTypeRSA,
kSecAttrKeySizeInBits: pubKey.utf8.count as AnyObject,
kSecAttrKeyClass: kSecAttrKeyClassPublic,
kSecAttrApplicationTag: "me.lhu" as AnyObject,
kSecValueData: keyData as AnyObject,
kSecReturnRef: true as AnyObject
];
SecItemDelete(dict as CFDictionary)
var err = SecItemAdd(dict as CFDictionary, nil);
if ((err != noErr)) {
print("error loading public key");
}
var resultData: SecKey? = nil
var result: AnyObject?
err = SecItemCopyMatching(dict as CFDictionary, &result)
if err == noErr, let keyRef as! SecKey {
let plaintext = tf_taikhoan.text
let plaintextLen = plaintext?.lengthOfBytes(using: String.Encoding.utf8)
let plaintextBytes = [UInt8](plaintext!.utf8)
var encryptedLen: Int = SecKeyGetBlockSize(keyRef)
var encryptedBytes = [UInt8](repeating: 0, count: encryptedLen)
err = SecKeyEncrypt(keyRef, SecPadding.PKCS1, plaintextBytes, plaintextLen!, &encryptedBytes, &encryptedLen);
if (err != noErr) {
print(encryptedBytes);
}
}
是您不想要的。它发挥作用,因为编译器假定您可能想要创建这些事物的数组,在这种情况下,每个typedef struct structc_tag
{
char c;
char pad1[7];
double d;
int s;
char pad2[4];
} structc_t;
必须对齐,因此pad2
是24。
您可以使用特定于编译器的扩展来启用struct packing,如下所示:
d
现在需要sizeof(structc_t)
(否则typedef struct structc_tag
{
char c;
char pad1[7];
double d;
int s;
} __attribute__((packed)) structc_t;
会立即跟随你不想要的pad1
。并且d
不再隐式添加,因此大小现在应为20。