【发布时间】:2017-07-11 09:16:48
【问题描述】:
public static String encryptStringToBase64(String messageString) {
byte[] messageBytes = messageString.getBytes("UTF-8");
byte[] encrypted = convert(1, messageBytes);
return Base64.encodeBytes(encrypted);
}
private static byte[] convert(int mode, byte[] messageBytes) {
MessageDigest sha256 = MessageDigest.getInstance("SHA-256");
sha256.update("abcdefgh".getBytes("UTF-8"));
byte[] keyBytes = sha256.digest();
byte[] hash = Arrays.copyOfRange(keyBytes, 0, 16);
SecretKeySpec keySpec = new SecretKeySpec(hash, "AES");
byte[] ivBytes = new byte[16];
IvParameterSpec ivSpec = new IvParameterSpec(ivBytes);
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
cipher.init(mode, keySpec, ivSpec);
return cipher.doFinal(messageBytes);
}
上面是java中用于加密的逻辑,下面是加密技术 https://gist.github.com/m1entus/f70d4d1465b90d9ee024 https://github.com/krzyzanowskim/CryptoSwift 但我无法在 java 和 iOS 中生成相同的加密字符串。有什么方法可以在 iOS 中重现相同的数据。
Swift 3.0 代码
import CryptoSwift
let ram = "aaaa"
let pas = "abbbb"
let usernameutf8data = ram.data(using: String.Encoding.utf8)
let passwordutf8data = pas.data(using: String.Encoding.utf8)
let copyRight = "abcdefgh"
let copyRightUtf8 = copyRight.data(using: .utf8)
let hash = copyRightUtf8?.sha256()
let key: Array<UInt8> = Array(hash!.bytes[0..<16])
let iv: Array<UInt8> = [0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00]//AES.randomIV(AES.blockSize)
let encrypted = try AES(key:key , iv: iv, blockMode: .CBC, padding: PKCS7()).encrypt(usernameutf8data!)
let encryptedpas = try AES(key:key , iv: iv, blockMode: .CBC, padding: PKCS7()).encrypt(passwordutf8data!)
【问题讨论】:
-
显示你的 Swift 代码并指定什么不起作用
标签: ios swift swift3 cryptoswift