public bool Verify(byte[] sign, VPSS.PublicKeySystem publicKeySystem, byte[] data) { try { //Initiate Crypto Service RSACryptoServiceProvider rsa = new RSACryptoServiceProvider(); if (publicKeySystem == VPSS.PublicKeySystem.CheckOut) { rsa.ImportCspBlob(checkOutPublicKey); } else if (publicKeySystem == VPSS.PublicKeySystem.vtcafes) { rsa.ImportCspBlob(vtcafesPublicKey); } else if (publicKeySystem == VPSS.PublicKeySystem.OtherSystem) { rsa.ImportCspBlob(OtherSystemPublicKey); } else { rsa.ImportCspBlob(VPSSPublicKey); } //xac thuc return(rsa.VerifyData(data, new SHA1CryptoServiceProvider(), sign)); } catch { return(false); } }
//Mã hóa một string trả về Base64String public string EncryptToBase64String(string data, VPSS.PublicKeySystem publicKeySystem) { byte[] plainbuffer = System.Text.Encoding.UTF8.GetBytes(data); byte[] encryptbuffer = Encrypt(plainbuffer, publicKeySystem); return(System.Convert.ToBase64String(encryptbuffer)); }
//Mã hóa dữ liệu nhiều khối public byte[] Encrypt(byte[] data, VPSS.PublicKeySystem publicKeySystem) { RSACryptoServiceProvider rsa = new RSACryptoServiceProvider(); //Chose where place to send data if (publicKeySystem == VPSS.PublicKeySystem.CheckOut) { rsa.ImportCspBlob(checkOutPublicKey); } else if (publicKeySystem == VPSS.PublicKeySystem.vtcafes) { rsa.ImportCspBlob(vtcafesPublicKey); } else if (publicKeySystem == VPSS.PublicKeySystem.OtherSystem) { rsa.ImportCspBlob(OtherSystemPublicKey); } else { rsa.ImportCspBlob(VPSSPublicKey); } int block = (data.Length % DECRYPT_BLOCK != 0) ? data.Length / DECRYPT_BLOCK + 1 : data.Length / DECRYPT_BLOCK; int length = (block == 0) ? DATA_BLOCK : block * DATA_BLOCK; byte[] eData = new byte[length]; int i1 = 0, i2 = 0; for (int i = 0; i < block - 1; i++) { byte[] t = new byte[DECRYPT_BLOCK]; for (int j = 0; j < DECRYPT_BLOCK; j++) { t[j] = data[i1++]; } foreach (byte item in rsa.Encrypt(t, false)) { eData[i2++] = item; } } byte[] t1 = new byte[data.Length % DECRYPT_BLOCK]; for (int i = 0; i < t1.Length; i++) { t1[i] = data[i1++]; } // Encrypt the last block byte[] lastBlock = rsa.Encrypt(t1, false); for (int i = 0; i < lastBlock.Length; i++) { eData[i2++] = lastBlock[i]; } return(eData); }
//Xác thực tham số dữ liệu public bool Verify(string Base64sign, VPSS.PublicKeySystem publicKeySystem, params object[] data) { try { //Gop DL thanh một khối System.Text.StringBuilder sb = new System.Text.StringBuilder(); foreach (var item in data) { sb.Append(item); } //Chuyển về byte array byte[] dataBuffer = new System.Text.UTF8Encoding().GetBytes(sb.ToString()); //Chuyển chữ ký về mảng byte byte[] signBuffer = System.Convert.FromBase64String(Base64sign); //xác thực return(Verify(signBuffer, publicKeySystem, dataBuffer)); } catch { return(false); } }