Chào mừng đến với Diễn đàn lập trình - Cộng đồng lập trình.
Kết quả 1 đến 2 của 2
  1. #1
    Ngày tham gia
    Sep 2015
    Bài viết
    0

    Giải đáp vấn đề mã hóa RSA?

    - Vấn đề là em mã hóa bằng public key rồi giải mã bằng private key thì ok !
    Còn ngược lại mà mã hóa bằng private key thì public key không thể giải mã được!
    Bác nào xử lý giúp em !
    đây là source code
    Mã nguồn PHP:
    public static byte[] EncryptBuffer(string rsaKeyString, byte[] btSecret) { int keySize = 0; int blockSize = 0; int lastblockSize = 0; int counter = 0; int iterations = 0; int index = 0; byte[] btPlaintextToken; byte[] btEncryptedToken; byte[] btEncryptedSecret; RSACryptoServiceProvider rsaSender = new RSACryptoServiceProvider(); rsaSender.FromXmlString(rsaKeyString); keySize = rsaSender.KeySize / 8; blockSize = keySize - 11; if ((btSecret.Length % blockSize) != 0) { iterations = btSecret.Length / blockSize + 1; } else { iterations = btSecret.Length / blockSize; } btPlaintextToken = new byte[blockSize]; //See REMARK 2 below. btEncryptedSecret = new byte[iterations * keySize]; //See REMARK 2 below. for (index = 0, counter = 0; counter < iterations; counter++, index += blockSize) { if (counter == (iterations - 1)) //last block { //Size of last block to be encrypted. lastblockSize = btSecret.Length % blockSize; //REMARK 1: Caution! The last block of buffer may be of different size! // Don't copy into a block which is larger than what's actually there in the last block. // - you won't get exception, but last block will not be decrypted properly. btPlaintextToken = new byte[lastblockSize]; Array.Copy(btSecret, index, btPlaintextToken, 0, lastblockSize); } else { Array.Copy(btSecret, index, btPlaintextToken, 0, blockSize); } //REMARK 2: Okay, here's the TRICK! (Again, you won't find this on MSDN!) //(a) Size of blocks going in: blockSize (btPlaintextToken going in) //(b) Size of blocks going out: keySize (btEncryptedToken coming out) //If you don't do this carefully, you get cryptographic exception: "Bad Data" btEncryptedToken = rsaSender.Encrypt(btPlaintextToken, false); //set fOAEP to true only if you have the high encryption pack. Array.Copy(btEncryptedToken, 0, btEncryptedSecret, counter * keySize, keySize); } return btEncryptedSecret; } public static byte[] DecryptBuffer(string rsaKeyString, byte[] btEncryptedSecret) { int keySize = 0; int blockSize = 0; int counter = 0; int iterations = 0; int index = 0; int byteCount = 0; byte[] btPlaintextToken; byte[] btEncryptedToken; byte[] btDecryptedSecret; RSACryptoServiceProvider rsaReceiver = new RSACryptoServiceProvider(); rsaReceiver.FromXmlString(rsaKeyString); keySize = rsaReceiver.KeySize / 8; blockSize = keySize - 11; //REMARK 3: This should always be true: "btEncryptedSecret.Length % keySize == 0" if ((btEncryptedSecret.Length % keySize) != 0) { //Error condition. return null; } iterations = btEncryptedSecret.Length / keySize; btEncryptedToken = new byte[keySize]; //See REMARK 4 below. System.Collections.Queue tokenQueue = new System.Collections.Queue(); for (index = 0, counter = 0; counter < iterations; index += blockSize, counter++) { Array.Copy(btEncryptedSecret, counter * keySize, btEncryptedToken, 0, keySize); //REMARK 4: Okay, here's another trick: //(a) Encrypted blocks going in: btEncryptedToken / size: keySize //(b) Decrypted plain text coming out: btPlaintextToken / size: blockSize btPlaintextToken = rsaReceiver.Decrypt(btEncryptedToken, false); tokenQueue.Enqueue(btPlaintextToken); } byteCount = 0; foreach (byte[] PlaintextToken in tokenQueue) { //REMARK 5: Size of decrypted buffer depends on size of last block. byteCount += PlaintextToken.Length; } counter = 0; btDecryptedSecret = new byte[byteCount]; //REMARK 5 (see foreach loop above) foreach (byte[] PlaintextToken in tokenQueue) { if (counter == (iterations - 1)) { Array.Copy(PlaintextToken, 0, btDecryptedSecret, btDecryptedSecret.Length - PlaintextToken.Length, PlaintextToken.Length); } else { Array.Copy(PlaintextToken, 0, btDecryptedSecret, counter * blockSize, blockSize); } counter++; } return btDecryptedSecret; }  

  2. #2
    Ngày tham gia
    Sep 2015
    Bài viết
    0
    Xem lại các nguyên tắc mà RSA đưa ra, trả lời câu hỏi RSA dùng để làm gì? Tại sao người ta lại dùng RSA. Vấn đề của bạn sẽ được giải quyết.

 

 

Quyền viết bài

  • Bạn Không thể gửi Chủ đề mới
  • Bạn Không thể Gửi trả lời
  • Bạn Không thể Gửi file đính kèm
  • Bạn Không thể Sửa bài viết của mình
  •