|
 |
|
รบกวนสอบถามเรื่อง Content signature ใน VB.NET ครับ |
|
 |
|
|
 |
 |
|
พอดีผมต้องเขียนโปรแกรมเชื่อมต่อกับ API ของ Payment เจ้านึงซึ่งเขียนด้วย Java ทั้งหมด เพื่อเชื่อมกับระบบของบริษัทผมซึ่งเป็นระบบ POS ซึ่งเขียนด้วย .NET ทั้งหมด ปัญหาอยู่ที่ตอนสร้าง Content signature ครับ ต้องมีการ ้Hash ด้วย SHA256 แล้ว Sign ด้วย Privatekey ซึ่งเป็นไฟล์นามสกุล *pem โดยเจ้าของ Api ให้แต่ตัวอย่าง Code Java มา ส่วน.NET ทางนั้นไม่มีใครเขียนให้ได้ ตอนนี้ผมหาวิธีแปลง Code พวกนั้นให้เป็น .NET ยังไม่ได้ ท่านใดเคยมีประสบการณ์เขียน Code แนว ๆ นี้รบกวนแชร์แนวทางให้ด้วยครับ
นี่เป็นตัวอย่าง Code ภาษา Java ที่ทางเจ้าของ API ให้มาครับ ผมพยายามลองหา Class ต่าง ๆ ใน .NET แล้วยังหาไม่เจอเลยครับ
Code (Java)
private PrivateKey pri;
private PublicKey pub;
private Cipher cipher;
KeyFactory kf = KeyFactory.getInstance("RSA");
byte[] priKeyBytes = Files.readAllBytes(new File("private_key.pem").toPath());
PKCS8EncodedKeySpec spec = new PKCS8EncodedKeySpec(priKeyBytes);
pri = kf.generatePrivate(spec);
byte[] pubKeyBytes = Files.readAllBytes(new File("public_key.pem").toPath());
X509EncodedKeySpec pubSpec = new X509EncodedKeySpec(pubKeyBytes);
pub = kf.generatePublic(pubSpec);
public String createSignature(String message) throws NoSuchAlgorithmException,
SignatureException, IOException, InvalidKeySpecException, InvalidKeyException {
Signature signature = Signature.getInstance("SHA256withRSA");
signature.initSign(pri);
signature.update(message.getBytes());
byte[] signatureBytes = signature.sign();
String encodedSignature = Base64.getEncoder().encodeToString(signatureBytes);
return encodedSignature;
}
public boolean verify(String message, String signature) throws SignatureException,
IOException, NoSuchAlgorithmException, InvalidKeySpecException, InvalidKeyException {
Signature sign = Signature.getInstance("SHA256withRSA");
sign.initVerify(pub);
sign.update(message.getBytes("UTF-8"));
return sign.verify(Base64.getDecoder().decode(signature.getBytes("UTF-8")));
}
Tag : .NET, VB.NET, VS 2005 (.NET 2.x)

|
ประวัติการแก้ไข 2018-02-22 07:52:53 2018-02-22 08:08:06 2018-02-22 08:10:10
|
 |
 |
 |
 |
Date :
2018-02-22 07:36:43 |
By :
Chaan |
View :
982 |
Reply :
3 |
|
 |
 |
 |
 |
|
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ไม่มีใครตอบเบย...
|
 |
 |
 |
 |
Date :
2018-02-25 13:39:50 |
By :
Chaan |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
|
|
|

|
Load balance : Server 01
|