凱撒密碼(凱撒用過的所以叫做凱撒密碼)是一種簡單的加密技術,將字母平移,平移后的字母代替原先的意思。
凱撒密碼中有數字怎么辦。
這里平移3個字母以后,D就代表a。
凱撒密碼公式,實現:
class caesarCipher
{
public:static QByteArray createCipher(const QByteArray & array,int translation)//加密 參數:待加密內容,平移位數{QMap<signed char,signed char> map;auto trsC = static_cast<signed char>(translation);for (auto c = 'A';c <= 'Z';++c){map[c] = c + trsC;}for (auto c = 'a';c <= 'z';++c){map[c] = c + trsC;}QByteArray cipher;for (auto i = 0;i < array.size();++i){cipher.append(map.value(array.at(i)));}return cipher;}static QByteArray decrypt(const QByteArray & cipher,int translation)//解密 參數:待解密內容,平移位數{QMap<signed char,signed char> map;auto trsC = static_cast<signed char>(translation);for (auto c = 'A';c <= 'Z';++c){map[c] = c + trsC;}for (auto c = 'a';c <= 'z';++c){map[c] = c + trsC;}QByteArray data;for (auto i = 0;i < cipher.size();++i){data.append(map.key(cipher.at(i)));}return data;}
};int main(int argc, char *argv[])
{QByteArray array = "helloWorld";auto cipher = caesarCipher::createCipher(array,8);qDebug()<<"加密后:"<<cipher.toHex();qDebug()<<"解密后:"<<caesarCipher::decrypt(cipher,8);
}
上面是一種簡單的實現,大小寫字母平移一定位數。
版权声明:本站所有资料均为网友推荐收集整理而来,仅供学习和研究交流使用。
工作时间:8:00-18:00
客服电话
电子邮件
admin@qq.com
扫码二维码
获取最新动态