javascript加密代码样例
我们使用CryptoJS进行DES加密。(GitHub: https://github.com/brix/crypto-js)
function crypDES (value, key) { var keyHex = CryptoJS.enc.Utf8.parse(key); var xtoken = CryptoJS.DES.encrypt(value, keyHex, { mode: CryptoJS.mode.ECB, padding: CryptoJS.pad.Pkcs7 }); var str = xtoken.ciphertext.toString(CryptoJS.enc.Base64); str = str.replace(/=/g, ""); str = str.replace(/+/g, "-"); str = str.replace(///g, "_"); return str; },
IOS加密代码样例
/// o2oa DES加密 @param publicKey 加密公钥 func o2DESEncode(code: String, publicKey: String) -> String? { if let encode = desEncrypt(code: code, key: publicKey, iv: "12345678", options: (kCCOptionECBMode + kCCOptionPKCS7Padding)) { let first = encode.replacingOccurrences(of: "+", with: "-") let second = first.replacingOccurrences(of: "/", with: "_") let token = second.replacingOccurrences(of: "=", with: "") return token }else { print("加密错误") return nil } } /// DES 加密 func desEncrypt(code: String, key:String, iv:String, options:Int = kCCOptionPKCS7Padding) -> String? { if let keyData = key.data(using: String.Encoding.utf8), let data = code.data(using: String.Encoding.utf8), let cryptData = NSMutableData(length: Int((data.count)) + kCCBlockSizeDES) { let keyLength = size_t(kCCKeySizeDES) let operation: CCOperation = UInt32(kCCEncrypt) let algoritm: CCAlgorithm = UInt32(kCCAlgorithmDES) let options: CCOptions = UInt32(options) var numBytesEncrypted :size_t = 0 let cryptStatus = CCCrypt(operation, algoritm, options, (keyData as NSData).bytes, keyLength, iv, (data as NSData).bytes, data.count, cryptData.mutableBytes, cryptData.length, &numBytesEncrypted) if UInt32(cryptStatus) == UInt32(kCCSuccess) { cryptData.length = Int(numBytesEncrypted) let base64cryptString = cryptData.base64EncodedString() return base64cryptString } else { return nil } } return nil }
Android加密代码样例
fun o2DESEncode(code: String, publicKey: String): String { val sutil = CryptDES.getInstance(publicKey) var encode = "" try { encode = sutil.encryptBase64(code) Log.d(LOG_TAG,"加密后code:$encode") encode = encode.replace("+", "-") encode = encode.replace("/", "_") encode = encode.replace("=", "") Log.d(LOG_TAG,"替换特殊字符后的code:$encode") }catch (e: Exception) { Log.e(LOG_TAG,"加密失败", e) } return encode } public class CryptDES { private Cipher encryptCipher = null; private Cipher decryptCipher = null; private static CryptDES des = null; public static CryptDES getInstance(String des_key) { try { DESKeySpec key = new DESKeySpec(des_key.getBytes()); SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES"); des = new CryptDES(keyFactory.generateSecret(key)); } catch (Exception e) { e.printStackTrace(); } return des; } private CryptDES(SecretKey key) throws Exception { encryptCipher = Cipher.getInstance("DES"); decryptCipher = Cipher.getInstance("DES"); encryptCipher.init(Cipher.ENCRYPT_MODE, key); decryptCipher.init(Cipher.DECRYPT_MODE, key); } public String encryptBase64 (String unencryptedString) throws Exception { // Encode the string into bytes using utf-8 byte[] unencryptedByteArray = unencryptedString.getBytes("UTF8"); // Encrypt byte[] encryptedBytes = encryptCipher.doFinal(unencryptedByteArray); // Encode bytes to base64 to get a string byte [] encodedBytes = Base64.encode(encryptedBytes, Base64.DEFAULT); return new String(encodedBytes); } public String decryptBase64 (String encryptedString) throws Exception { // Encode bytes to base64 to get a string byte [] decodedBytes = Base64.encode(encryptedString.getBytes(), Base64.DEFAULT); // Decrypt byte[] unencryptedByteArray = decryptCipher.doFinal(decodedBytes); // Decode using utf-8 return new String(unencryptedByteArray, "UTF8"); } }
若有收获,就点个赞吧